title: Rest API to connect (authorize) google for logged in user
body: <p>I'm working in an application which uses a REST api using the MEAN stack and <a href="http://passportjs.org" rel="nofollow noreferrer">Passport JS</a> to manage the authentication.</p>
  
  <p>The authentication, we use <a href="http://jwt.io" rel="nofollow noreferrer">JTW</a> tokens for the communication between the backend and frontend. The token is generated based on local username and passwords.</p>
  
  <p>Now I want to 'add' (authorize) the user's google account to the profile to use with google calendar API. (using this-> <a href="https://github.com/wanasit/google-calendar" rel="nofollow noreferrer">https://github.com/wanasit/google-calendar</a>)</p>
  
  <p>I've already have managed to send the user to the google authorization page, and get the token back from it. The problem is that when the user gets redirected to the page, it looses the JWT token where I check the user for the request.</p>
  
  <p>Is there any other way to get the current logged in user, or to pass some custom callback authorization header/param when calling the authorize method?</p>
  
  <p><em>auth.js:</em></p>
  
  <pre><code>var googleParams = {
          clientID: config.auth.google.clientID,
          clientSecret: config.auth.google.clientSecret,
          callbackURL: config.auth.google.callbackURL
      }
      var googleStrategy = new GoogleStrategy(googleParams, function (token, refreshToken, profile, done) {
          profile.token = token;
          return done(null, profile);
      });
  </code></pre>
  
  <p><em>routes:</em></p>
  
  <pre><code>rotas.get(
      '/google',
      auth.authenticate(), // will check the current user
      auth.isLoggedIn, // make sure the user is really logged in
      auth.authorize('google', { scope: googleScope, passReqToCallback: true }) // redirects to google to get the token
  );
  
  rotas.get('/callback/google',
      auth.authorize('google', { scope: googleScope, passReqToCallback: true })
      auth.authRedirect()
  );
  </code></pre>
  
  <p>the <code>auth.authRedirect()</code> function above is the closest solution I've found. It's a Express middleware wich redirects the user to a known route in the frontend where the user IS authenticated... but then I would not be able to fetch all his Google profile and information i need...</p>