var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var InstagramStrategy = require('passport-instagram').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var Model = require('../models/model.js');
var config = require('../config.js');

function checkUser(user,done)
{

    if (user !== null) {
        //we have the user in the db already
        console.log('at done');
        done(null, user);

    } else {
        //we don't have the user in the db
        user = Model.Users.build({
            oauth_id: profile.id,
            name: profile.displayName,
            created: Date.now()
        });

        user.save().then(function(user) {
            done(null, user);
        }).catch(function(e){
            done(e, false);
        });
    }

}

module.exports = passport.use(new FacebookStrategy({
  clientID: config[env].passport.facebook.publicKey,
  clientSecret: config[env].passport.facebook.secretKey,
  callbackURL: config[env].passport.facebook.callbackURL
  },
  function(accessToken, refreshToken, profile, done) {

    Model.Users.findOne({ where:{ oauth_id: profile.id } }).then(function(user) {
        checkUser(user,done);
    }).catch(function(e){
        console.log(e);  //manage the error
    });

  }
));

passport.use(new TwitterStrategy({
  consumerKey: config[env].passport.twitter.publicKey,
  consumerSecret: config[env].passport.twitter.secretKey,
  callbackURL: config[env].passport.twitter.callbackURL
  },
  function(accessToken, refreshToken, profile, done) {

    Model.Users.findOne({ where:{ oauth_id: profile.id } }).then(function(user) {
        checkUser(user,done);
    }).catch(function(e){
        console.log(e);  //manage the error
    });

  }
));

passport.use(new InstagramStrategy({
  clientID: config[env].passport.instagram.publicKey,
  clientSecret: config[env].passport.instagram.secretKey,
  callbackURL: config[env].passport.instagram.callbackURL
  },
  function(accessToken, refreshToken, profile, done) {

    Model.Users.findOne({ where:{ oauth_id: profile.id } }).then(function(user) {
        checkUser(user,done);
    }).catch(function(e){
        console.log(e);  //manage the error
    });

  }
));


@@ -312,28 +312,31 @@
 n checkUser(
-user
+profile
 ,done)%0A%7B%0A%0A  
@@ -333,16 +333,103 @@
 one)%0A%7B%0A%0A
+    Model.Users.findOne(%7B where:%7B oauth_id: profile.id %7D %7D).then(function(user) %7B%0A%0A    
     if (
@@ -441,24 +441,28 @@
 !== null) %7B%0A
+    
         //we
@@ -494,32 +494,36 @@
 already%0A        
+    
 console.log('at 
@@ -530,32 +530,36 @@
 done');%0A        
+ 
+   
 done(null, user)
@@ -557,24 +557,28 @@
 ll, user);%0A%0A
+    
     %7D else %7B
@@ -578,16 +578,20 @@
  else %7B%0A
+    
         
@@ -629,24 +629,28 @@
  db%0A        
+    
 user = Model
@@ -676,16 +676,20 @@
         
+    
 oauth_id
@@ -702,16 +702,20 @@
 ile.id,%0A
+    
         
@@ -757,16 +757,20 @@
         
+    
 created:
@@ -785,24 +785,28 @@
 w()%0A        
+    
 %7D);%0A%0A       
@@ -802,24 +802,28 @@
 );%0A%0A        
+   
+ 
 user.save().
@@ -848,32 +848,36 @@
 ) %7B%0A            
+    
 done(null, user)
@@ -882,24 +882,28 @@
 r);%0A        
+ 
+   
 %7D).catch(fun
@@ -920,24 +920,28 @@
             
+   
+ 
 done(e, fals
@@ -956,16 +956,20 @@
     
+    
 %7D);%0A    
 %7D%0A%0A%7D
@@ -964,17 +964,100 @@
 %7D);%0A    
+ 
-%7D
+   %7D%0A%0A    %7D).catch(function(e)%7B%0A        console.log(e);  //manage the error%0A    %7D);
 %0A%0A%7D%0A%0Amod
@@ -1334,102 +1334,17 @@
 one)
- %7B%0A%0A    Model.Users.findOne(%7B where:%7B oauth_id: profile.id %7D %7D).then(function(user) %7B
 %0A  
+%7B%0A
     
-  
 chec
@@ -1353,99 +1353,23 @@
 ser(
-user,done);%0A    %7D).catch(function(e)%7B%0A        console.log(e);  //manage the error%0A    %7D
+profile,done
 );%0A
-%0A
   %7D%0A
@@ -1638,203 +1638,42 @@
 one)
- %7B%0A%0A    Model.Users.findOne(%7B where:%7B oauth_id: profile.id %7D %7D).then(function(user) %7B%0A        checkUser(user,done);%0A    %7D).catch(function(e)%7B%0A        console.log(e);  //manage the error%0A    %7D
+%0A  %7B%0A    checkUser(profile,done
 );%0A
-%0A
   %7D%0A
@@ -1945,102 +1945,17 @@
 one)
+%0A 
  %7B%0A
-%0A    Model.Users.findOne(%7B where:%7B oauth_id: profile.id %7D %7D).then(function(user) %7B%0A   
     
- 
 chec
@@ -1964,99 +1964,23 @@
 ser(
-user,done);%0A    %7D).catch(function(e)%7B%0A        console.log(e);  //manage the error%0A    %7D
+profile,done
 );%0A
-%0A
   %7D%0A

refactor a little more