我有一个带有登录表单的典型网络应用程序,我正在尝试使用feathersjs作为后端来验证用户的身份。我在前端使用 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 为我创建它?能举个例子吗?
最佳答案
诀窍是在请求正文中包含“策略”。
{
"email": "test@test.com",
"password": "1234",
"strategy": "local"
}
关于javascript - FeathersJS/Angular 4 中的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47393879/