<commit_before>var express = require('express');
var router = express.Router();
var User  = require('./../app/models/user');
var oauth = require('./../oauth');
var passport = require('passport');
var request = require('request');
var jwt = require('jwt-simple');
var moment = require('moment');
/**
 * handleAuth creates a session object, which we then store the username as a user
 * property under the req.session object
 */
function handleAuth(req, res, username, id) {
  req.session.regenerate(function() {
    req.session.user = username;
    req.session.user_id = id.toString();
    console.log("SESSION!!! " + req.session.user + "ID!!! " + req.session.user_id);
    res.end();
  });
};

function createJWT(user) {
  var payload = {
    sub: user._id,
    iat: moment().unix(),
    exp: moment().add(14, 'days').unix()
  };
  return jwt.encode(payload, oauth.ids.local.secret);
};

router.post('/facebook', function(req, res){
  console.log("got a facebook post request!");
  var accessTokenUrl = 'https://graph.facebook.com/v2.3/oauth/access_token';
  var graphApiUrl = 'https://graph.facebook.com/v2.3/me';
  var params = {
    code: req.body.code,
    client_id: req.body.clientId,
    client_secret: oauth.ids.facebook.clientSecret,
    redirect_uri: req.body.redirectUri
  };
  console.log("requesting token...");
  request.get({ url: accessTokenUrl, qs: params, json: true }, function(err, response, accessToken) {
    if (response.statusCode !== 200) {
      return res.status(500).send({ message: accessToken.error.message });
    }
    console.log("requesting user data...");
    request.get({ url: graphApiUrl, qs: accessToken, json: true }, function(err, response, profile) {
      if (response.statusCode !== 200) {
        return res.status(500).send({ message: profile.error.message });
      }
      if (req.headers.authorization) {
        new User({ facebookId: profile.id }).fetch()
        .then(function(model){
          if(!model){
            var user = new User({
              username: profile.name,
              email: profile.email,
              firstName: profile.first_name,
              lastName: profile.last_name,
              facebookId: profile.id,
              facebookToken: accessToken
            });
            user.save().then(function() {
              var token = createJWT(user);
              console.log("auth exists, user created. responding...");
              res.send({ token: token, user: user.username });
            });
          }
        });
      } else {
        new User({ facebookId: profile.id }).fetch()
        .then(function(model){
          if(model) {
            var token = createJWT(model);
            console.log("Not authorized, but model exists. responding...");
            return res.send({ token: token, user: model.attributes.username });
          }
          var user = new User({
            username: profile.name,
            email: profile.email,
            firstName: profile.first_name,
            lastName: profile.last_name,
            facebookId: profile.id,
            facebookToken: accessToken
          });
          user.save().then(function() {
            var token = createJWT(user);
            console.log("Not authorized, user created. responding...");
            res.send({ token: token, user: user.username });
          });
        });
      }
    });
  });
});

// Google OAuth Initiation
router.get('/google', passport.authenticate('google', { scope: 'https://www.googleapis.com/auth/plus.login' }), function(req, res){
});

// Google OAuth Callback
router.get('/google/callback', passport.authenticate('google', { failureRedirect: '#/signup' }), function(req, res) {
 res.redirect('#/signin');
});

// Local Auth Sign-in
router.post('/local', passport.authenticate('local', { failureRedirect: '#/signup' }), function(req, res) {
  var username = req.body.username;

  new User({username: username})
  .fetch()<commit_msg>Implemented sessions with FB OAuth<commit_after>var express = require('express');
var router = express.Router();
var User  = require('./../app/models/user');
var oauth = require('./../oauth');
var passport = require('passport');
var request = require('request');
var jwt = require('jwt-simple');
var moment = require('moment');
/**
 * handleAuth creates a session object, which we then store the username as a user
 * property under the req.session object
 */
function handleAuth(req, res, username, id) {
  req.session.regenerate(function() {
    req.session.user = username;
    req.session.user_id = id.toString();
    console.log("SESSION!!! " + req.session.user + "ID!!! " + req.session.user_id);
    res.end();
  });
};

function handleFBAuth(req, res, token, username, id) {
  req.session.regenerate(function() {
    req.session.user = username;
    req.session.user_id = id.toString();
    console.log("SESSION!!! " + req.session.user + "ID!!! " + req.session.user_id);
    res.send({ token: token, user: username });
    res.end();
  });
};

function createJWT(user) {
  var payload = {
    sub: user._id,
    iat: moment().unix(),
    exp: moment().add(14, 'days').unix()
  };
  return jwt.encode(payload, oauth.ids.local.secret);
};

router.post('/facebook', function(req, res){
  console.log("got a facebook post request!");
  var accessTokenUrl = 'https://graph.facebook.com/v2.3/oauth/access_token';
  var graphApiUrl = 'https://graph.facebook.com/v2.3/me';
  var params = {
    code: req.body.code,
    client_id: req.body.clientId,
    client_secret: oauth.ids.facebook.clientSecret,
    redirect_uri: req.body.redirectUri
  };
  console.log("requesting token...");
  request.get({ url: accessTokenUrl, qs: params, json: true }, function(err, response, accessToken) {
    if (response.statusCode !== 200) {
      return res.status(500).send({ message: accessToken.error.message });
    }
    console.log("requesting user data...");
    request.get({ url: graphApiUrl, qs: accessToken, json: true }, function(err, response, profile) {
      if (response.statusCode !== 200) {
        return res.status(500).send({ message: profile.error.message });
      }
      if (req.headers.authorization) {
        new User({ facebookId: profile.id }).fetch()
        .then(function(model){
          if(!model){
            var user = new User({
              username: profile.name,
              email: profile.email,
              firstName: profile.first_name,
              lastName: profile.last_name,
              facebookId: profile.id,
              facebookToken: accessToken
            });
            user.save().then(function() {
              var token = createJWT(user);
              console.log("auth exists, user created. responding...");
              handleFBAuth(req, res, token, user.username, user.id);
            });
          }
        });
      } else {
        new User({ facebookId: profile.id }).fetch()
        .then(function(model){
          if(model) {
            var token = createJWT(model);
            console.log("Not authorized, but model exists. responding...");
            return handleFBAuth(req, res, token, model.attributes.username, model.attributes.id);
          }
          var user = new User({
            username: profile.name,
            email: profile.email,
            firstName: profile.first_name,
            lastName: profile.last_name,
            facebookId: profile.id,
            facebookToken: accessToken
          });
          user.save().then(function() {
            var token = createJWT(user);
            console.log("Not authorized, user created. responding...");
            handleFBAuth(req, res, token, model.attributes.username, model.attributes.id);
          });
        });
      }
    });
  });
});

// Google OAuth Initiation
router.get('/google', passport.authenticate('google', { scope: 'https://www.googleapis.com/auth/plus.login' }), function(req, res){
});

// Google OAuth Callback
router.get('/google/callback', passport.authenticate('google', { failureRedirect: '#/signup' }), function(req, res) {
 res.redirect('#/signin');
});

// Local Auth Sign-in
router.post('/local', passport.authenticate('local', { failureRedirect: '#/signup' }), function(req, res) {
  var username = req.body.username;

  new User({username: username})
  .fetch()
{'fraction_non_alphanumeric': 0.15916495716181972, 'fraction_numerical': 0.004344153493423434, 'mean_word_length': 2.630310994305738, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2587948', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2386, 'n_words': 548}