1
votes

I'm using Passport Google auth for authentication. I am just confused about how I can pass the user profile data to the callback URL passed to passport google strategy. enter image description here

In reference to the above picture, I have passed a callback URL to passport google strategy. I want the below user data object on this callback URL. How can I achieve this?

This is my callback path: enter image description here

And the handler for this route: enter image description here

I am getting only the code, scope params over there: enter image description here

In Short, I want to return the custom jwt token to the frontend when the OAuth successfully completes. Can someone please help me achieve this? Thanks in Advance.

1

1 Answers

3
votes

After having a lot of search over the internet, finally I found the solution for this. To achieve this, you have to use the req argument in the callback and attach the user data with it as shown in the screenshot below:

enter image description here

and then in the callback, you can get the user from req._user

enter image description here

Passport google strategy Code:

 passport.use(new GoogleStrategy({
    clientID: config.googleOAuth.clientId,
    clientSecret: config.googleOAuth.clientSecret,
    callbackURL: `${config.endpoints.apiUrl}/user/gmail/oauth`,
    passReqToCallback: true
    },
    async function (req, accessToken, refreshToken, profile, done) {
      const profileJson = profile._json;
      const userData = {
        username: profileJson.email,
        firstname: profileJson.given_name,
        lastname: profileJson.family_name,
        picture: profileJson.picture,
        authType: 'oauth'
      }
      const user = await userCtrl.createOrUpdateUser(userData);
      req._user = userData;
      return done(null, user);
    }))

Callback Code:

 const OAuthCallback = async (req, res, next) => {
// function called after successfull oauth
console.log(req._user)
 }