moving resetLink post to forgotpass.js

This commit is contained in:
knighthawk4227 committed 2025-05-09 11:18:25 -07:00
1 parent f3b121fc71
commit f3dbbebb43
2 files changed
+54 -4

No files matched your search

+52 -2
View File
@@ -2,7 +2,9 @@ const express = require('express');
const crypto = require('crypto'); const crypto = require('crypto');
const joi = require('joi'); const joi = require('joi');
const nodeMail = require('nodemailer'); const nodeMail = require('nodemailer');
const bcrypt = require('bcrypt');
require('dotenv').config(); require('dotenv').config();
const PORT = process.env.PORT;
const transporter = nodeMail.createTransport({ const transporter = nodeMail.createTransport({
service: 'gmail', service: 'gmail',
@@ -16,7 +18,6 @@ module.exports = (users) => {
const router = express.Router(); const router = express.Router();
router.post('/auth/resetPass', async (req, res) => { router.post('/auth/resetPass', async (req, res) => {
console.log("we are inside of the post");
const resetSchema = joi.object({ const resetSchema = joi.object({
email: joi.string().email().required(), email: joi.string().email().required(),
}); });
@@ -44,7 +45,7 @@ module.exports = (users) => {
$set: { resetToken: token, resetTokenExpires: expiration } $set: { resetToken: token, resetTokenExpires: expiration }
}); });
const resetUrl = `http://localhost:3000/reset/${token}`; const resetUrl = `http://localhost:${PORT}/reset/${token}`;
const mailSend = { const mailSend = {
from: process.env.EMAIL_USER, from: process.env.EMAIL_USER,
@@ -63,6 +64,55 @@ module.exports = (users) => {
res.status(500).send('email failed to send try again'); res.status(500).send('email failed to send try again');
} }
}); });
router.post('/resetLink', async (req, res) => {
const { token, password, confirmPassword, } = req.body;
const passwordSchema = joi.object({
password: joi.string().max(20).required(),
confirmPassword: joi.string().max(20).required(),
});
const valid = passwordSchema.validate({ password, confirmPassword });
if (valid.error) {
console.log("houston we have a problem");
req.session.error = 'Invalid input';
res.status(status.BadRequest);
return res.redirect(`/reset/${token}`);
}
if (!token || !password || !confirmPassword) {
req.session.error = 'field may be missing';
return res.redirect(`/reset/${token}`);
}
if (password !== confirmPassword) {
req.session.error = 'passwords do not match';
return res.redirect(`/reset/${token}`);
}
const user = await users.findOne({
resetToken: token,
resetTokenExpires: { $gt: Date.now() },
});
if (!user) {
req.session.error = 'Reset link is invalid.';
return res.redirect(`/reset`);
}
const hashPassword = await bcrypt.hash(password, 12);
await users.updateOne(
{
email: user.email
},
{
$set: {
password: hashPassword,
resetToken: '',
resetTokenExpires: 0,
},
}
);
req.session.success = 'Password has been reset';
res.redirect('/login');
});
return router; return router;
} }
+2 -2
View File
@@ -9,13 +9,13 @@
<input type="hidden" name="token" value="<%=token %>"> <input type="hidden" name="token" value="<%=token %>">
<div class="mt-3"> <div class="mt-3">
<label for="password" class="block text-base mb-2">New Password:</label> <label for="password" class="block text-base mb-2">New Password:</label>
<input type="text" id="password" name="password" <input type="password" id="password" name="password"
class="border focus:border-gray-600 w-full rounded-md text-base px-2 py-1 focus:outline-none focus:ring-0 " class="border focus:border-gray-600 w-full rounded-md text-base px-2 py-1 focus:outline-none focus:ring-0 "
placeholder="Enter Password" /> placeholder="Enter Password" />
</div> </div>
<div class="mt-3"> <div class="mt-3">
<label for="password" class="block text-base mb-2">Confirm Password:</label> <label for="password" class="block text-base mb-2">Confirm Password:</label>
<input type="text" id="Repassword" name="confirmPassword" <input type="password" id="Repassword" name="confirmPassword"
class="border focus:border-gray-600 w-full rounded-md text-base px-2 py-1 focus:outline-none focus:ring-0 " class="border focus:border-gray-600 w-full rounded-md text-base px-2 py-1 focus:outline-none focus:ring-0 "
placeholder="ReType Password" /> placeholder="ReType Password" />
</div> </div>