-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassportConfig.js
48 lines (43 loc) · 1.18 KB
/
passportConfig.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
// passportConfig.js
const passport = require('passport');
const LocalStrategy = require('passport-local');
const bcrypt = require('bcrypt');
const User = require('./models/User');
passport.use(
new LocalStrategy(async (username, password, done) => {
try {
const user = await User.findOne({ username });
if (!user) {
return done(null, false, {
message: 'Invalid credentials',
});
}
const isMatch = await bcrypt.compare(password, user.password);
if (isMatch) {
return done(null, user);
} else {
return done(null, false, {
message: 'Invalid credentials',
});
}
} catch (err) {
return done(err);
}
}),
);
passport.serializeUser(async (user, done) => {
try {
done(null, user.id);
} catch (err) {
done(err, null);
}
});
passport.deserializeUser(async (id, done) => {
try {
const user = await User.findById(id);
done(null, user);
} catch (err) {
done(err, null);
}
});
module.exports = passport;