微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Passport JS Google oauth20 回调缺少请求对象

如何解决Passport JS Google oauth20 回调缺少请求对象

我看过一些类似的问题 here 但答案与我的无关,因为我已经声明了 passReqToCallback。

我正在尝试使用护照创建身份验证系统。我已经成功地集成了本地护照,它在用户注册/登录并创建了一个会话,但在谷歌策略的逻辑中遇到了问题:

passport.use(new GoogleStrategy(
{
    clientID: process.env.GOOGLE_CLIENT_ID,clientSecret: process.env.GOOGLE_CLIENT_SECRET,callbackURL: process.env.GOOGLE_CALLBACK_URL,passReqToCallback: true
},(req,accesstoken,refreshToken,profile,done) => {
/**
 * 
 * This if statement is failing,as far as I can tell there is no req object being passed despite declaring it above
 * 
 */
// If user is logged in,proceed to simply link account
if (req.user) {
    req.user.googleid = profile.id;
    req.user.googleEmail = profile.emails[0].value;
    req.user.googledisplayName = profile.displayname;

    pool.query('UPDATE users SET googleid = ?,google_email = ?,google_display_name = ? WHERE id = ?',[
        req.user.googleid,req.user.googleEmail,req.user.googledisplayName,req.id
    ],function(err,rows) {
        // If google account is duplicate (linked to different account) will return error
        if (err) {
            return done(err,false,{message: "The Google account you tried to link is associated with another account."});
        }
        return done(null,req.user);
    })
}

// Check if google account is registered
pool.query('SELECT * FROM users WHERE googleid = ?',[profile.id],rows) {
    if (err) {
        return done(err);
    }

    // If not logged in but user already registered,log in
    if (rows.length) {
        return done(null,rows[0]);
    }

    // If no existing record,register the user.
    else {
        let newUser = {
            email: profile.emails[0].value,// Google account specific fields
            googleid: profile.id,googledisplayName: profile.displayName,method: "gl",// This field ties this new user to the google account
            // General fields (taken from the stuff google gives us)
            firstName: profile.name.givenname,lastName: profile.name.familyName,googleEmail: profile.emails[0].value
        }

        let insertQuery = "INSERT INTO users (email,googleid,google_display_name,method,first_name,last_name,google_email) VALUES (?,?,?)";
        pool.query(insertQuery,[
            newUser.email,newUser.googleid,newUser.googledisplayName,newUser.method,newUser.firstName,newUser.lastName,newUser.googleEmail
        ],rows) {
            if (err) return done(err,null);

            newUser.id = rows.insertId;
            return done(null,newUser);
        })
    }
})}));

所以基本上第一个 if 应该查看用户是否已经通过身份验证,然后就这样链接帐户。但是,在实践中,即使经过身份验证,它也会跳过这一点并继续执行其余的逻辑,这绝对可以正常工作。它将继续在 else 语句中创建一个新帐户(如果收到电子邮件则返回错误,我仍然需要实现该部分)。

但是,有趣的是,如果我在使用它时登录,它不会像其他情况那样以新用户的身份登录我,而是让我以当前会话的身份登录

我哪里出错了?为什么没有检测到 req.user?我也试过使用 req.isAuthenticated() 得到相同的结果。

以下是我的回调路线,如果有帮助的话:

// Google
router.get('/oauth/google',passport.authenticate('google',{
    scope: ['email','profile']
}));
// Callback
router.get('/oauth/google/redirect',{
    successRedirect: '/account',failureFlash: 'Something went wrong. Please enable third party cookies to allow Google to sign in.',failureRedirect: '/login'
    }
));

更新 1:如果我尝试 (!req.user),同样的结果,跳到下面,不确定这意味着什么

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?