title: Facebook-passport with JWT
body: <p>I've been using <strong>Passport</strong> on my server for user authentication. 
  When a user is signing in locally (using a username and password), the server sends them a <strong>JWT</strong> which is stored in localstorage, and is sent back to server for every api call that requires user authentication.</p>
  
  <p>Now I want to support <strong>Facebook and Google login</strong> as well. Since I began with Passport I thought it would be best to continue with Passport strategies, using <strong>passport-facebook</strong> and <strong>passport-google-oauth</strong>.</p>
  
  <p>I'll refer to Facebook, but both strategies behave the same. They both require redirection to a server route (<strong>'/auth/facebook'</strong> and <strong>'/auth/facebook/callback'</strong> for that matter). 
  The process is successful to the point of saving users including their facebook\google ids and tokens on the DB.</p>
  
  <p>When the user is created on the server, a JWT is created (without any reliance on the token received from facebook\google).      </p>
  
  <pre><code>     ... // Passport facebook startegy
       var newUser = new User();
       newUser.facebook = {};
       newUser.facebook.id = profile.id; 
       newUser.facebook.token = token; // token received from facebook
       newUser.facebook.name  = profile.displayName;   
       newUser.save(function(err) {
            if (err)
                 throw err;
            // if successful, return the new user
            newUser.jwtoken = newUser.generateJwt(); // JWT CREATION!
            return done(null, newUser);
       });
  </code></pre>
  
  <p>The problem is that after its creation, <strong>I don't find a proper way to send the JWT to the client</strong>, since I should also redirect to my app.</p>
  
  <pre><code>app.get('/auth/facebook/callback',
      passport.authenticate('facebook', {
          session: false,
          successRedirect : '/',
          failureRedirect : '/'
      }), (req, res) =&gt; {
          var token = req.user.jwtoken;
          res.json({token: token});
      });
  </code></pre>
  
  <p>The code above <strong>redirects me to my app main page, but I don't get the token</strong>.
  If I remove the successRedirect, <strong>I do get the token, but I'm not redirected to my app</strong>.</p>
  
  <p>Any solution for that? Is my approach wrong? Any suggestions will do. </p>