feathersjsfeathers-authentication

How do you add your own Oauth2 Passport strategy to a FeathersJS app?


Apologies in advance for what might be an obvious question/answer, but I keep scouring the docs and cannot find it.

I know FeathersJS has drop-in strategies for Facebook/Twitter/Github -- I see those in the docs. And I know you can do all sorts of customization authorization strategies. All I am looking to do is authenticate users through a Oauth2 provider that doesnt already have a pre-packaged strategy. I cannot find a working example that does this.

More frustratingly, when I try to follow examples/docs, I get errors coming from the feathersjs npm modules, like:

    <...>/node_modules/@feathersjs/authentication-oauth2/lib/index.js:96
      app.passport.use(name, new Strategy(oauth2Settings, verifier.verify.bind(verifier)));
                             ^
TypeError: Strategy is not a constructor

Does anyone have a working example?


Solution

  • That error means that you didn't pass a Passport oAuth2 strategy. You can set up the general Passport oAuth2 adapter very similar to the example in the documentation:

    const oauth2 = require('@feathersjs/authentication-oauth2');
    const OAuth2Strategy = require('passport-oauth2').Strategy;
    
    app.configure(oauth2({
      name: 'custom',
      Strategy: OAuth2Strategy,
      authorizationURL: 'https://www.example.com/oauth2/authorize',
      tokenURL: 'https://www.example.com/oauth2/token',
      clientID: EXAMPLE_CLIENT_ID,
      clientSecret: EXAMPLE_CLIENT_SECRET,
      callbackURL: "http://localhost:3000/auth/example/callback"
    }));