Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Reset Password #27

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions controllers/otpController.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,40 @@ const verifyOtp = async (req, res) => {
}
}

const resetPassword = async (req, res) => {
const user = await prisma.user.findFirst({
where: {
id: parseToken(req.userId).userId,
}
});

if (!user) {
return res.status(400).json({ message: "User not found" });
}

const salt = await bcrypt.genSalt(10);
const newHashedPass = await bcrypt.hash(req.body.password, salt);

try {
prisma.user.update({
where: {
id: user.id,
},
data: {
password: newHashedPass,
},
})
} catch(err) {
console.log(err);
return res.status(500).json({ message: "Internal server error" });
}
const newToken = jwt.sign({ id: user.id }, JWT_SECRET, { expiresIn: jwtExpiryTime }); // Generate new token or user id
res.json({ message: "Password Updated", data: { token: newToken, user: user } }); // Return token and user data
}

module.exports = {
generateOtp,
verifyOtp,
resetPassword,
};

15 changes: 14 additions & 1 deletion routes/otpRoutes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const router = require("express").Router();

const { generateOtp, verifyOtp } = require("../controllers/otpController");
const { generateOtp, verifyOtp, resetPassword} = require("../controllers/otpController");



Expand Down Expand Up @@ -41,5 +41,18 @@ router.post("/verify", [
next();
}, verifyOtp);

router.post('/reset_password', [
check('password', 'Password length should be atleast 8 characters')
.isLength({ min: 8 }),
header('Authorization', 'Token is required').exists(),
],(req, res, next) => {
const errors = validationResult(req);
console.log(errors);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
next();
}, resetPassword)

module.exports = router;