text: 'use strict';
  
  var GitHubStrategy = require('passport-github').Strategy;
  var FacebookStrategy = require('passport-facebook').Strategy;
  var User = require('../models/users');
  var configAuth = require('./auth');
  
  module.exports = function (passport) {
  	passport.serializeUser(function (user, done) {
  		done(null, user.id);
  	});
  
  	passport.deserializeUser(function (id, done) {
  		User.findById(id, function (err, user) {
  			done(err, user);
  		});
  	});
  	passport.use(new FacebookStrategy({
  		clientID: configAuth.facebookAuth.clientID,
  		clientSecret: configAuth.facebookAuth.clientSecret,
  		callbackURL: configAuth.facebookAuth.callbackURL
  	},
  	function (token, refreshToken, profile, done) {
  		console.log(profile)
  		process.nextTick(function () {
  			User.findOne({ 'user.id': profile.id }, function (err, user) {
  				if (err) {
  					return done(err)
  				}
  
  				if (user) {
  					return done(null, user)
  				} else {
  					var newUser = new User()
  					newUser.user.passport = 'facebook'
  					newUser.user.id = profile.id;
  					newUser.user.username = profile.username;
  					newUser.user.displayName = profile.displayName;
  					newUser.user.publicRepos = profile._json.public_repos;
  					newUser.nbrClicks.clicks = 0;
  
  					newUser.save(function (err) {
  						if (err) {
  							throw err;
  						}
  
  						return done(null, newUser);
  					});
  				}
  			})
  		})
  	}))
  
  	passport.use(new GitHubStrategy({
  		clientID: configAuth.githubAuth.clientID,
  		clientSecret: configAuth.githubAuth.clientSecret,
  		callbackURL: configAuth.githubAuth.callbackURL
  	},
  	function (token, refreshToken, profile, done) {
  		process.nextTick(function () {
  			User.findOne({ 'user.id': profile.id }, function (err, user) {
  				if (err) {
  					return done(err);
  				}
  
  				if (user) {
  					return done(null, user);
  				} else {
  					var newUser = new User();
  
  					newUser.user.passport = 'github'
  					newUser.user.id = profile.id;
  					newUser.user.username = profile.username;
  					newUser.user.displayName = profile.displayName;
  					newUser.user.publicRepos = profile._json.public_repos;
  					newUser.nbrClicks.clicks = 0;
  
  					newUser.save(function (err) {
  						if (err) {
  							throw err;
  						}
  
  						return done(null, newUser);
  					});
  				}
  			});
  		});
  	}));
  };
  