首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FeathersJS身份验证停用用户

FeathersJS身份验证停用用户
EN

Stack Overflow用户
提问于 2018-02-06 07:06:47
回答 3查看 452关注 0票数 1

我正在使用FeathersJS,并且对它提供的身份验证很满意。在这种情况下,它是本地JWT。客户端请求具有禁用某些功能的用户管理。在用户模型中有字段isDisabled,但很难确定应该在哪里执行检查以及如何设置它。

"@feathersjs/feathers": "^3.0.2", "@feathersjs/authentication": "^2.1.0", "@feathersjs/authentication-jwt": "^1.0.1", "@feathersjs/authentication-local": "^1.0.2",

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-02-07 01:22:57

这取决于您想要检查的位置。您可以在users服务上为get方法创建customize the JWT verifierhook

代码语言:javascript
复制
app.service('users').hooks({
  after: {
    get(context) {
      const user = context.result;

      if(user.isDisabled) {
        throw new Error('This user has been disabled');
      }
    }
  }
});
票数 2
EN

Stack Overflow用户

发布于 2018-07-10 03:32:55

我直接在我的身份验证钩子中做到了这一点:

代码语言:javascript
复制
const { authenticate } = require('@feathersjs/authentication').hooks
const { NotAuthenticated } = require('@feathersjs/errors')

const verifyIdentity = authenticate('jwt')

function hasToken(hook) {
  if (hook.params.headers == undefined) return false
  if (hook.data.accessToken == undefined) return false
  return hook.params.headers.authorization || hook.data.accessToken
}

module.exports = async function authenticate(context) {
  try {
    await verifyIdentity(context)
  } catch (error) {
    if (error instanceof NotAuthenticated && !hasToken(context)) {
      return context
    }
  }
  if (context.params.user && context.params.user.disabled) {
    throw new Error('This user has been disabled')
  }
  return context
}

您可以看到,我确实检查了刚刚加载的用户记录,并抛出了一个错误,以防万一。当这个钩子在before:all中被调用时,用户在执行任何操作之前就被拒绝了。

票数 1
EN

Stack Overflow用户

发布于 2021-01-28 02:13:16

至于羽毛4,你可以很容易地扩展你的身份验证策略。例如,如果我们希望用户只能登录并验证他们的JWT,我们可以在authentication.ts (Typescript)中执行以下操作:

代码语言:javascript
复制
import { Id, Query, ServiceAddons } from '@feathersjs/feathers';
import { AuthenticationService, JWTStrategy } from '@feathersjs/authentication';
import { LocalStrategy } from '@feathersjs/authentication-local';
import { expressOauth } from '@feathersjs/authentication-oauth';

import { Application } from './declarations';

declare module './declarations' {
  interface ServiceTypes {
    'authentication': AuthenticationService & ServiceAddons<any>;
  }
}

通过更改getEntityQuery将本地策略扩展到仅包含活动用户。

代码语言:javascript
复制
class CustomLocalStrategy extends LocalStrategy {
  async getEntityQuery(query: Query) {
    return {
      ...query,
      active: true,
      $limit: 1
    };
  }
}

通过更改getEntity()来扩展JWT策略,以便在用户不活动时返回null

代码语言:javascript
复制
class CustomJWTStrategy extends JWTStrategy {
  async getEntity(id: Id) {
    const entity = await this.entityService.get(id);

    if (!entity.active) {
      return null;
    }

    return entity;
  }
}

export default function(app: Application): void {
  const authentication = new AuthenticationService(app);

  authentication.register('jwt', new CustomJWTStrategy());
  authentication.register('local', new CustomLocalStrategy());

  app.use('/authentication', authentication);
  app.configure(expressOauth());
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48632949

复制
相关文章

相似问题

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