我将passport-google-oauth20
其用作我的应用程序的唯一身份验证形式。一切似乎都很好,我可以使用verify
回调护照提供的身份验证用户并将他存储在数据库中。
在这个应用程序中,我只会使用 google 对用户进行身份验证并检查他是否存在于数据库中。我不会向谷歌提出更多请求,也不会从谷歌获取更多信息。
我注意到护照的回调函数背后发生了很多神奇的事情。我对如何serializeUser
和deserializeUser
工作有一个模糊的理解,但我不确定什么时候有必要。
护照提供的回调函数在localStorage上设置了一个JWT,所以即使我设置{ session: false }
了重定向选项,我还需要序列化用户吗?
这是一些清除问题的代码
passport.use(
new GoogleStrategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: 'http://localhost:3000/auth/google/redirect'
},
(accessToken, refreshToken, profile, done) => {
// console.log('TCL: profile', profile); -> Gives profile info
// console.log('TCL: refreshToken', refreshToken); -> Null
// console.log('TCL: accessToken', accessToken);
// This attaches the user profile to the req object
done(null, profile);
})
);
以下是路线:
app.use(passport.initialize());
app.get(
'/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] })
);
app.get(
'/auth/google/redirect',
passport.authenticate('google', { session: false }),
(req, res) => {
console.log(req.user);
res.redirect('/');
}
);
我能够获得个人资料和我需要的所有信息。我还需要序列化用户吗?