title: Handling authentication in Nodejs with passport-facebook-token, request coming from frontend Facebook SDK
body: <p>I am working on a Unity App. For login, there are two methods, one using Email and another using Facebook. In case of login separately, I do not have any problem. Registration and Login with Email works perfectly. And Login with Facebook works perfectly as well. Here's the workflow, I created just to make you clear.
  <a href="https://i.stack.imgur.com/uGoF7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uGoF7.jpg" alt="Login work flow"></a></p>
  
  <p><strong>tl;dr</strong> [read update]</p>
  
  <p>There's another schema for <code>account</code>, which is used for login.</p>
  
  <pre><code>var Account = new Schema({
    email: String,
    password: String,
    facebookId: String
  });
  </code></pre>
  
  <p><strong>Things to know about the backend API.</strong></p>
  
  <ol>
  <li>Passport is used for Authentication</li>
  <li>Successful login returns email and token to the client through API.</li>
  <li>On client, token is most to play game and use the overall features.</li>
  </ol>
  
  <p>As I said, I have already covered the part when if a client registers and login using email, then client can use the app. But my confusion is handling the logins with Facebook. Facebook SDK is already integrated with the Unity App, and Login is success. </p>
  
  <p>Now, how can I use the Facebook login information that is generated by the Facebook SDK onto my back end, so that I can authorize the user throughout the system, as done in email login. </p>
  
  <p>Going through other questions in SO and Google, I came across <code>passport-facebook-token</code>, I also tried using the plugin but could not came up with the logic and flow for handling the data from SDK into the Nodejs API. Can someone me help understand how it is done?</p>
  
  <p><strong>Update 1: Using <code>passport-facebook-token</code></strong></p>
  
  <p><strong>Strategy on index.js</strong></p>
  
  <pre><code>passport.use(new FacebookTokenStrategy({
      clientID: FACEBOOK_APP_ID,
      clientSecret: FACEBOOK_APP_SECRET
    }, function(accessToken, refreshToken, profile, done) {
      Account.findOrCreate({facebookId: profile.id}, function (error, user) {
        return done(error, user);
      });
    }
  ));
  </code></pre>
  
  <p><strong>Controller API</strong></p>
  
  <pre><code>api.post('/auth/facebook/token',
  passport.authenticate('facebook-token'),
  function (req, res) {
    console.log(req.user);
    // do something with req.user
    res.sendStatus(req.user? 200 : 401);
  }
  );
  </code></pre>
  
  <p>Now, there is no error shown, but the data is not inserted into Account Schema, I have this findOrCreate() function in Model.</p>
  
  <pre><code>Account.statics.findOrCreate = function findOrCreate(profile, cb){
  var userObj = new this();
  this.findOne({facebookId : profile.id},function(err,result){
      if(!result){
          userObj.facebookId = profile.id;
          //....
          userObj.save(cb);
      }else{
          cb(err,result);
      }
  });
  };
  </code></pre>