简体   繁体   English

googleapis oauth2.0 + 用户注册通行证 - nodejs express 应用

[英]googleapis oauth2.0 + passport for user registration - nodejs express application

I am using googleapis oauth2.0 for user registration on my website through through OAuth2.0 .我正在使用googleapis oauth2.0 通过OAuth2.0在我的网站上进行用户注册。 I get to the point where I get the access token and id token and save the new user in my database, from that point on I want to create a user session and I am trying to use passport for it but passport needs one of its strategies to implement which I do not need at this point because I already have verified user email and everything and have saved the user in my database all I now need to do is create a user session and send the user to the home page but there seems to be no way to just create user session without a strategy in passport .我到了获取访问令牌和 id 令牌并将新用户保存在我的数据库中的地步,从那时起我想创建一个用户会话,我正在尝试使用passport ,但passport需要其策略之一实现我此时不需要的,因为我已经验证了用户电子邮件和所有内容并将用户保存在我的数据库中,我现在需要做的就是创建一个用户会话并将用户发送到主页,但似乎有没有办法没有一项战略,只是创建用户会话passport

here is my oauth2.0 redirect function where I get the access token etc.这是我的 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 ????
    
})

found the way to do it, there is req.login() function exposed by passport that you can use to manually log the user in.找到了这样做的方法,通行证公开了 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');
    });
    
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM