Javascript FeatherJS/angular 4中的身份验证
我有一个典型的带有登录表单的web应用程序,我正在尝试使用FeatherJS作为后端,通过rest对用户进行身份验证。我使用角4作为前端 Angular 4中的前端身份验证服务:Javascript FeatherJS/angular 4中的身份验证,javascript,angular,feathersjs,Javascript,Angular,Feathersjs,我有一个典型的带有登录表单的web应用程序,我正在尝试使用FeatherJS作为后端,通过rest对用户进行身份验证。我使用角4作为前端 Angular 4中的前端身份验证服务: import { Injectable } from '@angular/core'; import { HttpHeaders, HttpClient } from '@angular/common/http'; import 'rxjs/add/operator/toPromise'; @Injectable()
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();
}
}
后端中的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')
]
}
});
};
根据以上内容,我得到了404作为/authentication端点。我必须手动创建身份验证端点还是featherjs为我创建它?你能提供一个例子吗?技巧是在请求正文中包含“策略”
{
"email": "test@test.com",
"password": "1234",
"strategy": "local"
}
在发出请求之前,您需要对客户端进行身份验证。以下是执行此操作的示例:
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);
});
});
为什么会提到Angular?这个问题是特定于后端的,可以通过Postman之类的工具进行调试和复制。是的,
/authentication
应该是这样工作的。仔细检查节点控制台中的auth插件是否没有错误。考虑提供(例如回购),因为羽毛包装版本很重要。
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);
});
});