update user basic details and password done
This commit is contained in:
parent
e394325eb7
commit
062c6d84d3
@ -1,16 +1,16 @@
|
|||||||
import ErrorHander from "../../Utils/errorhander.js"
|
import ErrorHander from "../../Utils/errorhander.js";
|
||||||
import catchAsyncErrors from "../../middlewares/catchAsyncErrors.js"
|
import catchAsyncErrors from "../../middlewares/catchAsyncErrors.js";
|
||||||
import User from "./userModel.js"
|
import User from "./userModel.js";
|
||||||
import sendToken from "../../Utils/jwtToken.js"
|
import sendToken from "../../Utils/jwtToken.js";
|
||||||
import sendEmail from "../../Utils/sendEmail.js"
|
import sendEmail from "../../Utils/sendEmail.js";
|
||||||
import crypto from "crypto"
|
import crypto from "crypto";
|
||||||
import cloudinary from "cloudinary"
|
import cloudinary from "cloudinary";
|
||||||
import password from 'secure-random-password'
|
import password from "secure-random-password";
|
||||||
// 1.Register a User
|
// 1.Register a User
|
||||||
export const registerUser = async (req, res) => {
|
export const registerUser = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { name, email, password, phone } = req.body;
|
const { name, email, password, phone } = req.body;
|
||||||
let findUser = await User.findOne({ email })
|
let findUser = await User.findOne({ email });
|
||||||
if (findUser) {
|
if (findUser) {
|
||||||
return res
|
return res
|
||||||
.status(400)
|
.status(400)
|
||||||
@ -18,14 +18,16 @@ export const registerUser = async (req, res) => {
|
|||||||
}
|
}
|
||||||
if (req.files) {
|
if (req.files) {
|
||||||
const files = req.files.avatar;
|
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",
|
folder: "ATP/user-image",
|
||||||
},
|
},
|
||||||
function (error, result) { (result, error) });
|
function (error, result) {
|
||||||
|
result, error;
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const user = await User.create({
|
const user = await User.create({
|
||||||
name,
|
name,
|
||||||
@ -39,12 +41,8 @@ export const registerUser = async (req, res) => {
|
|||||||
});
|
});
|
||||||
sendToken(user, 201, res);
|
sendToken(user, 201, res);
|
||||||
} catch (e) {
|
} 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
|
// 2.Login User
|
||||||
@ -54,20 +52,19 @@ export const loginUser = async (req, res, next) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (!email || !password) {
|
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");
|
const user = await User.findOne({ email }).select("+password");
|
||||||
|
|
||||||
if (!user) {
|
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);
|
const isPasswordMatched = await user.comparePassword(password);
|
||||||
|
|
||||||
if (!isPasswordMatched) {
|
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);
|
sendToken(user, 200, res);
|
||||||
@ -76,10 +73,8 @@ export const loginUser = async (req, res, next) => {
|
|||||||
.status(500)
|
.status(500)
|
||||||
.json({ message: "Something went wrong!", error: error?.message || "" });
|
.json({ message: "Something went wrong!", error: error?.message || "" });
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// 3.Logout User
|
// 3.Logout User
|
||||||
export const logout = catchAsyncErrors(async (req, res, next) => {
|
export const logout = catchAsyncErrors(async (req, res, next) => {
|
||||||
res.cookie("token", null, {
|
res.cookie("token", null, {
|
||||||
@ -93,7 +88,6 @@ export const logout = catchAsyncErrors(async (req, res, next) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// 4.Forgot Password
|
// 4.Forgot Password
|
||||||
|
|
||||||
export const forgotPassword = async (req, res, next) => {
|
export const forgotPassword = async (req, res, next) => {
|
||||||
@ -101,10 +95,9 @@ export const forgotPassword = async (req, res, next) => {
|
|||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return res.status(404).json({ message: "User not found" });
|
return res.status(404).json({ message: "User not found" });
|
||||||
|
|
||||||
}
|
}
|
||||||
// Get ResetPassword Token
|
// Get ResetPassword Token
|
||||||
const resetToken = user.getResetPasswordToken();//call function
|
const resetToken = user.getResetPasswordToken(); //call function
|
||||||
|
|
||||||
//save database reset token
|
//save database reset token
|
||||||
await user.save({ validateBeforeSave: false });
|
await user.save({ validateBeforeSave: false });
|
||||||
@ -115,23 +108,21 @@ export const forgotPassword = async (req, res, next) => {
|
|||||||
{ characters: password.upper, exactly: 1 },
|
{ characters: password.upper, exactly: 1 },
|
||||||
{ characters: password.symbols, exactly: 1 },
|
{ characters: password.symbols, exactly: 1 },
|
||||||
password.lower,
|
password.lower,
|
||||||
password.digits]
|
password.digits,
|
||||||
})
|
],
|
||||||
console.log(passwords);
|
});
|
||||||
|
|
||||||
user.password = passwords;
|
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.`;
|
// 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 {
|
try {
|
||||||
|
|
||||||
await sendEmail({
|
await sendEmail({
|
||||||
|
|
||||||
to: `${user.email}`, // Change to your recipient
|
to: `${user.email}`, // Change to your recipient
|
||||||
|
|
||||||
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
||||||
|
|
||||||
subject: `The-solar-sign Password Recovery`,
|
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({
|
res.status(200).json({
|
||||||
@ -144,10 +135,11 @@ export const forgotPassword = async (req, res, next) => {
|
|||||||
|
|
||||||
await user.save({ validateBeforeSave: false });
|
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
|
// 5.Reset Password
|
||||||
export const resetPassword = catchAsyncErrors(async (req, res, next) => {
|
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)
|
// 7.Get single user (admin)
|
||||||
export const getSingleUser = catchAsyncErrors(async (req, res, next) => {
|
export const getSingleUser = catchAsyncErrors(async (req, res, next) => {
|
||||||
if (!req.params.id) {
|
if (!req.params.id) {
|
||||||
return next(
|
return next(new ErrorHander(`please send User ID`, 404));
|
||||||
new ErrorHander(`please send User ID`, 404)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
const user = await User.findById(req.params.id);
|
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) => {
|
export const updateProfile = catchAsyncErrors(async (req, res, next) => {
|
||||||
const newUserData = {
|
const newUserData = {
|
||||||
name: req.body.name,
|
name: req.body.name,
|
||||||
phone: req.body.phone,
|
|
||||||
email: req.body.email,
|
email: req.body.email,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (req.files) {
|
// if (req.files) {
|
||||||
const userImage = req.files?.avatar;
|
// const userImage = req.files?.avatar;
|
||||||
const user = await User.findById(req.user.id);
|
// const user = await User.findById(req.user.id);
|
||||||
|
|
||||||
|
// if (user?.avatar) {
|
||||||
|
// const imageId = user?.avatar?.public_id;
|
||||||
|
|
||||||
if (user?.avatar) {
|
// await cloudinary.uploader.destroy(imageId)
|
||||||
const imageId = user?.avatar?.public_id;
|
// }
|
||||||
|
|
||||||
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, {
|
const user = await User.findByIdAndUpdate(req.user.id, newUserData, {
|
||||||
new: true,
|
new: true,
|
||||||
runValidators: true,
|
runValidators: true,
|
||||||
useFindAndModify: false,
|
useFindAndModify: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
user
|
user,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// 9.Get all users(admin)
|
// 9.Get all users(admin)
|
||||||
export const getAllUser = catchAsyncErrors(async (req, res, next) => {
|
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({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
|
Loading…
Reference in New Issue
Block a user