diff --git a/apps/backend/src/routes/cards.ts b/apps/backend/src/routes/cards.ts index 8b8d6ff2..a20b4601 100644 --- a/apps/backend/src/routes/cards.ts +++ b/apps/backend/src/routes/cards.ts @@ -3,7 +3,7 @@ import { handleDbError } from '../utils/error.util.js'; import { hashIp } from '../utils/refreshToken'; import { createCardSchema ,updateCardSchema, addPlatformLinkSchema} from '../validations/card.validation'; -import type { CardResponse, UpdateCardBody, UpdatedCardResponse } from '../services/cardService'; +import type { CardResponse, UpdateCardBody,UpdatedCardResponse } from '../services/cardService'; import type { Card } from '@devcard/shared/src/types.js'; import type { CardVisibility } from '@prisma/client'; import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; @@ -62,15 +62,9 @@ function hasErrorCode( } export async function cardRoutes(app: FastifyInstance): Promise { - app.addHook('preHandler', async (request, reply) => { - const server = request.server; - if (typeof server?.authenticate === 'function') { await server.authenticate(request, reply); return } - if (typeof app.authenticate === 'function') { await app.authenticate(request, reply); return } - try { await request.jwtVerify() } catch (_e) { reply.status(401).send({ error: 'Unauthorized' }) } - }); - + // ─── List Cards ─── - app.get('/', async (request: FastifyRequest, reply: FastifyReply): Promise => { + app.get('/', {preHandler: [(req, reply) => app.authenticate(req, reply)] },async (request: FastifyRequest, reply: FastifyReply): Promise => { const userId = request.user.id; try { return await cardService.listCards(app, userId) @@ -80,7 +74,7 @@ export async function cardRoutes(app: FastifyInstance): Promise { }); // ─── Creates Card ─── - app.post('/', async (request: FastifyRequest<{ Body: CreateCardBody }>, reply: FastifyReply): Promise => { + app.post<{ Body: CreateCardBody }>('/', { preHandler: [(req, reply) => app.authenticate(req, reply)]}, async (request, reply): Promise => { const userId = request.user.id; const parsed = createCardSchema.safeParse(request.body); @@ -99,7 +93,7 @@ export async function cardRoutes(app: FastifyInstance): Promise { // ─── Update Card ─── - app.put('/:id', async (request: FastifyRequest<{ Params: CardParams; Body: UpdateCardBody }>, reply: FastifyReply): Promise => { + app.put<{ Params: CardParams; Body: UpdateCardBody }>('/:id', {preHandler: [(req, reply) => app.authenticate(req, reply)] }, async (request, reply): Promise => { const userId = request.user.id; const { id } = request.params; @@ -117,7 +111,7 @@ export async function cardRoutes(app: FastifyInstance): Promise { // ─── Delete Card ─── - app.delete('/:id', async (request: FastifyRequest<{ Params: CardParams }>, reply: FastifyReply): Promise => { + app.delete<{ Params: CardParams }>('/:id', { preHandler: [(req, reply) => app.authenticate(req, reply)]}, async (request, reply): Promise => { const userId = request.user.id; const { id } = request.params; @@ -139,7 +133,7 @@ export async function cardRoutes(app: FastifyInstance): Promise { }); // ─── Set Default Card ─── - app.put('/:id/default', async (request: FastifyRequest<{ Params: CardParams }>, reply: FastifyReply): Promise => { + app.put<{ Params: CardParams }>('/:id/default', {preHandler: [(req, reply) => app.authenticate(req, reply)]}, async (request, reply): Promise => { const userId = request.user.id; const { id } = request.params; diff --git a/apps/backend/src/routes/event.ts b/apps/backend/src/routes/event.ts index 1999f58b..5c24b4ff 100644 --- a/apps/backend/src/routes/event.ts +++ b/apps/backend/src/routes/event.ts @@ -58,7 +58,7 @@ type EventWithAttendees = { }[]; } -export async function eventRoutes(app:FastifyInstance): Promise { +export async function eventRoutes(app:FastifyInstance): Promise { app.post<{Body: { name: string; description?: string; startDate: string; location: string; endDate: string; isPublic?: boolean; }}>('/', { preHandler: [(req, reply) => app.authenticate(req, reply)] }, async (request, reply) => { const userId = request.user.id; const parsed = createEventSchema.safeParse(request.body); @@ -271,4 +271,4 @@ export async function eventRoutes(app:FastifyInstance): Promise { return response; }) -} \ No newline at end of file +} diff --git a/apps/backend/src/routes/nfc.ts b/apps/backend/src/routes/nfc.ts index 5cf13f0c..9dcb8088 100644 --- a/apps/backend/src/routes/nfc.ts +++ b/apps/backend/src/routes/nfc.ts @@ -1,6 +1,7 @@ -import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; import { z } from 'zod'; +import type { FastifyInstance} from 'fastify'; + type NfcPayloadResponse = { type: 'URI'; payload: string; @@ -10,33 +11,16 @@ const nfcQuerySchema = z.object({ card: z.string().uuid('Invalid card ID format').optional(), }); -export async function nfcRoutes(app: FastifyInstance) { - app.addHook('preHandler', async (request, reply) => { - const server = request.server as any; - if (typeof server?.authenticate === 'function') { - await server.authenticate(request, reply); - return; - } - if (typeof (app as any).authenticate === 'function') { - await (app as any).authenticate(request, reply); - return; - } - try { - await request.jwtVerify(); - } catch (e) { - reply.status(401).send({ error: 'Unauthorized' }); - } - }); +export async function nfcRoutes(app: FastifyInstance): Promise { + // GET /api/nfc/payload — returns NDEF URI payload for user's default DevCard URL // GET /api/nfc/payload?card= — returns payload for a specific card - app.get( - '/payload', - async ( - request: FastifyRequest<{ Querystring: { card?: string } }>, - reply: FastifyReply - ) => { - const userId = (request.user as any).id; + app.get<{ Querystring: { card?: string } }>( + '/payload', + { preHandler: [(req, reply) => app.authenticate(req, reply)] }, + async (request, reply) => { + const userId = request.user.id; // Validate query params with Zod const parseResult = nfcQuerySchema.safeParse(request.query); @@ -111,4 +95,4 @@ const payloadUrl = `${process.env.PUBLIC_APP_URL}/${safeUsername}${ return reply.send(response); } ); -} \ No newline at end of file +}