diff --git a/backend/src/server.ts b/backend/src/server.ts index 4834bf6..7a3ab84 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -6,7 +6,7 @@ * Port: 4011 (configurable via PORT env var). */ -import { createServiceApp, startService } from '@bytelyst/fastify-core'; +import { createServiceApp, registerOptionalJwtContext, startService } from '@bytelyst/fastify-core'; import { timerRoutes } from './modules/timers/routes.js'; import { routineRoutes } from './modules/routines/routes.js'; import { householdRoutes } from './modules/households/routes.js'; @@ -37,15 +37,11 @@ const app = await createServiceApp({ metrics: true, }); -app.addHook('onRequest', async req => { - const auth = req.headers.authorization; - if (!auth?.startsWith('Bearer ')) return; - try { - const { payload } = await jwtVerify(auth.slice(7), jwtSecret, { issuer: 'bytelyst-platform' }); - req.jwtPayload = payload as unknown as JwtPayload; - } catch { - // Token invalid/expired — leave jwtPayload undefined. - } +await registerOptionalJwtContext(app, { + verifyToken: async (token: string) => { + const { payload } = await jwtVerify(token, jwtSecret, { issuer: 'bytelyst-platform' }); + return payload as unknown as JwtPayload; + }, }); await app.register(timerRoutes, { prefix: '/api' });