Passport: PassportJS - using multiple local strategies simultaneously

Created on 18 Sep 2014  Â·  7Comments  Â·  Source: jaredhanson/passport

Hi Jared,
Please refer to description of the issue I have got, described here http://stackoverflow.com/questions/25904977/passportjs-using-multiple-local-strategies-simultaneously

How can I resolve it, using PassportJS?

Thanks in advance!

Most helpful comment

You can do something like this

passport.use('local.one', myLocalStrategy1);
passport.use('local.two', myLocalStrategy2);
passport.use('local.three', myLocalStrategy3);

...

app.get('/login/s1', passport.authenticate('local.one');
app.get('/login/s2', passport.authenticate('local.two');
app.get('/login/s3', passport.authenticate('local.three');

All 7 comments

You can do something like this

passport.use('local.one', myLocalStrategy1);
passport.use('local.two', myLocalStrategy2);
passport.use('local.three', myLocalStrategy3);

...

app.get('/login/s1', passport.authenticate('local.one');
app.get('/login/s2', passport.authenticate('local.two');
app.get('/login/s3', passport.authenticate('local.three');

OtaK, can you show how to handle serializeUser in this situation

It shouldn't change for a simple reason: your User model should not change between different local strategies.
The serializeUser / deserializeUser methods are here to link passport to your data models (user ID <=> data store relationship) whereas strategies are logic flows defining different ways to handle authentication (credentials => user ID relationship).

IMO, something is wrong in your design if you need something like that in most cases.
But if you have a specific case, say like, different natures of users that have nothing in common (for example users and guest accounts -which could btw be solved via a simple flag in your DB-), you can spawn different instances of passport and attach the custom logic to each of them.
Example here: https://github.com/jaredhanson/passport/issues/286

passport.use(Recruteur.createStrategy());
passport.serializeUser(Recruteur.serializeUser());
passport.deserializeUser(Recruteur.deserializeUser());

this is my code

passport.use('local.one', myLocalStrategy1);

i dont understand myLocalStrategy1
ReferenceError: myLocalStrategy1 is not defined
what you mean by that

@Ahmedrh77
myLocalStrategy1/myLocalStrategy2/myLocalStrategy3 are placeholders for any strategy you're using, that's all. (here, you're using Recruteur.createStrategy()).

Thank you so much! It works!!

i found another solution of my probleme i add a role field to my data
base and that solve the problem instead of using multiple session

Le lun. 11 mai 2020 à 12:35, KU notifications@github.com a écrit :

The code that worked is
app.js

`passport.use('local.user', User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

passport.use('local.company', CompanyUser.createStrategy());
passport.serializeUser(CompanyUser.serializeUser());
passport.deserializeUser(CompanyUser.deserializeUser());
`

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/jaredhanson/passport/issues/287#issuecomment-626621210,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ALZ5RODZBBISWWQIXWHVJU3RQ7IH3ANCNFSM4AUNUZWA
.

Was this page helpful?
0 / 5 - 0 ratings