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

How to create multiple instance of Strategy? #77

Open
lamlejobchat opened this issue Feb 26, 2021 · 1 comment
Open

How to create multiple instance of Strategy? #77

lamlejobchat opened this issue Feb 26, 2021 · 1 comment

Comments

@lamlejobchat
Copy link

How to add multiple callback config with same cliendId?

I have some code:

I want to register two instance 'GoogleStrategy' with two 'callbackURL' use for web and desktop app:

passport.use(
    new GoogleStrategy( 'google-desktop',
      { clientID, clientSecret, callbackURL: 'localhost:6060/login/desktop/callback' },
      async (_, __, tokenData, profile, done) => {}
    ),
  );
passport.use(
    new GoogleStrategy( 'google-web',
      { clientID, clientSecret, callbackURL: 'localhost:6060/login/web/callback' },
      async (_, __, tokenData, profile, done) => {}
    ),
  );

With each platform will have difference url for request authenticate:

router.get('/login/web', passport.authenticate('google-web', { scope: ['profile', 'email'] }));
router.get('/login/desktop', passport.authenticate('google-desktop', { scope: ['profile', 'email'] }));

We register callback from Google with each type 'google-web' and 'google-desktop':

router.get('/login/web/callback', (req, res, next) => {
  passport.authenticate('google-web', (error, data) => {

  })(req, res, next);
});
router.get('/login/web/callback', (req, res, next) => {
  passport.authenticate('google-desktop', (error, data) => {

  })(req, res, next);
});

But this code really not working because inside Strategy constructor, 'name' alway equal 'google'.
I think I will custom 'name' is a input in Strategy constructor. Does that will destabilize the �passport and not working?

@lfa9603
Copy link

lfa9603 commented Aug 24, 2022

This is the closest solution I found: jaredhanson/passport#50 .
Seems to be working for me.

You must make sure that wherever you call authenticate or use functions you reference your custom strategy name. I encapsulated my strategies in a class with a strategy property to be able to call passport.authenticate(this.strategy, ....) and keep this nice and consistent

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