Skip to content

Commit

Permalink
Add user account management endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
hiteshchoudhary committed Dec 19, 2023
1 parent 11bccd7 commit 802aa88
Showing 1 changed file with 119 additions and 1 deletion.
120 changes: 119 additions & 1 deletion src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,127 @@ const refreshAccessToken = asyncHandler(async (req, res) => {

})

const changeCurrentPassword = asyncHandler(async(req, res) => {
const {oldPassword, newPassword} = req.body



const user = await User.findById(req.user?._id)
const isPasswordCorrect = await user.isPasswordCorrect(oldPassword)

if (!isPasswordCorrect) {
throw new ApiError(400, "Invalid old password")
}

user.password = newPassword
await user.save({validateBeforeSave: false})

return res
.status(200)
.json(new ApiResponse(200, {}, "Password changed successfully"))
})


const getCurrentUser = asyncHandler(async(req, res) => {
return res
.status(200)
.json(200, req.user, "current user fetched successfully")
})

const updateAccountDetails = asyncHandler(async(req, res) => {
const {fullName, email} = req.body

if (!fullName || !email) {
throw new ApiError(400, "All fields are required")
}

const user = User.findByIdAndUpdate(
req.user?._id,
{
$set: {
fullName,
email: email
}
},
{new: true}

).select("-password")

return res
.status(200)
.json(new ApiResponse(200, user, "Account details updated successfully"))
});

const updateUserAvatar = asyncHandler(async(req, res) => {
const avatarLocalPath = req.file?.path

if (!avatarLocalPath) {
throw new ApiError(400, "Avatar file is missing")
}

const avatar = await uploadOnCloudinary(avatarLocalPath)

if (!avatar.url) {
throw new ApiError(400, "Error while uploading on avatar")

}

const user = await User.findByIdAndUpdate(
req.user?._id,
{
$set:{
avatar: avatar.url
}
},
{new: true}
).select("-password")

return res
.status(200)
.json(
new ApiResponse(200, user, "Avatar image updated successfully")
)
})

const updateUserCoverImage = asyncHandler(async(req, res) => {
const coverImageLocalPath = req.file?.path

if (!coverImageLocalPath) {
throw new ApiError(400, "Cover image file is missing")
}

const coverImage = await uploadOnCloudinary(coverImageLocalPath)

if (!coverImage.url) {
throw new ApiError(400, "Error while uploading on avatar")

}

const user = await User.findByIdAndUpdate(
req.user?._id,
{
$set:{
coverImage: coverImage.url
}
},
{new: true}
).select("-password")

return res
.status(200)
.json(
new ApiResponse(200, user, "Cover image updated successfully")
)
})

export {
registerUser,
loginUser,
logoutUser,
refreshAccessToken
refreshAccessToken,
changeCurrentPassword,
getCurrentUser,
updateAccountDetails,
updateUserAvatar,
updateUserCoverImage
}

0 comments on commit 802aa88

Please sign in to comment.