Passport & JWT & Google Strategy - disconnect & res.send () session after Google callback

Usage: passport-google-oauth2 .

I want to use JWT with Google login - for this I need to disconnect the session and somehow pass the user model back to the client. All examples use google callback, which is magically redirected to '/' .

Like me:
1. Disconnect the session when using passport-google-oauth2.
2. User res.send () for the client after Google Authentication.

Feel free to suggest alternatives if I'm not in the right direction.

+9
source share
2 answers

Help overcome this with some ideas:
1. disconnect the session in express - just remove the session middleware

// app.use(session({secret: config.secret})) 

2. when using Google Authentication, what actually happens is a redirect to the google login page and, if the login is successfully completed, redirects you back with the address you provided.

This actually means that after Google calls back your callback, you wonโ€™t be able to execute res.send (token, user) - it just doesnโ€™t work (can anyone understand why?). This way you can redirect to the client by doing res.redirect("/") . But the whole goal is to pass a token so that you can also do res.redirect("/?token=" + token) .

 app.get( '/auth/google/callback', passport.authenticate('google', { //successRedirect: '/', failureRedirect: '/' , session: false }), function(req, res) { var token = AuthService.encode(req.user); res.redirect("/home?token=" + token); }); 

But how will the client get the custom object? Thus, you can also pass the user in the same way, but I did not like it (passing the entire user object to the parameter list ...). So I made the client use the token and retrieve the user.

  function handleNewToken(token) { if (!token) return; localStorageService.set('token', token); // Fetch activeUser $http.get("/api/authenticate/" + token) .then(function (result) { setActiveUser(result.data); }); } 

What does another http request mean - It makes me think that maybe I didn't get the correct token concept. Feel free to enlighten me.

+9
source

I came up with an article on how to use JWT with GoogleStrategy using passport data https://www.sitepoint.com/spa-social-login-google-facebook/

Hope it helps!

0
source

Source: https://habr.com/ru/post/1012685/


All Articles