-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassport.js
71 lines (62 loc) · 1.6 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var bcrypt = require('bcrypt-nodejs');
var db = require('./models');
const LocalStrategy = require('passport-local').Strategy;
var passport = require('passport');
const authenticate = (email, password, done) =>{
db.teachers.find({
where:{
teacher_email: email,
}
}).then(teacher => {
if (!teacher) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!bcrypt.compareSync(password, teacher.teacher_password)) {
return done(null, false, { message: 'Incorrect password.' });
}
done(null, teacher);
})
.catch(done)
}
const register = (req, email, password, done) => {
db.teachers.find({
where:{
teacher_email: req.body.email,
}
})
.then(user => {
if (user) {
return done(null, true, { message: 'an account with that email has already been created' });
}
if (password !== req.body.password2) {
return done(null, true, { message: `passwords don't match` });
}
db.teachers.create({
teacher_firstName: req.body.firstName,
teacher_lastName: req.body.lastName,
teacher_userName: req.body.username,
teacher_email: req.body.email,
teacher_password: bcrypt.hashSync(password)
})
.then(function() {
done(null, false);
})
})
.catch(done)
}
passport.use(new LocalStrategy(authenticate));
passport.use('local-register', new LocalStrategy({passReqToCallback: true}, register));
passport.serializeUser((teachers, done) => {
done(null, teachers.teacher_id)
});
passport.deserializeUser((id, done) => {
db.teachers.find({
where:{
teacher_id: id,
}
})
.then(teacher => {
done(null, teacher)
})
.catch(done)
})