feathers.js - 丢弃挂钩在 authentication.js 中不起作用

feathers.js - discard hook not working in authentication.js

我正在使用 Feathers.js 作为后端。

这是来自 POST /authentication

的原始回复
{
    "accessToken": "XXXXX",
    "authentication": {
        "strategy": "local",
        "accessToken": "XXXXX",
        "payload": {
            "iat": 1616402936,
            "exp": 1616489336,
            "aud": "https://yourdomain.com",
            "iss": "feathers",
            "sub": "c15ef318-68fc-471c-9710-52f14d87abda",
            "jti": "57d103e1-c81b-4fc6-8bbe-952b74aaf8e3"
        }
    },
    "user": {
        "id": "c15ef320-68fc-471c-9710-52f14d87ccda",
        "email": "abc.abc@abc.com",
    }
}

我想从响应中丢弃 accessToken 字段,所以我将 authentication.js 修改为:

const { AuthenticationService, JWTStrategy } = require('@feathersjs/authentication');
const { LocalStrategy } = require('@feathersjs/authentication-local');
const { expressOauth } = require('@feathersjs/authentication-oauth');
const { discard, iff, isProvider, lowerCase } = require('feathers-hooks-common')

module.exports = app => {
  const authentication = new AuthenticationService(app);

  authentication.register('jwt', new JWTStrategy());
  authentication.register('local', new LocalStrategy());

  authentication.hooks = {
    before: {
      create: [lowerCase('email')],
      update: [lowerCase('email')],
      patch: [lowerCase('email')],
    },
    after: {
      create: [discard('accessToken')]
    }
  };

  app.use('/authentication', authentication);
  app.configure(expressOauth());
};

但是我把代码改成上面的代码后没有任何变化

这里有什么问题?

API documentation on registering hooks 之后应该是 app.service('/authentication').hooks(hooks):

const { AuthenticationService, JWTStrategy } = require('@feathersjs/authentication');
const { LocalStrategy } = require('@feathersjs/authentication-local');
const { expressOauth } = require('@feathersjs/authentication-oauth');
const { discard, iff, isProvider, lowerCase } = require('feathers-hooks-common')

module.exports = app => {
  const authentication = new AuthenticationService(app);

  authentication.register('jwt', new JWTStrategy());
  authentication.register('local', new LocalStrategy());

  app.use('/authentication', authentication);
  app.service('/authentication').hooks({
    before: {
      create: [lowerCase('email')],
      update: [lowerCase('email')],
      patch: [lowerCase('email')],
    },
    after: {
      create: [discard('accessToken')]
    }
  });
  app.configure(expressOauth());
};

钩子函数应该放在最后

...
module.exports = app => {
  const authentication = new AuthenticationService(app);

  authentication.register('jwt', new JWTStrategy());
  authentication.register('local', new LocalStrategy());
 
  app.use('/authentication', authentication);
  app.configure(expressOauth());

  app.service('authentication').hooks({
    before: {
      create: [lowerCase('email')],
      update: [lowerCase('email')],
      patch: [lowerCase('email')],
    },
    after: {
      create: [
        discard('authentication')
      ]
    }
   });
};