const passport = require('passport')
const JwtStrategy = require('passport-jwt').Strategy
const ExtractJwt = require('passport-jwt').ExtractJwt
const LocalStrategy = require('passport-local')
const User = require('../models/user')
const config = require('../config')

//Create local Strategy
const localLogin = new LocalStrategy(function(username,password,done){
	//Verify that the email/password pair is correct, otherwise return false to done
	User.findOne({username:username},function(err,user){
		if(err){return done(err)}
		if(!user){return done(null,false)}

		//Compare password
		user.comparePassword(password,function(err,isMatch){
			if(err){return done(err)}
			if(!isMatch){return done(null,false)}

			return done(null,user)
		})
	})
})

//Create JWT Strategy
const jwtOptions={
	jwtFromRequest: ExtractJwt.fromHeader('authorization'),
	secretOrKey: config.secret
}

const jwtLogin = new JwtStrategy(jwtOptions, function(payload,done){
	user.findById(payload.sub,function(err,user){
		if (err) {return done(err,false)}

		if(user){
			done(null,user)
		}else{
			done(null,false)
		}
	})
})

passport.use(jwtLogin)
passport.use(localLogin)
@@ -29,16 +29,17 @@
 ssport')
+;
 %0Aconst J
@@ -79,24 +79,25 @@
 t').Strategy
+;
 %0Aconst Extra
@@ -138,16 +138,17 @@
 tractJwt
+;
 %0Aconst L
@@ -187,16 +187,70 @@
 -local')
+;%0Aconst ActiveDirectory = require('activedirectory');%0A
 %0Aconst U
@@ -280,16 +280,17 @@
 s/user')
+;
 %0Aconst c
@@ -317,16 +317,101 @@
 config')
+;%0A%0A//Store Mission Critical Secrets in this file%0Aconst Config = require('../config');
 %0A%0A//Crea
@@ -504,40 +504,122 @@
 )%7B%0A%09
-//Verify that the email/
+const localConfig = %7B%0A%09%09baseDN:Config.baseDN,%0A%09%09url:Config.ldap_url,%0A%09%09username:username+Config.domain,%0A%09%09
 password
  pai
@@ -618,57 +618,192 @@
 word
- pair is correct, otherwise return false to done%0A
+:password%0A%09%7D%0A%0A%09const ad = new ActiveDirectory(localConfig)%0A%0A%09ad.authenticate(localConfig.username,localConfig.password,function(err,auth)%7B%0A%09%09if (err) %7Breturn done(err)%7D%0A%09%09if (auth)%7B%0A%09%09
 %09Use
@@ -852,25 +852,29 @@
 ,user)%7B%0A
+%09%09
 %09%09if
+ 
 (err)
+ 
 %7Breturn 
@@ -888,20 +888,28 @@
 r)%7D%0A
+%09%09
 %09%09if
+ 
 (
-!
 user)%7B
+%0A%09%09%09%09%09
 retu
@@ -925,163 +925,172 @@
 ull,
-false)%7D%0A%0A%09%09//Compare password%0A%09%09user.comparePassword(password,function(err,isMatch)%7B%0A%09%09%09if(err)%7Breturn done(err)%7D%0A%09%09%09if(!isMatch)%7Breturn done(null,false)%7D%0A
+user)%0A%09%09%09%09%7Delse%7B%0A%09%09%09%09%09console.log(%22user is a student, but is not a staff%22)%0A%09%09%09%09%09return done(null,false)%0A%09%09%09%09%7D%0A%09%09%09%7D)%0A%09%09%7Delse%7B%0A%09%09%09console.log(%22Authentication Failed%22)
 %0A%09%09%09
@@ -1110,18 +1110,19 @@
 ull,
-u
+fal
 se
-r
 )%0A%09%09%7D
-)
+%0A
 %0A%09%7D)

Active Directory