oauth-2.0feathersjsfeathers-authentication

@feathersjs/authentication-oauth2 not creating JWT and user


I cannot authenticate to a FeathersJS server using OAuth2 Facebook strategy because after Facebook grants access to user profile, the feathers-authentication-oauth2 plugin doesn't create the user into the DB and it also doesn't create the required JWT token to be authenticated when calling feathersclient.authenticate() in the client app.

I've tried to follow all documents I've found that explain how to do it, but as a good example I could select this one (https://blog.feathersjs.com/how-to-setup-oauth-flow-with-featherjs-522bdecb10a8) that is very well explained.

As a starting point I've taken the Feathers chat application explained at the documentation (https://docs.feathersjs.com/guides/chat/readme.html) after having it working properly, I've added tha OAuth2 part as explained in the Medium document. In the default.json file I've added the "facebook" authentication strategy:

"facebook": {
      "clientID": "MY_CLIENT_ID",
      "clientSecret": "MY_CLIENT_SECRET"
    }

In the authentication.js fileI've added the configuration of the Facebook OAuth2 authentication:

const authentication = require('@feathersjs/authentication');
const jwt = require('@feathersjs/authentication-jwt');
const oauth2 = require('@feathersjs/authentication-oauth2');
const FacebookStrategy = require('passport-facebook').Strategy;

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

  // Set up authentication with the secret
  app.configure(authentication(config));
  app.configure(jwt());
  app.configure(oauth2({
    name: 'facebook',
    Strategy: FacebookStrategy,
    callbackURL: '/',
    scope: ['public_profile', 'email'],
  }));
...

And finally, in src/app.js file I've added a new "Facebook login" button that just changes window.location to '/auth/facebook' so that the OAuth2 Facebook process can begin.

After pressing the "Facebook login", I'd expect the user to be created in the NeDB DB and a valid JWT to be stored so that the feathersclient.authenticate() call would not fail. But instead of that, the Facebook login page is properly called, and after that the browser is returned to the main page ('/'), but after that, when the main page is reloaded and the feathersclient.authenticate() is called, the server complains that there isn't any valid JWT token, so authentication fails. Also I cannot see the user created in the NeDB DB, so the supposed user and JWT creation that should be done by the feathers-authentication-oauth2 plugin is not...


Solution

  • I've finally made it work... I was wrongly configuring the Facebook authentication strategy, I've changed it to:

    app.configure(oauth2({
        name: 'facebook',
        successRedirect: '/',
        failureRedirect: '/',
        Strategy: FacebookStrategy
      }));
    

    and now it is working.