Upload files to 'config'

This commit is contained in:
Muhammad Iqbal Rifai 2018-07-04 11:17:32 +02:00
parent 260b71c90e
commit 5ed8545710
1 changed files with 39 additions and 0 deletions

39
config/passport.js Normal file
View File

@ -0,0 +1,39 @@
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const User = require('../models/user');
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
try{
const user = await User.findById(id);
done(null, user.id);
}catch(error){
done(error, null);
}
});
passport.use('local', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: false
}, async (email, password, done)=>{
try{
// 1) Check if the email already exists
const user = await User.findOne({ 'email': email});
if (!user){
return done(null,false,{ message: 'Unknown User' })
}
// 2) Check if the password is correct
const isValit = User.comparePasswords(password,user.password);
if(isValit){
return done(null, user);
}else{
return done(null, false,{ message: 'Unknown Password'});
}
}catch(error){
return done(error, false);
}
}));