-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassportConfig.js
53 lines (43 loc) · 1.44 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
49
50
51
52
53
const LocalStrategy = require("passport-local").Strategy;
// const { pool } = require('./Database/dbConfig');
// const bcrypt = require("bcrypt");
const argon2 = require("argon2");
/*************************************
* Require Models
*************************************/
const userModel = require("./Models/userModel");
function initialize(passport) {
const authenticateUser = async (username, password, done) => {
// Get user from database
const user = userModel.getUserByUsername(username);
// Check if there were no records returned
if (user) {
// Check Password
if (await argon2.verify(user.passwordHash, password)) {
return done(null, user);
}
else {
return done(null, false, {message: "Username or Password is not correct!"});
}
}
else {
return done(null, false, {message: "Username is not registered!"});
}
};
passport.use(new LocalStrategy({
usernameField: "username",
passwordField: "password",
},
authenticateUser
));
passport.serializeUser((user, done) => {
done(null, user.userID);
});
passport.deserializeUser((id, done) => {
user = userModel.getUserByID(id);
if (user) {
return done(null, user);
}
});
}
module.exports = initialize;