diff --git a/src/server/utils/handler.ts b/src/server/utils/handler.ts index 79e1f435..5f0baa08 100644 --- a/src/server/utils/handler.ts +++ b/src/server/utils/handler.ts @@ -138,34 +138,27 @@ export const defineMetricsHandler = < handler: MetricsHandler ) => { return defineEventHandler(async (event) => { - const auth = getHeader(event, 'Authorization'); - - if (!auth) { - throw createError({ - statusCode: 401, - statusMessage: 'Unauthorized', - }); - } + const metricsConfig = await Database.general.getMetricsConfig(); - const [method, value] = auth.split(' '); + if (metricsConfig.password) { + const auth = getHeader(event, 'Authorization'); - if (method !== 'Bearer' || !value) { - throw createError({ - statusCode: 401, - statusMessage: 'Bearer Auth required', - }); - } + if (!auth) { + throw createError({ + statusCode: 401, + statusMessage: 'Unauthorized', + }); + } - const metricsConfig = await Database.general.getMetricsConfig(); + const [method, value] = auth.split(' '); - if (metricsConfig[type] !== true) { - throw createError({ - statusCode: 400, - statusMessage: 'Metrics not enabled', - }); - } + if (method !== 'Bearer' || !value) { + throw createError({ + statusCode: 401, + statusMessage: 'Bearer Auth required', + }); + } - if (metricsConfig.password) { const tokenValid = await isPasswordValid(value, metricsConfig.password); if (!tokenValid) { @@ -176,6 +169,13 @@ export const defineMetricsHandler = < } } + if (metricsConfig[type] !== true) { + throw createError({ + statusCode: 400, + statusMessage: 'Metrics not enabled', + }); + } + return await handler({ event }); }); };