首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >feathersjs / angular 4中的身份验证

feathersjs / angular 4中的身份验证
EN

Stack Overflow用户
提问于 2017-11-20 22:03:40
回答 2查看 331关注 0票数 0

我有一个典型的带有登录表单的web应用程序,我正在尝试使用feathersjs作为后端,通过rest对用户进行身份验证。我使用angular 4作为前端。

Angular 4中的前端身份验证服务:

代码语言:javascript
复制
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:

代码语言:javascript
复制
"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

代码语言:javascript
复制
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为我创建它?你能举个例子吗?

EN

回答 2

Stack Overflow用户

发布于 2017-11-21 03:23:33

诀窍是在请求正文中包含“策略”。

代码语言:javascript
复制
{
  "email": "test@test.com",
  "password": "1234",
  "strategy": "local"
}
票数 1
EN

Stack Overflow用户

发布于 2018-09-06 15:13:25

您需要对客户端进行身份验证,然后才能发出请求。以下是执行此操作的示例:

代码语言:javascript
复制
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);
            });
        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47393879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档