-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
23 lines (19 loc) · 876 Bytes
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// sets up Passport with a local authentication strategy, using a Person model for user data. - Auth.js file
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const Person = require("./models/Person"); // Adjust the path as needed
passport.use(
new LocalStrategy(async (username, password, done) => {
try {
// console.log('Received credentials:', username, password);
const user = await Person.findOne({ username });
if (!user) return done(null, false, { message: "Incorrect username." });
const isPasswordMatch = await user.comparePassword(password);
if (isPasswordMatch) return done(null, user);
else return done(null, false, { message: "Incorrect password." });
} catch (error) {
return done(error);
}
})
);
module.exports = passport; // Export configured passport