Node.js 带passport的Google Api重定向

Node.js 带passport的Google Api重定向,node.js,reactjs,api,passport.js,Node.js,Reactjs,Api,Passport.js,我的应用程序中有一个google auth API,我可以用重定向功能附加用户数据吗?或者如何查看用户是否登录了我的react应用程序。 例如,这里我在我的请求字段中发送用户名,但我不知道如何访问它 router.get('/google/redirect', passport.authenticate('google'), (req, res) => { res.redirect('http://localhost:3100/login1/' + req.user.userna

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

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

提前谢谢

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

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);
    }
);
然后像这样处理邮件:

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)才能使其“按预期”工作