简体   繁体   English

PassportJS谷歌登录错误,但成功

[英]PassportJS Google login is erroring, but succeeding

When I login with Google using PassportJS and passport-google-oauth20 , my application completes the request successfully, but Passport does not recognize this and calls an error instead. 当我使用PassportJS和passport-google-oauth20使用Google登录时,我的应用程序成功完成了请求,但Passport无法识别此问题而是调用错误。

My code looks like this: 我的代码看起来像这样:

router.get('/auth/google/callback',
  passport.authenticate('google', {failureRedirect: '/login'}),
  (req, res) => console.log(req),
  (err, req, res, next) => {
    console.log('test point 1')
    console.log(err.stack)
    return res.status(500).send(err)
  })

The output looks like this, indicating that the request did fail I believe: 输出看起来像这样,表明请求确实失败我相信:

test point 1
undefined

But when I check my browser, it returned a 500 code with the correct Google user information , such as the profile picture and ID. 但是,当我查看我的浏览器时,它返回了500代码,其中包含正确的Google用户信息 ,例如个人资料图片和ID。 Passport does not store this in the req.user variable that it should either. Passport不会将它存储在它应该的req.user变量中。 Thanks! 谢谢!

If it's useful, my passport initialization is pretty simple and clear: 如果它有用,我的护照初始化非常简单明了:

// TODO: eventually setup user account system
passport.serializeUser((user, done) => done(null, user))

passport.deserializeUser((obj, done) => done(null, obj))

// TODO: eventually use this to associate the users account
passport.use(new GoogleStrategy({
  callbackURL: 'http://localhost:8080/api/v0/auth/google/callback',
  clientID: 'number-id.apps.googleusercontent.com',
  clientSecret: 'secrets'
}, (accessToken, refreshToken, profile, cb) => {
  cb(profile)
}))

The only thing I can think of that would be different about my situation is that I'm only use ing passport on a specific set of routes. 我唯一能想到的就是我的情况不同的是,我只是在一组特定的路线上use护照。 (eg the API) (例如API)

On this line: cb(profile) I was sending an error to the callback instead of the user profile. 在这一行: cb(profile)我向回调而不是用户配置文件发送错误。

It should be this: cb(null, profile) . 它应该是这样的: cb(null, profile)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM