forget pasword

This commit is contained in:
pawan-dot 2024-07-02 13:05:19 +05:30
parent d4b4c1341d
commit 77ae4b7c5c
2 changed files with 57 additions and 2 deletions

View File

@ -2,8 +2,9 @@
import crypto from 'crypto'; import crypto from 'crypto';
import Patient from './PatientModel.js' import Patient from './PatientModel.js'
import { sendOtp } from '../../Utils/sendEmail.js'; import sendEmail, { sendOtp } from '../../Utils/sendEmail.js';
import validator from "validator"; import validator from "validator";
import password from "secure-random-password";
export const register = async (req, res) => { export const register = async (req, res) => {
let { name, countryCode, mobileNumber } = req.body; let { name, countryCode, mobileNumber } = req.body;
@ -275,7 +276,58 @@ export const EnterPersonalDetails = async (req, res) => {
}; };
// 4.Forgot Password
export const forgotPassword = async (req, res, next) => {
const patient = await Patient.findOne({ email: req.body.email });
if (!patient) {
return res.status(404).json({ message: "Patient not found" });
}
// Get ResetPassword Token
// const resetToken = patient.getResetPasswordToken(); //call function
//save database reset token
await patient.save({ validateBeforeSave: false });
const passwords = password.randomPassword({
length: 12,
characters: [
{ characters: password.upper, exactly: 1 },
{ characters: password.symbols, exactly: 1 },
password.lower,
password.digits,
],
});
patient.password = passwords;
await patient.save();
// const message = `Your password reset token are :- \n\n ${resetPasswordUrl} \n\nyour new password is:${password}\n\nIf you have not requested this email then, please ignore it.`;
try {
await sendEmail({
to: `${patient?.email}`, // Change to your recipient
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
subject: `Tavisa Password Recovery`,
html: `your new password is: <br/> <strong> ${passwords}</strong><br/><br/>If you have not requested this email then, please ignore it.`,
});
res.status(200).json({
success: true,
message: `Email sent to ${patient?.email} successfully`,
});
} catch (error) {
patient.resetPasswordToken = undefined;
patient.resetPasswordExpire = undefined;
await patient.save({ validateBeforeSave: false });
return res
.status(500)
.json({ message: "Something went wrong!", error: error?.message || "" });
}
};
export const completeRegistration = async (req, res) => { export const completeRegistration = async (req, res) => {
const { const {
mobileNumber, mobileNumber,

View File

@ -2,7 +2,7 @@ import express from "express";
const router = express.Router(); const router = express.Router();
// import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js"; // import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
import { EnterPatientDetails, EnterPersonalDetails, Otp, completeRegistration, create1RegistrationDetails, getAllPatient, register, verifyOtp } from "./PatientController.js"; import { EnterPatientDetails, EnterPersonalDetails, Otp, completeRegistration, create1RegistrationDetails, forgotPassword, getAllPatient, register, verifyOtp } from "./PatientController.js";
router.post('/register', register); router.post('/register', register);
@ -14,6 +14,9 @@ router.post('/rgstr_psrnl_details-p3', EnterPersonalDetails);
router.get('/getAll', getAllPatient); router.get('/getAll', getAllPatient);
router.post('/complete-registration', completeRegistration); router.post('/complete-registration', completeRegistration);
router.post('/forgot-password', forgotPassword);
router.get('/otp', Otp); router.get('/otp', Otp);