-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport-config.js
49 lines (46 loc) · 1.91 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
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcrypt');
const { query } = require('./db');
module.exports = function(passport) {
passport.use(new LocalStrategy(
async (username, password, done) => {
try {
const userQuery = await query('SELECT * FROM users WHERE username = $1', [username]);
if (userQuery.rows.length > 0) {
const user = userQuery.rows[0];
// Compare hashed password
if (await bcrypt.compare(password, user.password_hash)) {
console.log(`User authenticated successfully: ${username}`);
return done(null, user);
} else {
console.log(`Password incorrect for user: ${username}`);
return done(null, false, { message: 'Password incorrect' });
}
} else {
console.log(`No user found with username: ${username}`);
return done(null, false, { message: 'No user found' });
}
} catch (err) {
console.error(`Error during authentication for user ${username}:`, err.stack);
return done(err);
}
}
));
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
try {
const userQuery = await query('SELECT * FROM users WHERE id = $1', [id]);
if (userQuery.rows.length > 0) {
const user = userQuery.rows[0];
done(null, user);
} else {
done(new Error(`User with id ${id} not found`), null);
}
} catch (err) {
console.error(`Error during deserialization for user ID ${id}:`, err.stack);
done(err, null);
}
});
};