javascript - FeathersJS/Angular 4 中的身份验证

标签 javascript angular feathersjs

我有一个带有登录表单的典型网络应用程序,我正在尝试使用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/

相关文章:

javascript - 从不同的文件夹运行program.js

javascript - for..in 循环内的问题

angular - 将 firestore 查询与 rxjs 结合(或查询)

javascript - 想要用 ngif 在 div 中加载 img,当 ngif==false

FeathersJs 验证返回 NotAuthenticated : Invalid login

reactjs - 使用 feathers express 时,出现页面未找到错误 #2355

javascript - useEffect 在自定义钩子(Hook)中使用 ref 缺少依赖项警告

javascript - 使用 jQuery 防止回发并运行 OnClick

.net - 减少 Angular 8 中的 Vendor.js 文件大小

javascript - Feathers JS - 创建多个条目 - 愚蠢的问题