Skip to content

Commit

Permalink
fix(profile-update): return new token upon profile update
Browse files Browse the repository at this point in the history
- return new token upon profile update
- implement change full name endpoint

[Fixes #!7]
  • Loading branch information
jkarenzi committed Jul 17, 2024
1 parent 28d863b commit 11c21c7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
39 changes: 34 additions & 5 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Request, Response } from 'express';
import { IUser } from '../custom';
const { errorHandler } = require('../middleware/errorHandler');
const User = require('../models/User')
const bcrypt = require('bcrypt')
const {updatePasswordSchema, updateEmailSchema} = require('../middleware/validators/userSchema')
const {updatePasswordSchema, updateEmailSchema, updateFullNameSchema} = require('../middleware/validators/userSchema')
const cloudinary = require('../middleware/cloudinary')
const jwt = require('jsonwebtoken')
require('dotenv').config()


const jwtSecret = process.env.JWT_SECRET

interface cloudinaryUploadResult {
public_id: string,
Expand Down Expand Up @@ -72,9 +78,27 @@ const changeEmail = errorHandler(async (req: Request, res: Response) => {
}

const updatedDoc = await User.findByIdAndUpdate(userId,{email:formData.newEmail},{new:true})
return res.status(200).json({status:'success', message: 'Email successfully updated', data:{email:updatedDoc.email}})
const token = await jwt.sign({ updatedDoc }, jwtSecret, { expiresIn: '1h' });
return res.status(200).json({status:'success', message: 'Email successfully updated', data:{token}})
});

const changeFullName = errorHandler(async (req: Request, res: Response) => {
const formData = req.body
const userId = req.user!._id

const validationResult = updateFullNameSchema.validate(formData)

if(validationResult.error){
return res.status(400).json({status:'error', message:validationResult.error.details[0].message})
}

const updatedUser = await User.findByIdAndUpdate(userId,{fullName:formData.fullName},{new:true})
const token = await jwt.sign({ updatedUser }, jwtSecret, { expiresIn: '1h' });

return res.status(200).json({status:'success', data:{token}})

})

const changeProfileImg = errorHandler(async (req: Request, res: Response) => {
const userId = req.user!._id
const user = await User.findOne({_id:userId})
Expand Down Expand Up @@ -108,7 +132,9 @@ const changeProfileImg = errorHandler(async (req: Request, res: Response) => {
{new:true}
)

return res.status(200).json({status:'success', message:'Profile Image successfully updated', data:{profileImg:updatedUser.profileImg}})
const token = await jwt.sign({ updatedUser }, jwtSecret, { expiresIn: '1h' });

return res.status(200).json({status:'success', message:'Profile Image successfully updated', data:{token}})
});

const removeProfileImg = errorHandler(async (req: Request, res: Response) => {
Expand All @@ -121,8 +147,9 @@ const removeProfileImg = errorHandler(async (req: Request, res: Response) => {

const result = await cloudinary.uploader.destroy(user.profileImg.publicId,{invalidate:true})

let updatedUser: IUser;
if(result.result === 'ok'){
await User.findByIdAndUpdate(
updatedUser = await User.findByIdAndUpdate(
user._id,
{
profileImg: {
Expand All @@ -132,7 +159,8 @@ const removeProfileImg = errorHandler(async (req: Request, res: Response) => {
}
)

return res.status(204).json({})
const token = await jwt.sign({ updatedUser }, jwtSecret, { expiresIn: '1h' });
return res.status(200).json({status:'success', data:{token}})
}else{
return res.status(400).json({status:'error', message:'An error occured. Try again later'})
}
Expand All @@ -151,6 +179,7 @@ const deleteUser = errorHandler(async (req: Request, res: Response) => {
module.exports = {
getAllUsers,
getUser,
changeFullName,
changeEmail,
changePassword,
changeProfileImg,
Expand Down
13 changes: 12 additions & 1 deletion src/middleware/validators/userSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@ const updateEmailSchema = Joi.object({
newEmail: Joi.string().email().required(),
})

const updateFullNameSchema = Joi.object({
fullName: Joi.string()
.regex(/^[A-Za-z\s]{5,}$/)
.message(
'fullName can only contain letters and should have atleast 5 characters'
)
.required()
})


module.exports = {
updateEmailSchema,
updatePasswordSchema
updatePasswordSchema,
updateFullNameSchema
}
4 changes: 3 additions & 1 deletion src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const {
changePassword,
changeProfileImg,
removeProfileImg,
deleteUser
deleteUser,
changeFullName
} = require('../controllers/userController')
import { Router } from 'express';
const authenticateToken = require('../middleware/authenticateToken')
Expand All @@ -16,6 +17,7 @@ const userRouter = Router();

userRouter.use(authenticateToken)

userRouter.patch('/fullName', changeFullName)
userRouter.patch('/email', changeEmail)
userRouter.patch('/password', changePassword)
userRouter.route('/profileImg').patch(upload.fields([{name:'image'}]), changeProfileImg).delete(removeProfileImg)
Expand Down

0 comments on commit 11c21c7

Please sign in to comment.