feathersjs-ecosystem / authentication-local

[MOVED] Local authentication plugin for feathers-authentication
https://github.com/feathersjs/feathers
MIT License
26 stars 15 forks source link

cannot apply custom verifier #66

Closed ducnvhn closed 6 years ago

ducnvhn commented 6 years ago

Steps to reproduce

I followed exactly the example to create custom verifier in the document. but still cannot make it work. I want to customize the verifier to check if user is disabled / lock and if that the case, will reject the authentication request. (First please check that this issue is not already solved as described No! here) this is the error msg i got: F:\ongoing\november\node_modules\@feathersjs\authentication-local\lib\index.js:45 [2] let verifier = new Verifier(app, localSettings); [2] ^ [2] [2] TypeError: Verifier is not a constructor

Expected behavior

custom verifier should run as in the doc.

Actual behavior

throw an error

Please help to point out where I were wrong ? thank you.

daffl commented 6 years ago

You have to show more of your code. The error just says that the Verifier you passed is not a class

ducnvhn commented 6 years ago

Thanks @daffl for your response. Please help to review my code:

customVerifier.js
import local, { Verifier } from '@feathersjs/authentication-local';

class CustomVerifier extends Verifier {
  // The verify function has the exact same inputs and
  // return values as a vanilla passport strategy
  verify(req, username, password, done) {
    // do your custom stuff. You can call internal Verifier methods
    // and reference this.app and this.options. This method must be implemented.

    // the 'user' variable can be any truthy value
    // the 'payload' is the payload for the JWT access token that is generated after successful authentication
    done(null, user, payload);
  }
}

feathers.js authentication service

import auth from '@feathersjs/authentication';
import local from '@feathersjs/authentication-local';
import CustomVerifierfrom './customVerifier.js';
export default function authenticationService() {
  const app = this;

  const config = app.get('config').auth;

  app
    .configure(auth(config))
    .configure(jwt())
    .configure(local({ Verifier: CustomVerifier)) // .configure(oauth1()) // TODO twitter example
    .configure(oauth2({
      name: 'facebook', // if the name differs from your config key you need to pass your config options explicitly
      Strategy: FacebookTokenStrategy
    }));

  app.service('authentication').hooks({
    before: {
      // You can chain multiple strategies on create method
      create: auth.hooks.authenticate(['jwt', 'local', 'facebook']),
      remove: auth.hooks.authenticate('jwt')
    },
    after: {
      create: [populateUser(config), discard('user.password'), restToSocketAuth()]
    }
  });
}
bertho-zero commented 6 years ago

I have deleted restToSocketAuth in https://github.com/bertho-zero/react-redux-universal-hot-example :smile:

ducnvhn commented 6 years ago

@bertho-zero is that the reason why I cannot use custom verifier ?

bertho-zero commented 6 years ago

No, I just recognized my code.

EDIT: Have you updated your fork ?

daffl commented 6 years ago

If customVerifier.js is the entire class, I would say that you forgot export default:

import local, { Verifier } from '@feathersjs/authentication-local';

export default class CustomVerifier extends Verifier {
  // The verify function has the exact same inputs and
  // return values as a vanilla passport strategy
  verify(req, username, password, done) {
    // do your custom stuff. You can call internal Verifier methods
    // and reference this.app and this.options. This method must be implemented.

    // the 'user' variable can be any truthy value
    // the 'payload' is the payload for the JWT access token that is generated after successful authentication
    done(null, user, payload);
  }
}
ducnvhn commented 6 years ago

I made the change as your suggestion, actually I've try to do that but this is the error after changing to export default class ...

 Path:\To\Project\Project\folder\customVerifier.js:39
[2]     return (0, _possibleConstructorReturn3.default)(this, (CustomVerifier.__proto__ || (0, _getPrototypeOf2.default)(CustomVerifier)).apply(this, arguments));
[2]                                                                                                                                       ^
[2]
[2] TypeError: Class constructor LocalVerifier cannot be invoked without 'new'
[2]     at new CustomVerifier (Path:\To\Project\Project\folder\customVerifier.js:39:135)
[2]     at Function.app.setup (Path:\To\Project\node_modules\@feathersjs\authentication-local\lib\index.js:45:22)
[2]     at Function.app.setup [as _super] (Path:\To\Project\node_modules\feathers-authentication-oauth2\lib\index.js:101:27)
[2]     at Function.setup (Path:\To\Project\node_modules\@feathersjs\socketio\lib\index.js:90:30)
[2]     at Function.setup (Path:\To\Project\node_modules\uberproto\lib\proto.js:30:17)
[2]     at Function.listen (Path:\To\Project\node_modules\@feathersjs\express\lib\index.js:59:12)
[2]     at Function.<anonymous> (Path:\To\Project\node_modules\uberproto\lib\proto.js:30:17)
[2]     at Function.listen (Path:\To\Project\node_modules\@feathersjs\socketio\lib\index.js:37:25)
[2]     at Function.listen (Path:\To\Project\node_modules\uberproto\lib\proto.js:30:17)
[2]     at Object.<anonymous> (F:/ongoing/november/api/api.js:75:7)
[2]     at Module._compile (module.js:643:30)
[2]     at loader (Path:\To\Project\node_modules\babel-register\lib\node.js:144:5)
[2]     at Object.require.extensions.(anonymous function) [as .js] (Path:\To\Project\node_modules\babel-register\lib\node.js:154:7)
[2]     at Module.load (module.js:556:32)
[2]     at tryModuleLoad (module.js:499:12)
[2]     at Module._load (module.js:491:3)

any idea please?

daffl commented 6 years ago

That's a bug in Babel which can not extend from native classes. Maybe this or this Stackoverflow answer has some pointers.

ducnvhn commented 6 years ago

thanks @daffl . I will have a look at it.