Open thanhtung6824 opened 7 years ago
When first time I tried login , it worked well , but second time , when I tried with different account , It not save in database . Here is my code
app.use(passport.initialize()); app.use(passport.session()); passport.serializeUser(function(user, done) { done(null, user.id); }); passport.deserializeUser(function(id, done) { User.findById(id, function(err, user) { done(err, user); }); }); passport.use(new FacebookStrategy({ clientID: 'xxx', clientSecret: 'xxx', callbackURL: "http://localhost:2912/auth/facebook/callback", profileFields: ['id', 'name', 'emails'] }, function(accessToken, refreshToken, profile, done) { User.findOne({'facebook.id':profile.id},function(err,user) { if(err) return done(err); if(user){ return done(null,user); }else{ var newUser = new User(); newUser.facebook.id = profile.id; newUser.facebook.token = accessToken; newUser.facebook.name = profile.name.givenName +'' + profile.name.familyName; newUser.facebook.email = profile.emails[0].value; newUser.save(function(err) { if(err) console.log(err); return done(null,newUser); }) } }); } )); app.get('/auth/facebook', passport.authenticate('facebook',{scope:['email']})); app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login' }));
Here is my model
var userSchema = new Schema({ facebook:{ id:String, token:String, email:String, name:String } });
When first time I tried login , it worked well , but second time , when I tried with different account , It not save in database . Here is my code
Here is my model