forgot password validatation using joi
This commit is contained in:
1 parent
cb59e86d8a
commit
15dd4e0d20
3 files changed
+56
-19
No files matched your search
+14
-3
@@ -1,5 +1,6 @@
|
||||
const express = require('express');
|
||||
const crypto = require('crypto');
|
||||
const joi = require('joi');
|
||||
const nodeMail = require('nodemailer');
|
||||
require('dotenv').config();
|
||||
|
||||
@@ -16,17 +17,27 @@ module.exports = (users) => {
|
||||
|
||||
router.post('/auth/resetPass', async (req, res) => {
|
||||
console.log("we are inside of the post");
|
||||
const { email } = req.body;
|
||||
const user = await users.findOne({ email });
|
||||
const resetSchema = joi.object({
|
||||
email: joi.string().email().required(),
|
||||
});
|
||||
req.session.error = '';
|
||||
req.session.reset = '';
|
||||
|
||||
const valid = resetSchema.validate(req.body);
|
||||
if (valid.error) {
|
||||
req.session.error = 'invalid email';
|
||||
return res.redirect('/forgotPassword')
|
||||
}
|
||||
const { email } = req.body;
|
||||
const user = await users.findOne({ email });
|
||||
|
||||
if (!user) {
|
||||
req.session.error = 'No user found'
|
||||
return res.redirect('/forgotPassword');
|
||||
}
|
||||
|
||||
const token = crypto.randomBytes(32).toString('hex');
|
||||
console.log(`The reset token is ${token}`)
|
||||
const expiration = Date.now() + 360000;
|
||||
|
||||
await users.updateOne({ email }, {
|
||||
@@ -39,7 +50,7 @@ module.exports = (users) => {
|
||||
from: process.env.EMAIL_USER,
|
||||
to: email,
|
||||
subject: 'Password reset',
|
||||
text: `reset your password here ${resetUrl} \n this link will expire within 1 hour`,
|
||||
text: `reset your password here ${resetUrl} this link will expire within 1 hour`,
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user