-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
44 lines (36 loc) · 1009 Bytes
/
passport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
var passport = require('passport'),
User = require('../models').User,
LocalStrategy = require('passport-local').Strategy;
/**
* Passport configuration
*/
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.find({where: {id: id}}).then(function(user) {
done(null, user);
}, done);
});
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password' // this is the virtual field on the model
},
function(email, password, done) {
User.find({where: {email: email}}).then(function(user) {
if(user) {
user.verifyPassword(password).then(function(isEqual) {
if(isEqual) {
done(null, user.getJSON());
} else {
done(null, false, 'Incorrect password');
}
}).error(done);
} else {
done(null, false, 'Email is not registered');
}
}, done);
}
));
module.exports = passport;