-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from Real-Dev-Squad/fix/authorization
Fix/authorization
- Loading branch information
Showing
5 changed files
with
130 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Boom from '@hapi/boom'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { findCalendarWithId, findEventWithId } from '../services/eventsService'; | ||
|
||
/** | ||
* Middleware to authorize if the calendar belongs to the user | ||
* | ||
* @param req {Object} - Express request object | ||
* @param res {Object} - Express response object | ||
* @param next {Function} - Express middleware function | ||
* @return {Object} - Returns unauthenticated object if token is invalid | ||
*/ | ||
const calendarBelongsToUser = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
): Promise<any | Response> => { | ||
try { | ||
const userId = req.userData.id; | ||
const calendarId = +(req.params.calendarId || req.body.calendarId); | ||
|
||
const calendarData = await findCalendarWithId(calendarId); | ||
|
||
if (calendarData === null) { | ||
logger.error('Calendar not found.'); | ||
return res.boom(Boom.notFound('Calendar not found')); | ||
} | ||
|
||
if (calendarData.ownerId !== userId) { | ||
logger.info('Calendar does not belong to user'); | ||
return res.boom(Boom.forbidden('Calendar does not belong to user')); | ||
} | ||
|
||
return next(); | ||
} catch (err: any) { | ||
logger.error('An internal server error occurred', { err: err.stack }); | ||
return res.boom( | ||
Boom.badImplementation('An internal server error occurred') | ||
); | ||
} | ||
}; | ||
|
||
const eventBelongsToUser = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
): Promise<any | Response> => { | ||
try { | ||
const { eventId } = req.params; | ||
const userId = req.userData.id; | ||
|
||
const eventData = await findEventWithId(+eventId); | ||
|
||
if (eventData === null) { | ||
logger.info('Event not found'); | ||
return res.boom(Boom.notFound()); | ||
} | ||
|
||
if (eventData.ownerId !== userId) { | ||
logger.info('Event does not belong to user'); | ||
return res.boom(Boom.forbidden('Event does not belong to user')); | ||
} | ||
|
||
return next(); | ||
} catch (err: any) { | ||
logger.error('An internal server error occurred', { err: err.stack }); | ||
return res.boom( | ||
Boom.badImplementation('An internal server error occurred') | ||
); | ||
} | ||
}; | ||
|
||
export { calendarBelongsToUser, eventBelongsToUser }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters