a
This commit is contained in:
parent
f7e4775ed3
commit
ac98abc23e
110
consumerWebsite/controller/user_controller.js
Normal file
110
consumerWebsite/controller/user_controller.js
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
const connection = require('../database/mySQL');
|
||||||
|
const { isEmpty } = require('../utils/object_isEmpty');
|
||||||
|
const AppError = require('../utils/error');
|
||||||
|
const bcrypt = require('bcryptjs');
|
||||||
|
const { USER_MODEL, USER_LOGIN_MODEL, FORGOT_PASSWORD_MODEL, RESET_PASSWORD_MODEL } = require('../database/model/userModel');
|
||||||
|
const nodemailer = require('nodemailer');
|
||||||
|
|
||||||
|
exports.user_forgotPassword = (req, res, next) => {
|
||||||
|
|
||||||
|
//Check the form data is found or not
|
||||||
|
if (isEmpty(req.body)) return next(new AppError('form data not found', 400));
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
//Check the form data is valid or not
|
||||||
|
const { error } = FORGOT_PASSWORD_MODEL.validate(req.body);
|
||||||
|
|
||||||
|
if (error) return next(new AppError(error.details[0].message, 400));
|
||||||
|
|
||||||
|
connection.query("SELECT * FROM user WHERE email = ?", [[req.body.email]], async (err, data1, fields) => {
|
||||||
|
if (err) return next(new AppError(err, 500));
|
||||||
|
|
||||||
|
if (data1.length == 0) {
|
||||||
|
return next(new AppError("user not exist", 400))
|
||||||
|
}
|
||||||
|
|
||||||
|
const otp = Math.floor(1000 + Math.random() * 9000);
|
||||||
|
|
||||||
|
const otpExpier = new Date();
|
||||||
|
otpExpier.setMinutes(otpExpier.getMinutes() + 1);
|
||||||
|
|
||||||
|
connection.query("UPDATE user SET otp = ?, otpExpire = ? WHERE email = ?", [otp, otpExpier, req.body.email], (err, data2, fields) => {
|
||||||
|
if (err) return next(new AppError(err, 500));
|
||||||
|
|
||||||
|
const transporter = nodemailer.createTransport({
|
||||||
|
service: 'Gmail',
|
||||||
|
auth: {
|
||||||
|
user: 'ecosavertp@gmail.com',
|
||||||
|
pass: 'Ecosaver1234!',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const mailOptions = {
|
||||||
|
from: 'ecosavertp@gmail.com',
|
||||||
|
to: req.body.email,
|
||||||
|
subject: 'Password reset OTP',
|
||||||
|
text: `Your OTP (It is expired after 1 min) : ${otp}`,
|
||||||
|
};
|
||||||
|
|
||||||
|
transporter.sendMail(mailOptions, (error, info) => {
|
||||||
|
if (error) {
|
||||||
|
return next(new AppError(error, 500));
|
||||||
|
} else {
|
||||||
|
res.json({
|
||||||
|
data: "Your OTP send to the email"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
return next(new AppError(err, 500));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.user_resetPassword = (req, res, next) => {
|
||||||
|
|
||||||
|
const body = req.body;
|
||||||
|
const password = body.password;
|
||||||
|
const confirmPassword = body.confirmPassword;
|
||||||
|
|
||||||
|
if (isEmpty(body)) return next(new AppError('form data not found', 400));
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
const { error } = RESET_PASSWORD_MODEL.validate(body);
|
||||||
|
|
||||||
|
if (error) return next(new AppError(error.details[0].message, 400));
|
||||||
|
|
||||||
|
if (password.localeCompare(confirmPassword) != 0) return next(new AppError('passwords are not equal', 400));
|
||||||
|
|
||||||
|
connection.query("SELECT * FROM user WHERE otp = ? AND otpExpire > NOW()", [[body.otp]], async (err, data, fields) => {
|
||||||
|
if (err) return next(new AppError(err, 500));
|
||||||
|
|
||||||
|
if (data.length == 0) return next(new AppError('Invalid or expired OTP', 400));
|
||||||
|
|
||||||
|
const solt = await bcrypt.genSalt(10);
|
||||||
|
const hashedPassword = await bcrypt.hash(password, solt);
|
||||||
|
|
||||||
|
connection.query("UPDATE user SET password = ?, otp = null, otpExpire = null WHERE otp = ?", [hashedPassword, body.otp], async (err, data, fields) => {
|
||||||
|
if (err) return next(new AppError(err, 500));
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
data: 'Password reset successful'
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
return next(new AppError(err, 500));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -76,4 +76,8 @@ router.delete("/delete", async function (req, res, next) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { user_register, user_login, user_forgotPassword, user_resetPassword } = require('../controller/user_controller');
|
||||||
|
router.route("/forgotPassword").post(user_forgotPassword);
|
||||||
|
router.route("/resetPassword").post(user_resetPassword);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
13
consumerWebsite/utils/error.js
Normal file
13
consumerWebsite/utils/error.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
class AppError extends Error {
|
||||||
|
constructor(msg, statusCode) {
|
||||||
|
super(msg);
|
||||||
|
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
this.error = `${statusCode}`.startsWith('4') ? 'fail' : 'error';
|
||||||
|
this.isOperational = true;
|
||||||
|
|
||||||
|
Error.captureStackTrace(this, this.constructor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = AppError;
|
0
consumerWebsite/utils/errorHandler.js
Normal file
0
consumerWebsite/utils/errorHandler.js
Normal file
8
consumerWebsite/utils/object_isEmpty.js
Normal file
8
consumerWebsite/utils/object_isEmpty.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
//Checking that the request body is empty
|
||||||
|
exports.isEmpty = function (obj) {
|
||||||
|
for(var prop in obj) {
|
||||||
|
if(obj.hasOwnProperty(prop))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return JSON.stringify(obj) === JSON.stringify({});
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user