title: Passportjs Google OAuth does not return profile object
body: <p>I am working on a MERN stack appliaction and I wanna integrate google authencation using PassportJs, I already configured the server-side and ran a couple of test which were successful but all of a sudden it no longers returns the usual profile object which contains the user's name, first_name, _json e.t.c, instead it sends back this object;</p>
  <pre><code>{
    access_token: 'ya29.A0ARrdaM8QpjEP_3sDyDRBT8OJiOlXVgOHFcyEV1nne13jd_qRelaTYh5ry0H0E8WvmOs14h6PycgTHqteS85U9lPxj2sfhnabOI6XdMgWAM_Z_y4WR1F50NR7MVDcjpH6aS8xLzewScSt8R-6Cs6t4Adn3Vgu',
    expires_in: 3599,
    scope: 'https://www.googleapis.com/auth/userinfo.email openid https://www.googleapis.com/auth/userinfo.profile',
    token_type: 'Bearer',
    id_token: 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImNlYzEzZGViZjRiOTY0Nzk2ODM3MzYyMDUwODI0NjZjMTQ3OTdiZDAiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiI5NDc2NDU0NDY5NTctZWhiNXN1dWduZnZkcGJzNnE5cXZjNDQ2ZWRzZTY4ZWwuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiI5NDc2NDU0NDY5NTctZWhiNXN1dWduZnZkcGJzNnE5cXZjNDQ2ZWRzZTY4ZWwuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMDA3NDM5NTc3MzIwMDkwMzE5OTAiLCJlbWFpbCI6InNpbW9uY2NvdmVuYW50QGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJhdF9oYXNoIjoiODBfN20xMVJVNHV5SHZiblc2UEZIZyIsIm5hbWUiOiJTaW1vbiBDb3ZlbmFudCIsInBpY3R1cmUiOiJodHRwczovL2xoMy5nb29nbGV1c2VyY29udGVudC5jb20vYS9BQVRYQUp4QWtNdEhtSWFjLV9CNEFzVkVYQU5jMUF1WEh5QU5KNTdFVVRVPXM5Ni1jIiwiZ2l2ZW5fbmFtZSI6IlNpbW9uIiwiZmFtaWx5X25hbWUiOiJDb3ZlbmFudCIsImxvY2FsZSI6ImVuIiwiaWF0IjoxNjQ5MTA5MDk5LCJleHAiOjE2NDkxMTI2OTl9.cRn6YA8OYSkp_MGLoDQTqeu6R5Ajm3KQMg6cQkZaBXuduJIJOnVL1r-lgCAIDHmkTsH-gohBIWcELPL0Pzm0rueW2X6b0LEzPdFNMsHFL_RkbRh2bwPwqAZqToaEJsN6M9DqqQwjuc8aENwHOxflVfTqM71aidt96cEIucRcCYxF1Q-rxBw4STNy0c2Lqae_85fFO5uArEJPyOPYDjVYjEqR0wNWFezRadA8zAKV7tv2WJFhEbA2tgnnbIKP5rWmkF6V6mlbFKv9p2qFvBLUpj6ffqVnQZmwILJng6GvNrWu03VfbAvHao4PA-qLwPnge65hqjet3S8TxzlNkkAtDA'
  }
  </code></pre>
  <p>This my passport setup:</p>
  <pre><code>const strategy = new GoogleStrategy(
    {
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: &quot;/auth/google/callback&quot;,
    },
    function (accessToken, refreshToken, profile, cb, done) {
      console.log(profile);
  
      const { email, given_name, family_name, email_verified, gender } = profile._json;
  
      User.findOne({ email }).exec((err, user) =&gt; {
        if (user) {
          const token = jwt.sign({ _id: user._id }, process.env.JWT_SECRET, {
            expiresIn: &quot;7d&quot;,
          });
          const { password, ...others } = user._doc;
  
          return cb(err, others, token);
        } else {
          let password = email + process.env.JWT_SECRET;
          user = new User({
            firstname: given_name,
            lastname: family_name,
            email,
            password: CryptoJS.AES.encrypt(
              password,
              process.env.PASS_SEC
            ).toString(),
            gender: &quot;male&quot;,
          });
  
          console.log(user);
  
          user.save((err, data) =&gt; {
            console.log(data);
            if (err) {
              console.log(&quot;Save error&quot;, errorHandler(err));
              return done(null, false, { message: errorHandler(err) });
            } else {
              const token = jwt.sign({ _id: data._id }, process.env.JWT_SECRET, {
                expiresIn: &quot;7d&quot;,
              });
  
              const { password, ...others } = data._doc;
              console.log(others);
              return cb(err, token, others);
            }
          });
        }
      });
    }
  );
  
  passport.use(strategy);
  
  passport.serializeUser((user, done) =&gt; {
    done(null, user);
  });
  
  passport.deserializeUser((user, done) =&gt; {
    done(null, user);
  });
  </code></pre>
  <p>And this is my routes :</p>
  <pre><code>router.get(
    &quot;/google&quot;,
    passport.authenticate(&quot;google&quot;, {
      scope: [&quot;profile&quot;, &quot;email&quot;],
    })
  );
  
  router.get(
    &quot;/google/callback&quot;,
    passport.authenticate(&quot;google&quot;, {
      successRedirect: &quot;/login/success&quot;,
      failureRedirect: &quot;/login/failed&quot;,
    })
  );
  </code></pre>
  <p>I thought maybe it had something to do with the accessToken so I tried using the &quot;passport-oauth2-refresh&quot; dependency to refresh and get a new accessToken;</p>
  <pre><code>const refresh = require(&quot;passport-oauth2-refresh&quot;);
  refresh.use(strategy);
  </code></pre>
  <p>But that didnt work, I have tried searching stack-overflow for similar issues but found none. The strange thing was that it was working before, cant seem to figure out what went wrong.</p>
  <p>I also wanna ask, after successful authentication I want to be able to send the user to my frontend and store it in localStorage but I found out that <a href="https://stackoverflow.com/questions/46818363/how-to-handle-cors-in-a-service-account-call-to-google-oauth/46818486">Google or Oauth don't support CORS</a>. So I cannot make axios request nor catch server response, so what would be the optimal way to handle the authentication on the frontend?</p>
  <p>My frontend google login code:</p>
  <pre><code>const google = () =&gt; {
      window.open(`${process.env.REACT_APP_API_URL}/auth/google`, &quot;_self&quot;);
  }
  </code></pre>