-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport-config.js
79 lines (65 loc) · 2.3 KB
/
passport-config.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
72
73
74
75
76
77
78
79
// GNU GENERAL PUBLIC LICENSE
// Version 3, 29 June 2007
// Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
// Everyone is permitted to copy and distribute verbatim copies
// of this license document, but changing it is not allowed.
// Preamble
// The GNU General Public License is a free, copyleft license for
// software and other kinds of works.
// const LocalStrategy = require('passport-local').Strategy
// const bcrypt = require('bcrypt')
// const User = require('./models/user_model')
// function initialize(passport, getUserById) {
// const authenticateUser = async (email, password, done) => {
// const user = await User.findOne({email: email});
// if (user == null) {
// return done(null, false, { message: 'No user with that email' })
// }
// try {
// if (await bcrypt.compare(password, user.password)) {
// return done(null, user)
// } else {
// return done(null, false, { message: 'Password incorrect' })
// }
// } catch (e) {
// return done(e)
// }
// }
// passport.use(new LocalStrategy({ usernameField: 'email' }, authenticateUser))
// passport.serializeUser((user, done) => done(null, user.id))
// passport.deserializeUser((id, done) => {
// return done(null, getUserById(id))
// })
// }
// module.exports = initialize
const passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcrypt')
const User = require('./models/user_model')
function initialize(){
const authenticateUser = async function(email, password, done) {
const user = await User.findOne({ email: email })
if (!user) {
return done(null, false, { message: 'Incorrect email.' });
}
try {
if (await bcrypt.compare(password, user.password)) {
return done(null, user)
} else {
return done(null, false, { message: 'Password incorrect' })
}
} catch (e) {
return done(e)
};
};
passport.use(new LocalStrategy({ usernameField: 'email' }, authenticateUser))
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
}
module.exports = initialize