-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
31 lines (26 loc) · 996 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
const LocalStrategy = require('passport-local').Strategy
const bcrypt = require('bcrypt')
const User = require('./schemas/userSchema');
const { validateEmail } = require('./utilities/misc')
async function initialize(passport) {
const authenticateUser = async (email, password, done) => {
if (!validateEmail(email)) {
return done(null, false, { msg: 'The email is not a valid email.' })
}
User.findOne({ email: email }).then(async user => {
if (!user) {
return done(null, false, { msg: 'There is no user with That email' })
}
done(null, user)
})
}
await passport.use(new LocalStrategy({ usernameField: 'email' }, authenticateUser))
await passport.serializeUser((user, done) => {
done(null, user._id)
})
passport.deserializeUser(async function (id, done) {
const user = await User.findById(id)
done(null, user);
});
}
module.exports = initialize