【问题标题】:Google Api redirection with passport带护照的 Google Api 重定向
【发布时间】:2019-01-26 02:43:42
【问题描述】:

我的应用中有一个 google auth API,我可以通过重定向功能以某种方式附加用户数据吗?或者我如何查看用户是否登录了我的反应应用程序。 例如这里我在我的请求字段中发送用户名,但我不知道如何访问它...

router.get('/google/redirect', passport.authenticate('google'), (req, res) => {
     res.redirect('http://localhost:3100/login1/' + req.user.username);
});

提前致谢!

【问题讨论】:

    标签: node.js reactjs api passport.js


    【解决方案1】:

    您必须像这样请求身份验证流程:

    router.get('/auth/login',
       (req, res, next) => {
    
            // Save the url of the user's current page so the app can redirect back to it after authorization
            if (req.query.return) {req.session.oauth2return = req.query.return;}
            next();
        },
    
        // Start OAuth 2 flow using Passport.js
        passport.authenticate('google', { scope: ['email', 'profile'] })
    );
    

    然后像这样处理回发:

    // OAuth 2 callback url.
    // Use this url to configure your OAuth client in the Google Developers console.
    router.get('/auth/google/callback',
    
        // Finish OAuth 2 flow using Passport.js
        passport.authenticate('google'), (req, res) => {
            // Redirect back to the original page, if any
            const redirect = req.session.oauth2return || '/';
            delete req.session.oauth2return;
            res.redirect(redirect);
        }
    );
    

    虽然您的localhost 没有任何可路由的 IP,因此您将永远不会收到任何回发。需要在线托管脚本(具有可公开路由的 IP)才能使其“按预期”工作。

    【讨论】:

      猜你喜欢
      • 2014-08-27
      • 1970-01-01
      • 2014-06-12
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 2017-09-27
      • 1970-01-01
      相关资源
      最近更新 更多