Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hhchow7 opened this issue Mar 22, 2021 · 2 comments
Closed

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

hhchow7 opened this issue Mar 22, 2021 · 2 comments

Comments

@hhchow7
Copy link

hhchow7 commented Mar 22, 2021

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?

@daffl
Copy link
Member

daffl commented Mar 24, 2021

The correct way to register hooks is app.service('authentication').hooks():

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

@daffl daffl closed this as completed Mar 24, 2021
@hhchow7
Copy link
Author

hhchow7 commented Apr 7, 2021

The correct way to register hooks is app.service('authentication').hooks():

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

/server/node_modules/@feathersjs/authentication/lib/service.js:141

this.hooks({

^

TypeError: this.hooks is not a function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants