我学习了一门课程,它使用护照、passport-google-oauth20、cookie-session 实现了用户身份验证,一切正常(登录、注销、会话处理)但是当我发送登录/注册请求时它不会问/提示 google 身份验证窗口输入凭据,它始终使用相同的帐户登录。
这是护照策略配置:
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');
const User = mongoose.model('users');
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id).then(user => {
done(null, user);
});
});
passport.use(
new GoogleStrategy(
{
clientID: keys.googleClientID,
clientSecret: keys.googleClientSecret,
callbackURL: '/auth/google/callback',
proxy: true,
authorizationParams: {
access_type: 'offline',
approval_prompt: 'force'
}
},
async (accessToken, refreshToken, profile, done) => {
const existingUser = await User.findOne({ googleID: profile.id })
if (existingUser) {
// we already have a record with the given profile ID
return done(null, existingUser);
}
// we don't have a user record with this ID, make a new record!
const user = await new User({ googleID: profile.id, name: profile.displayName }).save()
done(null, user);
})
);