Reputation: 497
I have a google auth API in my app, can I somehow attach user data with redirect function? Or how can I see if the user logged in at my react app. For example here I'm sending username in my request field, but I don't know the way to access it...
router.get('/google/redirect', passport.authenticate('google'), (req, res) => {
res.redirect('http://localhost:3100/login1/' + req.user.username);
});
Thanks in advance!
Upvotes: 1
Views: 2003
Reputation: 76849
you have to request the authentication flow alike this:
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'] })
);
and then handle the post-back alike this:
// 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);
}
);
while your localhost
does not have any route-able IP, therefore you will never receive any post-back. hosting the script on-line (with a publicly route-able IP) is required to make it work "as expected".
Upvotes: 2