Search code examples
feathersjs

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


I am using Feathers.js for the backend.

This is the original response from 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",
    }
}

I want to discard the accessToken field from the response, so I modified authentication.js as:

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());
};

But nothing changes after I've changed the code to the above one.

What is wrong here?


Solution

  • The hook function should be put in the last.

    ...
    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')
          ]
        }
       });
    };