update user basic details and password done

This commit is contained in:
print-signs 2023-11-29 19:37:13 +05:30
parent e394325eb7
commit 062c6d84d3

View File

@ -1,16 +1,16 @@
import ErrorHander from "../../Utils/errorhander.js"
import catchAsyncErrors from "../../middlewares/catchAsyncErrors.js"
import User from "./userModel.js"
import sendToken from "../../Utils/jwtToken.js"
import sendEmail from "../../Utils/sendEmail.js"
import crypto from "crypto"
import cloudinary from "cloudinary"
import password from 'secure-random-password'
import ErrorHander from "../../Utils/errorhander.js";
import catchAsyncErrors from "../../middlewares/catchAsyncErrors.js";
import User from "./userModel.js";
import sendToken from "../../Utils/jwtToken.js";
import sendEmail from "../../Utils/sendEmail.js";
import crypto from "crypto";
import cloudinary from "cloudinary";
import password from "secure-random-password";
// 1.Register a User
export const registerUser = async (req, res) => {
try {
const { name, email, password, phone } = req.body;
let findUser = await User.findOne({ email })
let findUser = await User.findOne({ email });
if (findUser) {
return res
.status(400)
@ -18,14 +18,16 @@ export const registerUser = async (req, res) => {
}
if (req.files) {
const files = req.files.avatar;
const myCloud = await cloudinary.uploader.upload(files.tempFilePath, {
const myCloud = await cloudinary.uploader.upload(
files.tempFilePath,
{
folder: "ATP/user-image",
},
function (error, result) { (result, error) });
function (error, result) {
result, error;
}
);
}
const user = await User.create({
name,
@ -39,12 +41,8 @@ export const registerUser = async (req, res) => {
});
sendToken(user, 201, res);
} catch (e) {
return res
.status(400)
.json({ success: false, message: e.message });
return res.status(400).json({ success: false, message: e.message });
}
};
// 2.Login User
@ -54,20 +52,19 @@ export const loginUser = async (req, res, next) => {
try {
if (!email || !password) {
return res.status(400).json({ message: 'Please Enter Email & Password' });
return res.status(400).json({ message: "Please Enter Email & Password" });
}
const user = await User.findOne({ email }).select("+password");
if (!user) {
return res.status(400).json({ message: 'Invalid Email or Password' });
return res.status(400).json({ message: "Invalid Email or Password" });
}
const isPasswordMatched = await user.comparePassword(password);
if (!isPasswordMatched) {
return res.status(400).json({ message: 'Invalid Email or Password' });
return res.status(400).json({ message: "Invalid Email or Password" });
}
sendToken(user, 200, res);
@ -76,10 +73,8 @@ export const loginUser = async (req, res, next) => {
.status(500)
.json({ message: "Something went wrong!", error: error?.message || "" });
}
};
// 3.Logout User
export const logout = catchAsyncErrors(async (req, res, next) => {
res.cookie("token", null, {
@ -93,7 +88,6 @@ export const logout = catchAsyncErrors(async (req, res, next) => {
});
});
// 4.Forgot Password
export const forgotPassword = async (req, res, next) => {
@ -101,10 +95,9 @@ export const forgotPassword = async (req, res, next) => {
if (!user) {
return res.status(404).json({ message: "User not found" });
}
// Get ResetPassword Token
const resetToken = user.getResetPasswordToken();//call function
const resetToken = user.getResetPasswordToken(); //call function
//save database reset token
await user.save({ validateBeforeSave: false });
@ -115,23 +108,21 @@ export const forgotPassword = async (req, res, next) => {
{ characters: password.upper, exactly: 1 },
{ characters: password.symbols, exactly: 1 },
password.lower,
password.digits]
})
console.log(passwords);
password.digits,
],
});
user.password = passwords;
await user.save()
await user.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: `${user.email}`, // Change to your recipient
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
subject: `The-solar-sign Password Recovery`,
html: `your new password is: <br/> <strong> ${passwords}</strong><br/><br/>If you have not requested this email then, please ignore it.`
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({
@ -144,10 +135,11 @@ export const forgotPassword = async (req, res, next) => {
await user.save({ validateBeforeSave: false });
return res.status(500).json({ message: "Something went wrong!", error: error?.message || "" });
return res
.status(500)
.json({ message: "Something went wrong!", error: error?.message || "" });
}
}
};
// 5.Reset Password
export const resetPassword = catchAsyncErrors(async (req, res, next) => {
@ -197,9 +189,7 @@ export const getUserDetails = catchAsyncErrors(async (req, res, next) => {
// 7.Get single user (admin)
export const getSingleUser = catchAsyncErrors(async (req, res, next) => {
if (!req.params.id) {
return next(
new ErrorHander(`please send User ID`, 404)
);
return next(new ErrorHander(`please send User ID`, 404));
}
const user = await User.findById(req.params.id);
@ -239,52 +229,47 @@ export const updatePassword = catchAsyncErrors(async (req, res, next) => {
export const updateProfile = catchAsyncErrors(async (req, res, next) => {
const newUserData = {
name: req.body.name,
phone: req.body.phone,
email: req.body.email,
};
if (req.files) {
const userImage = req.files?.avatar;
const user = await User.findById(req.user.id);
// if (req.files) {
// const userImage = req.files?.avatar;
// const user = await User.findById(req.user.id);
// if (user?.avatar) {
// const imageId = user?.avatar?.public_id;
if (user?.avatar) {
const imageId = user?.avatar?.public_id;
// await cloudinary.uploader.destroy(imageId)
// }
await cloudinary.uploader.destroy(imageId)
}
// const myCloud = await cloudinary.v2.uploader.upload(userImage.tempFilePath,
// {
// folder: "ATP/user-image",
// });
// newUserData.avatar = {
// public_id: myCloud.public_id,
// url: myCloud.secure_url,
// };
// }
const myCloud = await cloudinary.v2.uploader.upload(userImage.tempFilePath,
{
folder: "ATP/user-image",
});
newUserData.avatar = {
public_id: myCloud.public_id,
url: myCloud.secure_url,
};
}
const user = await User.findByIdAndUpdate(req.user.id, newUserData, {
new: true,
runValidators: true,
useFindAndModify: false,
});
res.status(200).json({
return res.status(200).json({
success: true,
user
user,
});
});
// 9.Get all users(admin)
export const getAllUser = catchAsyncErrors(async (req, res, next) => {
const users = await User.find()//.select('-role');
const users = await User.find(); //.select('-role');
res.status(200).json({
success: true,