-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserAuthentication.js
44 lines (37 loc) · 1.14 KB
/
UserAuthentication.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
import passport from "passport";
import { Strategy as LocalStrategy } from "passport-local";
import User from "./users/model.js"
passport.use(
new LocalStrategy(
{
usernameField: "email",
},
async (email, password, done) => {
// Match Email's User
const user = await User.findOne({ email: email });
if (!user) {
return done(null, false, { message: "Not User found." });
} else {
// Match Password's User
const match = await user.matchPassword(password);
if (match) {
return done(null, user);
} else {
return done(null, false, { message: "Incorrect Password." });
}
}
}
)
);
// object serialization: translating a data structure or object state into a format that can be stored
// as simple as serializing the user ID, and finding the user by ID when deserializing.
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user);
});
});
export let init_passport = passport.initialize();
export let init_session = passport.session();