Skip to content

Commit ed8308b

Browse files
committed
fix(auth): sanitize login response to exclude password hash
1 parent 9d34c19 commit ed8308b

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

backend/models/User.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ UserSchema.methods.comparePassword = async function (enteredPassword) {
3131
return bcrypt.compare(enteredPassword, this.password);
3232
};
3333

34+
UserSchema.methods.toSafeObject = function () {
35+
return {
36+
id: this._id,
37+
username: this.username,
38+
email: this.email,
39+
};
40+
};
41+
3442
module.exports = mongoose.model("User", UserSchema);

backend/routes/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ router.post("/signup", validateRequest(signupSchema), async (req, res) => {
3232

3333
// Login route
3434
router.post("/login", validateRequest(loginSchema), passport.authenticate('local'), (req, res) => {
35-
res.status(200).json( { message: 'Login successful', user: req.user } );
35+
+ res.status(200).json({ message: 'Login successful', user: req.user.toSafeObject() });
3636
});
3737

3838
// Logout route

0 commit comments

Comments
 (0)