我在应用程序中启用cookie,然后基本上告诉
passport
在我的身份验证中使用Cookie。
为了测试这一点,我在应用程序中添加了一个新的路由处理程序,其唯一目的是检查
req.user
所有物
这是我的服务/护照。js文件:
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() is a generic register to make Passport
// aware of new strategy
// creates a new instance to authenticate users
passport.use(
new GoogleStrategy(
{
clientID: keys.googleClientID,
clientSecret: keys.googleClientSecret,
callbackURL: '/auth/google/callback'
},
(accessToken, refreshToken, profile, done) => {
User.findOne({ googleId: profile.id }).then(existingUser => {
if (existingUser) {
// we already have a record with given profile id
} else {
// we dont have a user record with this id, make a new record
done(null, existingUser);
new User({ googleId: profile.id })
.save()
.then(user => done(null, user));
}
});
}
)
);
数据传递给
护照
这就引出了
id
超出cookie数据。这个
身份证件
然后传递给我的
deserializeUser
我正在使用的函数
身份证件
将其转换为用户模型实例,然后整个目标是从
反序列化用户
作为添加到请求对象
需求。使用者
因此,上面提到的新路由处理程序有一项检查工作
需求。使用者
。
因此,在我的路线/授权路线中。js公司:
const passport = require('passport');
module.exports = app => {
app.get(
'/auth/google',
passport.authenticate('google', {
scope: ['profile', 'email']
})
);
app.get('/auth/google/callback', passport.authenticate('google'));
app.get('/api/current_user', (req, res) => {
res.send(req.user);
});
};
因此,这是为了测试理论上已经通过OAuth流的人现在可以重新登录,我们可以验证这是同一个用户。
因此,预期的行为是,我将通过访问
localhost:5000/auth/google
然后在中打开单独的浏览器
localhost:5000/api/current_user
并且能够看到该用户的MongoDB记录
json
在浏览器中,我得到了一个空白页面,命令行终端或其他任何地方都没有错误。
这里会发生什么事?