-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Linking multiple logins #81
Comments
You can achieve this by using calling Both methods use the same strategies, but This is how I achieved it in my express + passport app with instagram and facebook strategies. My configuration is split is 3 different modules, but it's think is clear enough to follow. I used CouchDB and not MongoDB, so I skipped the whole db logic. Basically you just need two DB functions - both called from the passportConf.js var callback = exports.callback = function(req, accessToken, refreshToken, profile, done ){
if( !req.user ){
/* Check if user exists - if not, creates a new account in DB. Return user account to the callback */
DB.getOrCreateUser(accessToken, refreshToken, profile, done);
}else{
/* Link the login to the existing account in the DB, return full, updated user account to the callback */
DB.linkProfile(req.user, accessToken, refreshToken, profile, done)
}
};
exports.instagramStrategy = new InstagramStrategy({
clientID: '-----', clientSecret: '-----',
callbackURL: "/connect/instagram/callback",
passReqToCallback : true
}, callback);
exports.facebookStrategy = new FacebookStrategy({
clientID: '-----', clientSecret: '----',
callbackURL: "/connect/facebook/callback",
passReqToCallback : true
}, callback);
exports.serialize = function(account, done) {
done(null, account.id );
};
exports.deserialize = function(id, done) {
DB.getUserById( id, done ); /* should return full user account to the callback */
}; app.js passport.use( passportConfig.facebookStrategy );
passport.use( passportConfig.instagramStrategy );
passport.serializeUser( passportConfig.serialize );
passport.deserializeUser( passportConfig.deserialize );
app.get('/connect/:method', routes.login );
app.get('/connect/:method/callback', routes.loginCallback);
(...) routes.js module.exports = {
login: function(req, res, next) {
if(req.user){
/* This will call passport.authenticate('facebook') if the route was /connect/facebook
and passport.authenticate('instagram') if the route was /connect/instagram */
passport.authenticate(req.params.method)( req, res, next );
}else{
passport.authorize(req.params.method)( req, res, next );
}
},
loginCallback: function(req, res, next){
passport.authenticate(req.params.method, {
successRedirect: '/account',
failureRedirect: '/'
})(req, res, next);
}
} |
@lukaszfiszer Thanks for jumping in and providing some help! |
I'd opened an issue earlier trying to link multiple oauth accounts together. I am having challenges implementing it from the PassportJS guid:
Thanks
DK
The text was updated successfully, but these errors were encountered: