<commit_before>/* 
 * This file contains all of the Passport configuration that will
 * be used through the application for Facebook authentication.
 */

var FacebookStrategy = require('passport-facebook').Strategy;
var keys = require('./keys');
var model = require('../models/index');

module.exports = function(app, passport) {

  app.use(passport.initialize());
  app.use(passport.session());

	passport.use(new FacebookStrategy({
			clientID        : keys.facebook.APP_ID,
			clientSecret    : keys.facebook.SECRET,
			callbackURL     : 'http://localhost:3000/auth/facebook/callback'
		},
		
		function(accessToken, refreshToken, profile, done) {
			process.nextTick(function() {

        model['User'].findOne({ where: { facebookId: profile.id }}, {})
        .then(function(result) {
          if (!result) {
            model['User'].create({ facebookId: profile.id, v2AccessToken: accessToken });
          }
        })

			});
		})
  );
}<commit_msg>Add sessions and more robust Facebook auth.<commit_after>/* 
 * This file contains all of the Passport configuration that will
 * be used through the application for Facebook authentication.
 */

var FacebookStrategy = require('passport-facebook').Strategy;
var keys = require('./keys');
var model = require('../models/index');

module.exports = function(app, passport) {

  app.use(passport.initialize());
  app.use(passport.session());

  // Serializes the user for the session based on their ID
  passport.serializeUser(function(user, done) {
    done(null, user.id); 
  });

  // Deserializes the user based on their ID
  passport.deserializeUser(function(id, done) {
    model.User.find({ where: { id: id }})
    .then(function(user) {
      done(null, user);
    })
    .catch(function(err) {
      done(err, null);
    });
  });

  // Setup for the Facebook authentication strategy
	passport.use(new FacebookStrategy({
			clientID        : keys.facebook.APP_ID,
			clientSecret    : keys.facebook.SECRET,
			callbackURL     : keys.facebook.CALLBACK
		},
		
		function(accessToken, refreshToken, profile, done) {

      // Performs verification asynchronously
			process.nextTick(function() {

        // Queries the database to see if the user is already stored there
        model.User.find({ where: { facebookId: profile.id }}, {})
        
        .then(function(user) {
          // If the user is not stored in the database, then store the user there
          if (user === null) {
            model.User.create({ facebookId: profile.id, v2AccessToken: accessToken })
            .then(function(user) {
              done(null, user);
            })
            .catch(function(err) {
              done(err, null);
            });
          }

          else {
            // If the user has a new access token, then update the token in the database
            if (user.v2AccessToken !== accessToken) {
              user.updateAttributes({ v2AccessToken: accessToken })
              .then(function() {
                done(null, user);
              });
            } 
            else {
              done(null, user);
            }
          }
        })

        .catch(function(err) {
          done(err, null)
        });

      });
		}
  ));
}
{'fraction_non_alphanumeric': 0.12810173697270472, 'fraction_numerical': 0.0024813895781637717, 'mean_word_length': 2.314491264131552, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2192703', 'n_tokens_mistral': 934, 'n_tokens_neox': 877, 'n_words': 228}