Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions backend/config/passportConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ passport.use(
{ usernameField: "email" },
async (email, password, done) => {
try {
const user = await User.findOne( {email} );
const user = await User.findOne( {email} ).select("+password");;
if (!user) {
return done(null, false, { message: 'Email is invalid '});
}
Expand Down Expand Up @@ -38,7 +38,10 @@ passport.serializeUser((user, done) => {
passport.deserializeUser(async (id, done) => {
try {
const user = await User.findById(id);
done(null, user);
if (!user) {
return done(null, false);
}
done(null,user);
} catch (err) {
done(err, null);
}
Expand Down
16 changes: 10 additions & 6 deletions backend/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ const UserSchema = new mongoose.Schema({
});

// ✅ FIXED: no next()
UserSchema.pre('save', async function () {
if (!this.isModified('password')) return;

const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
UserSchema.pre('save', async function (next) {
if (!this.isModified('password')) return next();
try {
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
next(); // Tells Mongoose hashing is done, save the document now
} catch (err) {
next(err); // Safely passes any encryption errors to the database handler
}
});

// ✅ password comparison
UserSchema.methods.comparePassword = async function (enteredPassword) {
return bcrypt.compare(enteredPassword, this.password);
};

module.exports = mongoose.model("User", UserSchema);
module.exports = mongoose.model("User", UserSchema);
Loading