【问题标题】:Google node authentication not storing user details to db谷歌节点身份验证不将用户详细信息存储到数据库
【发布时间】: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


    【解决方案1】:

    尝试执行以下操作:

    ...
    newUser['google.id']    = profile.id;
    newUser['google.token'] = token;
    newUser['google.name']  = profile.displayName;
    newUser['google.email'] = profile.emails[0].value;
    ...
    

    【讨论】:

    • 它不工作。我没有让我的控制器进入 GoogleStrategy 函数。
    猜你喜欢
    • 2011-02-05
    • 2020-09-11
    • 2016-12-02
    • 1970-01-01
    • 2011-05-23
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多