diff --git a/server/src/frameworks/webserver/middlewares/imageUpload.ts b/server/src/frameworks/webserver/middlewares/imageUpload.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/frameworks/webserver/middlewares/roleCheckMiddleware.ts b/server/src/frameworks/webserver/middlewares/roleCheckMiddleware.ts index 460d604..a33f186 100644 --- a/server/src/frameworks/webserver/middlewares/roleCheckMiddleware.ts +++ b/server/src/frameworks/webserver/middlewares/roleCheckMiddleware.ts @@ -1,54 +1,17 @@ -// Admin Role Middleware -import { CustomRequest } from '../../../types/customRequest'; import { NextFunction,Response } from 'express'; +import { CustomRequest } from '../../../types/customRequest'; import AppError from '../../../utils/appError'; import HttpStatusCodes from '../../../constants/HttpStatusCodes'; -export const adminRoleCheckMiddleware = ( - req: CustomRequest, - res: Response, - next: NextFunction -) => { - const role = req.user?.role; - if (role === 'admin') { - // User has the admin role, allow access - next(); - } else { - // User does not have the admin role, deny access - throw new AppError('Unauthorized role', HttpStatusCodes.UNAUTHORIZED); - } -}; - -// Instructor Role Middleware -export const instructorRoleCheckMiddleware = ( - req: CustomRequest, - res: Response, - next: NextFunction -) => { - const role = req.user?.role - - if (role === 'instructor') { - // User has the instructor role, allow access - next(); - } else { - // User does not have the instructor role, deny access - throw new AppError('Unauthorized role, you are not a instructor', HttpStatusCodes.UNAUTHORIZED); - } +const roleCheckMiddleware = (roleToCheck: string) => { + return (req: CustomRequest, res: Response, next: NextFunction) => { + const role = req.user?.role; + if (role === roleToCheck) { + next(); + } else { + throw new AppError('Unauthorized role', HttpStatusCodes.UNAUTHORIZED); + } + }; }; -export const studentRoleCheckMiddleware = ( - req: CustomRequest, - res: Response, - next: NextFunction -) => { - const role = req.user?.role; - - - if (role === 'student') { - // User has the instructor role, allow access - next(); - } else { - // User does not have the instructor role, deny access - throw new AppError('Unauthorized role', HttpStatusCodes.UNAUTHORIZED); - } -}; +export default roleCheckMiddleware; \ No newline at end of file diff --git a/server/src/frameworks/webserver/routes/course.ts b/server/src/frameworks/webserver/routes/course.ts index 18d8a79..0a596a9 100644 --- a/server/src/frameworks/webserver/routes/course.ts +++ b/server/src/frameworks/webserver/routes/course.ts @@ -2,10 +2,7 @@ import express from 'express'; import courseController from '../../../adapters/controllers/courseController'; import { courseRepositoryMongodb } from '../../../frameworks/database/mongodb/repositories/courseReposMongoDb'; import { courseDbRepository } from '../../../app/repositories/courseDbRepository'; -import { - instructorRoleCheckMiddleware, - studentRoleCheckMiddleware -} from '../middlewares/roleCheckMiddleware'; +import roleCheckMiddleware from '../middlewares/roleCheckMiddleware'; import { cloudServiceInterface } from '../../../app/services/cloudServiceInterface'; import { s3Service } from '../../../frameworks/services/s3CloudService'; import upload from '../middlewares/multer'; @@ -46,7 +43,7 @@ const courseRouter = (redisClient: RedisClient) => { router.post( '/instructors/add-course', jwtAuthMiddleware, - instructorRoleCheckMiddleware, + roleCheckMiddleware('instructor'), upload.array('files'), controller.addCourse ); @@ -54,7 +51,7 @@ const courseRouter = (redisClient: RedisClient) => { router.put( '/instructors/edit-course/:courseId', jwtAuthMiddleware, - instructorRoleCheckMiddleware, + roleCheckMiddleware('instructor'), upload.array('files'), controller.editCourse ); @@ -70,14 +67,14 @@ const courseRouter = (redisClient: RedisClient) => { router.get( '/get-course-by-instructor', jwtAuthMiddleware, - instructorRoleCheckMiddleware, + roleCheckMiddleware('instructor'), controller.getCoursesByInstructor ); router.post( '/instructors/add-lesson/:courseId', jwtAuthMiddleware, - instructorRoleCheckMiddleware, + roleCheckMiddleware('instructor'), upload.array('media'), controller.addLesson ); @@ -85,7 +82,7 @@ const courseRouter = (redisClient: RedisClient) => { router.put( '/instructors/edit-lesson/:lessonId', jwtAuthMiddleware, - instructorRoleCheckMiddleware, + roleCheckMiddleware('instructor'), upload.array('media'), controller.editLesson ); @@ -142,7 +139,7 @@ const courseRouter = (redisClient: RedisClient) => { router.get( '/get-recommended-courses', jwtAuthMiddleware, - studentRoleCheckMiddleware, + roleCheckMiddleware('student'), controller.getRecommendedCourseByStudentInterest ); diff --git a/server/src/frameworks/webserver/routes/index.ts b/server/src/frameworks/webserver/routes/index.ts index 46c4272..6d76c30 100644 --- a/server/src/frameworks/webserver/routes/index.ts +++ b/server/src/frameworks/webserver/routes/index.ts @@ -5,7 +5,7 @@ import courseRouter from './course'; import instructorRouter from './instructor'; import { RedisClient } from '../../../app'; import jwtAuthMiddleware from '../middlewares/userAuth'; -import { adminRoleCheckMiddleware } from '../middlewares/roleCheckMiddleware'; +import roleCheckMiddleware from '../middlewares/roleCheckMiddleware'; import videoStreamRouter from './videoStream'; import refreshRouter from './refresh'; import paymentRouter from './payment'; @@ -18,7 +18,7 @@ const routes = (app: Application, redisClient: RedisClient) => { app.use( '/api/admin', jwtAuthMiddleware, - adminRoleCheckMiddleware, + roleCheckMiddleware('admin'), adminRouter() ); app.use('/api/category', categoryRouter()); diff --git a/server/src/frameworks/webserver/routes/instructor.ts b/server/src/frameworks/webserver/routes/instructor.ts index 8bac32e..bc8a880 100644 --- a/server/src/frameworks/webserver/routes/instructor.ts +++ b/server/src/frameworks/webserver/routes/instructor.ts @@ -8,7 +8,7 @@ import { authService } from '../../../frameworks/services/authService'; import { authServiceInterface } from '../../../app/services/authServicesInterface'; import { cloudServiceInterface } from '../../../app/services/cloudServiceInterface'; import { s3Service } from '../../../frameworks/services/s3CloudService'; -import { instructorRoleCheckMiddleware } from '../middlewares/roleCheckMiddleware'; +import roleCheckMiddleware from '../middlewares/roleCheckMiddleware'; import jwtAuthMiddleware from '../middlewares/userAuth'; import upload from '../middlewares/multer'; import { courseDbRepository } from '../../../app/repositories/courseDbRepository'; @@ -57,7 +57,7 @@ const instructorRouter = () => { router.get( '/get-instructor-details', jwtAuthMiddleware, - instructorRoleCheckMiddleware, + roleCheckMiddleware('instructor'), controller.getInstructorDetails ); @@ -65,14 +65,14 @@ const instructorRouter = () => { '/update-profile', jwtAuthMiddleware, upload.single('image'), - instructorRoleCheckMiddleware, + roleCheckMiddleware('instructor'), controller.updateProfile ); router.patch( '/change-password', jwtAuthMiddleware, - instructorRoleCheckMiddleware, + roleCheckMiddleware('instructor'), controller.changePassword ); diff --git a/server/src/frameworks/webserver/routes/student.ts b/server/src/frameworks/webserver/routes/student.ts index d99a7ee..44db0d0 100644 --- a/server/src/frameworks/webserver/routes/student.ts +++ b/server/src/frameworks/webserver/routes/student.ts @@ -14,8 +14,7 @@ import { cacheRepositoryInterface } from '../../../app/repositories/cachedRepoIn import jwtAuthMiddleware from '../middlewares/userAuth'; import { contactDbInterface } from '../../../app/repositories/contactDbRepository'; import { contactRepositoryMongodb } from '../../../frameworks/database/mongodb/repositories/contactsRepoMongoDb'; -import { adminRoleCheckMiddleware } from '../middlewares/roleCheckMiddleware'; -import { adminRepoMongoDb } from '@src/frameworks/database/mongodb/repositories/adminRepoMongoDb'; +import roleCheckMiddleware from '../middlewares/roleCheckMiddleware'; const studentRouter = (redisClient: RedisClient) => { const router = express.Router(); @@ -57,21 +56,21 @@ const studentRouter = (redisClient: RedisClient) => { router.patch( '/block-student/:studentId', jwtAuthMiddleware, - adminRoleCheckMiddleware, + roleCheckMiddleware('admin'), controller.blockStudent ); router.patch( '/unblock-student/:studentId', jwtAuthMiddleware, - adminRoleCheckMiddleware, + roleCheckMiddleware('admin'), controller.unblockStudent ); router.get( '/get-all-blocked-students', jwtAuthMiddleware, - adminRoleCheckMiddleware, + roleCheckMiddleware('admin'), controller.getAllBlockedStudents );