我有一个典型的带有登录表单的web应用程序,我正在尝试使用feathersjs作为后端,通过rest对用户进行身份验证。我使用angular 4作为前端。
Angular 4中的前端身份验证服务:
import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class AuthService {
private BASE_URL: string = 'http://localhost:3030';
private headers: HttpHeaders = new HttpHeaders({'Content-Type': 'application/json'});
constructor(private http: HttpClient) {}
login(user): Promise<any> {
let url: string = `${this.BASE_URL}/authentication`;
return this.http.post(url, user, {headers: this.headers}).toPromise();
}
}后端的config/default.json:
"authentication": {
"secret": "my-secret",
"strategies": [
"jwt",
"local"
],
"path": "/authentication",
"service": "users",
"jwt": {
"header": {
"typ": "access"
},
"audience": "https://yourdomain.com",
"subject": "anonymous",
"issuer": "feathers",
"algorithm": "HS256",
"expiresIn": "1d"
},
"local": {
"entity": "teams",
"usernameField": "email",
"passwordField": "password"
}
}后端的authentication.js
const authentication = require('feathers-authentication');
const jwt = require('feathers-authentication-jwt');
const local = require('feathers-authentication-local');
module.exports = function () {
const app = this;
const config = app.get('authentication');
// Set up authentication with the secret
app.configure(authentication(config));
app.configure(jwt());
app.configure(local());
app.service('authentication').hooks({
before: {
create: [
authentication.hooks.authenticate(config.strategies)
],
remove: [
authentication.hooks.authenticate('jwt')
]
}
});
};有了上面的内容,我得到了/authentication端点的404。我是否必须手动创建身份验证端点,还是由featherjs为我创建它?你能举个例子吗?
发布于 2017-11-21 03:23:33
诀窍是在请求正文中包含“策略”。
{
"email": "test@test.com",
"password": "1234",
"strategy": "local"
}发布于 2018-09-06 15:13:25
您需要对客户端进行身份验证,然后才能发出请求。以下是执行此操作的示例:
const io = require('socket.io-client');
const feathers = require('@feathersjs/client');
const socketio = require('@feathersjs/socketio-client');
const authentication = require('@feathersjs/authentication-client');
const LocalStorage = require('node-localstorage').LocalStorage;
const client = feathers();
const socket = io('http://localhost:3030');
const localStorage = new LocalStorage('./storage');
client.configure(socketio(socket));
client.configure(authentication({storage: localStorage}));
client
.authenticate(['jwt', 'local']).then(() => {
console.log("Auto authenticated");
}).catch(error => {
console.log(error);
client
.authenticate({
strategy: 'local', email: "feathers@example.com",
password: "secret"
}).then(() => {
console.log("Authenticated using Email");
}).catch(error => {
console.log('Unable to authenticate to server');
process.exit(1);
});
});
client.on('authenticated', (login) => {
console.log('Authenticated to server', login);
client.service('task').create({
text: 'A message from a REST client'
}).then(() => {
console.log("Task created");
}).catch(error => {
console.log("error", error);
});
});https://stackoverflow.com/questions/47393879
复制相似问题