generated from agiledev-students-fall2023/generic-project-repository
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from agiledev-students-fall2023/forgot-password
Implemented Forgot password
Showing
4 changed files
with
375 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,37 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const { User } = require("../models/User.js"); | ||
const bcrypt = require("bcrypt"); | ||
|
||
router.post("/", async (req, res) => { | ||
const response = { | ||
status: "Success", | ||
message: "We recieved your data!", | ||
data: { | ||
name: req.body.username, | ||
password: req.body.password, | ||
}, | ||
}; | ||
res.json(response); | ||
const username = req.body.username; | ||
const newPassword = req.body.newPassword; | ||
const email = req.body.email; | ||
|
||
try { | ||
const user = await User.findOne({ | ||
$and: [{ $or: [{ username: username }, { email: email }] }], | ||
}); | ||
|
||
console.log(user); | ||
|
||
if (!user) { | ||
console.error(`User not found.`); | ||
return res | ||
.status(404) | ||
.json({ success: false, message: "User not found." }); | ||
} | ||
|
||
const hashedPassword = await bcrypt.hash(newPassword, 10); | ||
user.password = hashedPassword; | ||
|
||
await user.save(); | ||
|
||
res.json({ success: true, message: "Password updated successfully." }); | ||
} catch (error) { | ||
console.error(`Error updating password: ${error}`); | ||
res.status(500).json({ success: false, message: "Internal server error." }); | ||
} | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters