From 8bbbee731bd1bb1c37c7b03bb76650782d403d55 Mon Sep 17 00:00:00 2001 From: juancarmore Date: Mon, 28 Apr 2025 12:19:07 +0200 Subject: [PATCH] backend: Rename configureTokenAuth middleware to configureParticipantTokenAuth and improve error handling --- backend/src/middlewares/participant.middleware.ts | 15 ++++++++++++--- backend/src/routes/participant.routes.ts | 6 +++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/backend/src/middlewares/participant.middleware.ts b/backend/src/middlewares/participant.middleware.ts index ab12574..4be74f8 100644 --- a/backend/src/middlewares/participant.middleware.ts +++ b/backend/src/middlewares/participant.middleware.ts @@ -1,6 +1,7 @@ import { AuthMode, ParticipantOptions, ParticipantRole, UserRole } from '@typings-ce'; import { NextFunction, Request, Response } from 'express'; import { container } from '../config/index.js'; +import { OpenViduMeetError } from '../models/error.model.js'; import { LoggerService, MeetStorageService, RoomService } from '../services/index.js'; import { allowAnonymous, tokenAndRoleValidator, withAuth } from './auth.middleware.js'; @@ -11,7 +12,7 @@ import { allowAnonymous, tokenAndRoleValidator, withAuth } from './auth.middlewa * - If the authentication mode is ALL_USERS, configure user authentication. * - Otherwise, allow anonymous access. */ -export const configureTokenAuth = async (req: Request, res: Response, next: NextFunction) => { +export const configureParticipantTokenAuth = async (req: Request, res: Response, next: NextFunction) => { const logger = container.get(LoggerService); const globalPrefService = container.get(MeetStorageService); const roomService = container.get(RoomService); @@ -22,8 +23,16 @@ export const configureTokenAuth = async (req: Request, res: Response, next: Next const { roomId, secret } = req.body as ParticipantOptions; role = await roomService.getRoomRoleBySecret(roomId, secret); } catch (error) { - logger.error('Error getting room secret role', error); - return res.status(500).json({ message: 'Internal server error' }); + logger.error('Error getting room role by secret', error); + + if (error instanceof OpenViduMeetError) { + return res.status(error.statusCode).json({ name: error.name, message: error.message }); + } else { + return res.status(500).json({ + name: 'Participant Error', + message: 'Internal server error. Participant operation failed' + }); + } } let authMode: AuthMode; diff --git a/backend/src/routes/participant.routes.ts b/backend/src/routes/participant.routes.ts index 7a25c64..6e836be 100644 --- a/backend/src/routes/participant.routes.ts +++ b/backend/src/routes/participant.routes.ts @@ -1,7 +1,7 @@ import bodyParser from 'body-parser'; import { Router } from 'express'; import * as participantCtrl from '../controllers/participant.controller.js'; -import { configureTokenAuth, validateParticipantTokenRequest } from '../middlewares/index.js'; +import { configureParticipantTokenAuth, validateParticipantTokenRequest } from '../middlewares/index.js'; export const internalParticipantRouter = Router(); internalParticipantRouter.use(bodyParser.urlencoded({ extended: true })); @@ -11,12 +11,12 @@ internalParticipantRouter.use(bodyParser.json()); internalParticipantRouter.post( '/token', validateParticipantTokenRequest, - configureTokenAuth, + configureParticipantTokenAuth, participantCtrl.generateParticipantToken ); internalParticipantRouter.post( '/token/refresh', validateParticipantTokenRequest, - configureTokenAuth, + configureParticipantTokenAuth, participantCtrl.refreshParticipantToken );