diff --git a/resources/Patients/PatientController.js b/resources/Patients/PatientController.js index 4cbf08f..2fb3758 100644 --- a/resources/Patients/PatientController.js +++ b/resources/Patients/PatientController.js @@ -2,8 +2,9 @@ import crypto from 'crypto'; import Patient from './PatientModel.js' -import { sendOtp } from '../../Utils/sendEmail.js'; +import sendEmail, { sendOtp } from '../../Utils/sendEmail.js'; import validator from "validator"; +import password from "secure-random-password"; export const register = async (req, res) => { 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:
${passwords}

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) => { const { mobileNumber, diff --git a/resources/Patients/PatientRoute.js b/resources/Patients/PatientRoute.js index dbdefc7..2d8b089 100644 --- a/resources/Patients/PatientRoute.js +++ b/resources/Patients/PatientRoute.js @@ -2,7 +2,7 @@ import express from "express"; const router = express.Router(); // 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); @@ -14,6 +14,9 @@ router.post('/rgstr_psrnl_details-p3', EnterPersonalDetails); router.get('/getAll', getAllPatient); router.post('/complete-registration', completeRegistration); +router.post('/forgot-password', forgotPassword); + + router.get('/otp', Otp);