要求的事情.使用google-passport-oauth登录后,用户未定义
req.user is undefined after logging in with google-passport-oauth
我使用Google账号成功登录后,在回调请求中,req.user
被设置为正确的用户,req.isAuthenticated()
为true。太棒了!
然而,任何后续请求都将req.user
设置为未定义。不管我做什么。
passport-google-auth2和passport-google-auth都出现了这个问题。
这是我的index.js
看起来像:
app.set('views', './src/express/views');
app.set('view engine', 'jsx');
app.engine('jsx', expressReactViews.createEngine({ beautify: true }));
app.use(express.static('./dist'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use( bodyParser.urlencoded({
extended: true
}));
app.use(session({
secret: 'keyboard cat',
proxy: true,
resave: true,
saveUninitialized: true,
cookie: { secure: true }
}));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
// user is passed here correctly
done(null, user.id);
});
passport.deserializeUser(function(userId, done) {
db.connect(function(error, connection) {
if(error) {
done(error);
}
else {
users.find(connection, userId, function (user) {
connection.close();
// user is populated here, don't worry :)
done(null, user);
});
}
});
});
passport.use(googleStrategy);
googleStrategy
是这样的:
module.exports = new GoogleStrategy(
{
clientID: '[FAKE]',
clientSecret: '[FAKE]',
callbackURL: 'http://localhost:3000/auth/google/callback'
},
function(accessToken, refreshToken, profile, done) {
db.connect((error, connection) => {
if(error) {
done(error);
}
else {
users.findOrCreateFromGoogleProfile(connection, profile, (error, user) => {
connection.close();
done(error, user);
});
}
});
}
);
这是我的auth
路由器的样子:
router.route('/google/callback').get(passport.authenticate('google', {
failureRedirect: '/error'
}), function(req, res) {
// here everything works. req.user is correctly set and req.isAuthenticated() is true
res.redirect('/index');
});
router.route('/google').get(passport.authenticate('google', {
scope: ['https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email']
}));
module.exports = router;
我做错了什么吗?
我希望我能评论一下而不是直接回答这个问题。
你能试着运行这个吗?
router.route('/google/callback').get(passport.authenticate('google', {
failureRedirect: '/error'
}), function(req, res) {
// here everything works. req.user is correctly set and req.isAuthenticated() is true
req.session.save(function(err) {
res.redirect('/index');
});
});
由于在重定向之后一切似乎都工作,这可能会解决这个问题。
我猜用户信息没有被保存到数据库。
如果您转到以下链接并从已批准连接的应用/网站列表中删除您的应用/网站,那么您是否可以再次登录?
https://myaccount.google.com/security connectedapps
相关文章:
- 需要提取当前登录用户的用户名以在字段中输入
- 如何获取登录用户的twitter oAuth令牌
- 如何向登录用户隐藏选择加入脚本
- 如何使用javascript确定登录用户的浏览器版本和IP地址
- 获取登录用户的电子邮件(Firebase安全规则)
- Facebook js-sdk可以't检索已登录用户的名称
- Sencha Touch 2-登录用户状态
- 在 SharePoint 中获取登录用户的用户名并立即使用它
- jQuery / bubbling - 如何为非登录用户停止我的 JavaScript 代码
- 如何在 Meteor 的服务器上创建用户帐户后自动登录用户
- 使用javascript基于登录用户重定向URL链接
- FB的替代内容,例如非登录用户的框
- 插座.IO - 同一登录用户在两个不同浏览器中使用 socket.id
- 骨干.js如何跟踪登录用户状态和有关应用程序结构的一般建议
- 执行流星重置时,登录用户的应用程序页面冻结
- 像Yelp和urbandictionary这样的网站如何防止非登录用户的双重投票
- 主干.js具有用于创建用户和登录用户的验证的模型
- 网站似乎在没有向服务器发送密码的情况下登录用户;这是怎么回事
- Express js会话——仅为登录用户创建
- 如何使用phonegap+quickblox注册/登录用户