【发布时间】:2016-08-01 16:50:53
【问题描述】:
我正在使用 passport.js 谷歌策略来验证和登录应用程序。登录尝试工作正常,但用户详细信息未存储在数据库中。我没有在 UserSchema 模型中提及任何内容。我尝试如下。
api.js
passport.use(new GoogleStrategy({
clientID : config.googleAuth.clientID,
clientSecret : config.googleAuth.clientSecret,
callbackURL : config.googleAuth.callbackURL
passReqToCallback : true
},
function(token, refreshToken, profile, done) {
process.nextTick(function() {
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 = token;
newUser.google.name = profile.displayName;
newUser.google.email = profile.emails[0].value; // pull the first email
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
}));
router.get('/pages/auth/loginWithGoogle', passport.authenticate('google', { scope : ['profile', 'email'] }));
router.get('/auth/google/callback', passport.authenticate('google', {
successRedirect : '/invite-friends',
failureRedirect : '/pages/auth/login'
}));
【问题讨论】:
标签: angularjs node.js mongodb express passport.js