updated the Auth apis

This commit is contained in:
ROSHAN GARG 2024-09-10 11:02:35 +05:30
parent aebdca98cf
commit 02c09fe74b
2 changed files with 9 additions and 30 deletions

View File

@ -13,12 +13,7 @@ const router = express.Router();
router.route("/rd-login").post(loginRD);
router.route("/rd-get-me").get(isAuthenticatedRD, getmyProfile);
router.post("/forgot-password", forgotPassword);
router.put(
"/rd-password/update",
isAuthenticatedRD,
ChangePasswordRD
);
router.put("/rd-password/update", isAuthenticatedRD, ChangePasswordRD);
router.patch(
"/rd-profile/update",
isAuthenticatedRD,

View File

@ -1,5 +1,9 @@
import RetailDistributor from "./RetailDistributorModel.js";
import validator from "validator";
import password from "secure-random-password";
import crypto from "crypto";
import catchAsyncErrors from "../../middlewares/catchAsyncErrors.js";
import sendEmail, { sendOtp } from "../../Utils/sendEmail.js";
export const loginRD = async (req, res) => {
const { email, password } = req.body;
@ -137,14 +141,9 @@ export const forgotPassword = async (req, res) => {
};
export const UpdateProfile = async (req, res) => {
const { name, email } = req.body;
const { name } = req.body; // Only expecting name from the request body
const userId = req.user._id; // Use the ID from params or authenticated user
// Validate email if provided
if (email && !validator.isEmail(email)) {
return res.status(400).json({ message: "Invalid email address" });
}
const userId = req.user._id; // User ID from authenticated user
try {
// Find the RetailDistributor by user ID
@ -155,27 +154,12 @@ export const UpdateProfile = async (req, res) => {
}
// Assuming you have an 'isVerified' field in your RetailDistributor schema
if (!retailDistributor.isVerified) {
return res
.status(400)
.json({ message: "Retail Distributor not verified" });
}
// Check if email is being changed and if it's already in use
if (email && email !== retailDistributor.email) {
const emailExists = await RetailDistributor.findOne({ email });
if (emailExists && emailExists._id.toString() !== userId) {
return res.status(400).json({
message:
"This Email ID is already in use by another Retail Distributor",
});
}
retailDistributor.email = email;
}
// Update name if provided
if (name) {
retailDistributor.name = name;
} else {
return res.status(400).json({ message: "Name is required" });
}
// Save the updated RetailDistributor