googleapis oauth2.0 + passport 用于用户注册 - nodejs express application
googleapis oauth2.0 + passport for user registration - nodejs express application
我正在使用 googleapis
oauth2.0 通过 OAuth2.0
在我的网站上进行用户注册。我到达了获取访问令牌和 ID 令牌并将新用户保存在我的数据库中的地步,从那时起我想创建一个用户会话并且我正在尝试使用 passport
但是 passport
需要它的一种策略来实现,我现在不需要,因为我已经验证了用户电子邮件和所有内容,并将用户保存在我的数据库中我现在需要做的就是创建一个用户会话并发送用户到主页,但似乎没有办法在 passport
.
中没有策略就创建用户会话
这是我的 oauth2.0 重定向函数,我在其中获取访问令牌等。
router.get('/oauth-redirect', function (req, res, next) = > {
const data = await googleAuth.getToken(req.code);
const user = getUserInfoFrom(data);
const savedUser = save(user);
//here: use passport to create a session and send the user to home page ????
})
找到方法了,护照公开了 req.login() 函数,您可以使用它来手动登录用户。
router.get('/oauth-redirect', function (req, res, next) = > {
const data = await googleAuth.getToken(req.code);
const user = getUserInfoFrom(data);
const savedUser = save(user);
//this below is what will create a session and will set connect.sid cookie
req.login(savedUser, function(err) {
if (err) {
console.log(err);
}
return res.redirect('/home');
});
})
我正在使用 googleapis
oauth2.0 通过 OAuth2.0
在我的网站上进行用户注册。我到达了获取访问令牌和 ID 令牌并将新用户保存在我的数据库中的地步,从那时起我想创建一个用户会话并且我正在尝试使用 passport
但是 passport
需要它的一种策略来实现,我现在不需要,因为我已经验证了用户电子邮件和所有内容,并将用户保存在我的数据库中我现在需要做的就是创建一个用户会话并发送用户到主页,但似乎没有办法在 passport
.
这是我的 oauth2.0 重定向函数,我在其中获取访问令牌等。
router.get('/oauth-redirect', function (req, res, next) = > {
const data = await googleAuth.getToken(req.code);
const user = getUserInfoFrom(data);
const savedUser = save(user);
//here: use passport to create a session and send the user to home page ????
})
找到方法了,护照公开了 req.login() 函数,您可以使用它来手动登录用户。
router.get('/oauth-redirect', function (req, res, next) = > {
const data = await googleAuth.getToken(req.code);
const user = getUserInfoFrom(data);
const savedUser = save(user);
//this below is what will create a session and will set connect.sid cookie
req.login(savedUser, function(err) {
if (err) {
console.log(err);
}
return res.redirect('/home');
});
})