-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpassport.js
More file actions
59 lines (55 loc) · 2.3 KB
/
Copy pathpassport.js
File metadata and controls
59 lines (55 loc) · 2.3 KB
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
54
55
56
57
58
59
// Import the LocalStrategy constructor from the passport-local module
const LocalStrategy = require("passport-local").Strategy;
// Import mongoose to interact with the MongoDB database
const mongoose = require("mongoose");
// Import the User model which contains schema and methods for authentication
const User = require("../models/User");
// Export a function to configure Passport.js
module.exports = function (passport) {
// Configure the local strategy for username/password authentication
passport.use(
new LocalStrategy(
{ usernameField: "email" }, // Use "email" instead of default "username"
(email, password, done) => {
// Find the user in the database by email (converted to lowercase)
User.findOne({ email: email.toLowerCase() }, (err, user) => {
if (err) {
return done(err); // Return any DB error
}
if (!user) {
// No user found with that email
return done(null, false, { msg: `Email ${email} not found.` });
}
if (!user.password) {
// User exists but doesn't have a local password set (used a third-party login)
return done(null, false, {
msg:
"Your account was registered using a sign-in provider. To enable password login, sign in using a provider, and then set a password under your user profile.",
});
}
// If user found, compare the entered password with the hashed one in DB
user.comparePassword(password, (err, isMatch) => {
if (err) {
return done(err); // Return error if comparison fails
}
if (isMatch) {
// Password matches, login successful
return done(null, user);
}
// Password does not match
return done(null, false, { msg: "Invalid email or password." });
});
});
}
)
);
// Serialize the user ID to store in session cookie
passport.serializeUser((user, done) => {
done(null, user.id); // Store only the user's ID in the session
});
// Deserialize the user based on ID stored in the session
passport.deserializeUser((id, done) => {
// Look up the user in the database by their ID
User.findById(id, (err, user) => done(err, user));
});
};