login and upload image

This commit is contained in:
pawan-dot 2024-07-02 14:59:03 +05:30
parent 77ae4b7c5c
commit d877929493
2 changed files with 112 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import Patient from './PatientModel.js'
import sendEmail, { 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"; import password from "secure-random-password";
import cloudinary from '../../Utils/cloudinary.js';
export const register = async (req, res) => { export const register = async (req, res) => {
let { name, countryCode, mobileNumber } = req.body; let { name, countryCode, mobileNumber } = req.body;
@ -28,10 +29,7 @@ export const register = async (req, res) => {
} }
await patient.save(); await patient.save();
await sendOtp(fullMobileNumber, `Your tavisa verification OTP is: ${otp}`); await sendOtp(fullMobileNumber, `Your tavisa verification OTP is: ${otp}`);
res.cookie('patientId', patient._id.toString(),
{ httpOnly: true }
); // 1 day in milliseconds
// { httpOnly: true, maxAge: 24 * 60 * 60 * 1000 }
return res.status(200).json({ patient, message: `OTP sent to your mobile number ${fullMobileNumber} successfully` }); return res.status(200).json({ patient, message: `OTP sent to your mobile number ${fullMobileNumber} successfully` });
@ -61,6 +59,10 @@ export const verifyOtp = async (req, res) => {
patient.otpExpires = undefined; patient.otpExpires = undefined;
await patient.save(); await patient.save();
res.cookie('patientId', patient._id.toString(),
{ httpOnly: true }
); // 1 day in milliseconds
// { httpOnly: true, maxAge: 24 * 60 * 60 * 1000 }
res.status(200).json({ patient, message: 'Mobile number verified successfully' }); res.status(200).json({ patient, message: 'Mobile number verified successfully' });
} catch (error) { } catch (error) {
@ -69,6 +71,37 @@ export const verifyOtp = async (req, res) => {
}); });
} }
}; };
// Login Patient
export const loginPatient = async (req, res) => {
const { email, password } = req.body;
// checking if patient has given password and email both
try {
if (!email || !password) {
return res.status(400).json({ message: "Please Enter Email & Password" });
}
const patient = await Patient.findOne({ email }).select("+password");
if (!patient) {
return res.status(400).json({ message: "Invalid Email or Password" });
}
const isPasswordMatched = await patient.comparePassword(password);
if (!isPasswordMatched) {
return res.status(400).json({ message: "Invalid Email or Password" });
}
await res.cookie('patientId', patient._id.toString(),
{ httpOnly: true }
);
return res.status(200).json({ success: true, message: "Login Successfully" });
} catch (error) {
return res
.status(500)
.json({ message: "Something went wrong!", error: error?.message || "" });
}
};
//get All Product //get All Product
export const getAllPatient = async (req, res) => { export const getAllPatient = async (req, res) => {
@ -280,7 +313,9 @@ export const EnterPersonalDetails = async (req, res) => {
export const forgotPassword = async (req, res, next) => { export const forgotPassword = async (req, res, next) => {
const patient = await Patient.findOne({ email: req.body.email }); const patient = await Patient.findOne({ email: req.body.email });
if (!req.body.email) {
return res.status(400).json({ message: 'please Enter Email!' });
}
if (!patient) { if (!patient) {
return res.status(404).json({ message: "Patient not found" }); return res.status(404).json({ message: "Patient not found" });
} }
@ -328,6 +363,71 @@ export const forgotPassword = async (req, res, next) => {
.json({ message: "Something went wrong!", error: error?.message || "" }); .json({ message: "Something went wrong!", error: error?.message || "" });
} }
}; };
//update Patient Profile Image
export const UploadProfileImage = async (req, res) => {
if (!req.files) {
return res.status(404).json({ message: "Please Select Image" });
}
// const patientId = req.cookies.patientId;
// console.log(req.params?.patientId)
if (!req.params?.patientId) {
return res.status(400).json({ message: 'Please Provide patientId!' });
}
let newPatientData = {};
try {
if (req.files) {
const patientImage = req.files?.avatar;
const patient = await Patient.findById(req.params?.patientId);
if (patient?.avatar === null) {
// console.log(patient?.avatar)
const imageId = patient?.avatar?.public_id;
await cloudinary.uploader.destroy(imageId)
}
const myCloud = await cloudinary.v2.uploader.upload(patientImage.tempFilePath,
{
folder: "tavisa/patient-image",
});
newPatientData.avatar = {
public_id: myCloud.public_id,
url: myCloud.secure_url,
};
let patientDetail = await Patient.findByIdAndUpdate(
req.params?.patientId, newPatientData,
{ new: true } // Return the updated document
);
// console.log(patientDetail)
// const patientDetail = await Patient.findByIdAndUpdate(patientId, newPatientData, {
// new: true,
// runValidators: true,
// useFindAndModify: false,
// });
return res.status(200).json({
success: true,
message: "Image Uploaded Successfully!",
patientDetail,
});
}
} catch (error) {
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,11 +2,14 @@ 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, forgotPassword, getAllPatient, register, verifyOtp } from "./PatientController.js"; import { EnterPatientDetails, EnterPersonalDetails, Otp, UploadProfileImage, completeRegistration, create1RegistrationDetails, forgotPassword, getAllPatient, loginPatient, register, verifyOtp } from "./PatientController.js";
router.post('/register', register); router.post('/register', register);
router.post('/verify-otp', verifyOtp); router.post('/verify-otp', verifyOtp);
router.post('/login', loginPatient);
router.post('/rgstr_details-p1', create1RegistrationDetails); router.post('/rgstr_details-p1', create1RegistrationDetails);
router.post('/rgstr_details-p2', EnterPatientDetails); router.post('/rgstr_details-p2', EnterPatientDetails);
router.post('/rgstr_psrnl_details-p3', EnterPersonalDetails); router.post('/rgstr_psrnl_details-p3', EnterPersonalDetails);
@ -14,7 +17,10 @@ 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.post('/forgot-password', forgotPassword);
router.post('/upload-image/:patientId', UploadProfileImage);
router.get('/otp', Otp); router.get('/otp', Otp);