1

I have incoming connections from two different clients (angular client and a node.js feathers client) and I wish them to use two different authentication endpoints (based on data in two separate tables). One should authentication against the /users service and the others against /users2 service.

How can this be achieved?

This is how it works with one authentication end-point:

// default.json
"authentication": {
    "secret": "<secret>",
    "strategies": [
      "jwt",
      "local"
    ],
    "path": "/authentication",
    "service": "users",
    "jwt": {
      "header": {
        "typ": "access"
      },
      "audience": "https://yourdomain.com",
      "subject": "anonymous",
      "issuer": "feathers",
      "algorithm": "HS256",
      "expiresIn": "1d"
    },
    "local": {
      "entity": "user",
      "usernameField": "email",
      "passwordField": "password"
    }
  }

// authentication.js
const authentication = require('@feathersjs/authentication');
const jwt = require('@feathersjs/authentication-jwt');
const local = require('@feathersjs/authentication-local');

module.exports = function (app) {
  const config = app.get('authentication');

  app.configure(authentication(config));
  app.configure(jwt());
  app.configure(local());

  app.service('authentication').hooks({
    before: {
      create: [
        authentication.hooks.authenticate(config.strategies),
      ],
      remove: [
        authentication.hooks.authenticate('jwt')
      ]
    }
  });

};

Thanks!

robtot
  • 863
  • 1
  • 9
  • 31

1 Answers1

5

Not sure if it was possible in the past, but with current FeathersJS (4.5.0) you can create multiple AuthenticationService instances with different configurations:

//default.json
"authentication": {
  "entity": "user",
  "service": "users",
  "secret": ** ** ** ** ** * ,
  "authStrategies": [
    "jwt",
    "local"
  ],
  ...
},
"authentication": {
  "entity": "user2",
  "service": "users2",
  "secret": ** ** ** ** ** * ,
  "authStrategies": [
    "jwt",
    "local"
  ],
  ...
},
...


// authentication.ts
...
export default function(app: Application) {
  const authentication = new AuthenticationService(app, 'authentication');
  authentication.register('jwt', new JWTStrategy());
  authentication.register('local', new LocalStrategy());
  app.use('/authentication/users', authentication2);

  const authentication2 = new AuthenticationService(app, 'authentication2');
  authentication2.register('jwt', new JWTStrategy());
  authentication2.register('local', new LocalStrategy());
  app.use('/authentication/users2', authentication2);

  app.configure(expressOauth());
}
...

// user.hooks.ts / user2.hooks.ts
import * as feathersAuthentication from '@feathersjs/authentication';
import * as local from '@feathersjs/authentication-local';
// Don't remove this comment. It's needed to format import lines nicely.

const {
  authenticate
} = feathersAuthentication.hooks;
const {
  hashPassword,
  protect
} = local.hooks;

export default {
  before: {
    all: [],
    find: [authenticate('jwt')],
    get: [authenticate('jwt')],
    create: [hashPassword('password')],
    update: [hashPassword('password'), authenticate('jwt')],
    patch: [hashPassword('password'), authenticate('jwt')],
    remove: [authenticate('jwt')]
  },

  after: {
    all: [
      // Make sure the password field is never sent to the client
      // Always must be the last hook
      protect('password')
    ],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

  error: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  }
};
Run_Script
  • 2,487
  • 2
  • 15
  • 30
Jakub Rak
  • 111
  • 1
  • 4