refactor(backend): use shared optional jwt context
This commit is contained in:
parent
f9439f4519
commit
8cc21d8586
@ -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' });
|
||||
|
||||
Loading…
Reference in New Issue
Block a user