I'm use passport-google to login , but it only save one account in first time I login . When I try login with different account , it not save in database . Here is my code
router
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
passport.use(new GoogleStrategy({
clientID: 'xxx',
clientSecret: 'xxx',
callbackURL: "http://localhost:2912/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOne({'google.id':profile.id},function(err,user) {
if(err)
return done(err);
if(user){
return done(null,user);
}else{
var newUser = new User();
newUser.google.id = profile.id;
newUser.google.token = accessToken;
newUser.google.name = profile.displayName;
newUser.google.email = profile.emails[0].value;
newUser.save(function(err) {
if(err)
console.log(err);
return done(null,newUser);
})
}
});
}
));
app.get('/auth/google',
passport.authenticate('google', {scope : ['profile', 'email'] }));
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
model
var userSchema = new Schema({
google:{
id:String,
token:String,
email:String,
name:String
}
});
Where is my wrong? Please help me . Any help would be appreciated
via Thanh Tùng
No comments:
Post a Comment