@@ -3,7 +3,7 @@ import { handleDbError } from '../utils/error.util.js';
33import { hashIp } from '../utils/refreshToken' ;
44import { createCardSchema , updateCardSchema , addPlatformLinkSchema } from '../validations/card.validation' ;
55
6- import type { CardResponse , UpdateCardBody , UpdatedCardResponse } from '../services/cardService' ;
6+ import type { CardResponse , UpdateCardBody , UpdatedCardResponse } from '../services/cardService' ;
77import type { Card } from '@devcard/shared/src/types.js' ;
88import type { CardVisibility } from '@prisma/client' ;
99import type { FastifyInstance , FastifyRequest , FastifyReply } from 'fastify' ;
@@ -62,15 +62,9 @@ function hasErrorCode(
6262}
6363
6464export async function cardRoutes ( app : FastifyInstance ) : Promise < void > {
65- app . addHook ( 'preHandler' , async ( request , reply ) => {
66- const server = request . server ;
67- if ( typeof server ?. authenticate === 'function' ) { await server . authenticate ( request , reply ) ; return }
68- if ( typeof app . authenticate === 'function' ) { await app . authenticate ( request , reply ) ; return }
69- try { await request . jwtVerify ( ) } catch ( _e ) { reply . status ( 401 ) . send ( { error : 'Unauthorized' } ) }
70- } ) ;
71-
65+
7266 // ─── List Cards ───
73- app . get ( '/' , async ( request : FastifyRequest , reply : FastifyReply ) : Promise < CardResponse [ ] | void > => {
67+ app . get ( '/' , { preHandler : [ ( req , reply ) => app . authenticate ( req , reply ) ] } , async ( request : FastifyRequest , reply : FastifyReply ) : Promise < CardResponse [ ] | void > => {
7468 const userId = request . user . id ;
7569 try {
7670 return await cardService . listCards ( app , userId )
@@ -80,7 +74,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
8074 } ) ;
8175
8276 // ─── Creates Card ───
83- app . post ( '/' , async ( request : FastifyRequest < { Body : CreateCardBody } > , reply : FastifyReply ) : Promise < Card | void > => {
77+ app . post < { Body : CreateCardBody } > ( '/' , { preHandler : [ ( req , reply ) => app . authenticate ( req , reply ) ] } , async ( request , reply ) : Promise < Card | void > => {
8478 const userId = request . user . id ;
8579 const parsed = createCardSchema . safeParse ( request . body ) ;
8680
@@ -99,7 +93,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
9993
10094 // ─── Update Card ───
10195
102- app . put ( '/:id' , async ( request : FastifyRequest < { Params : CardParams ; Body : UpdateCardBody } > , reply : FastifyReply ) : Promise < UpdatedCardResponse > => {
96+ app . put < { Params : CardParams ; Body : UpdateCardBody } > ( '/:id' , { preHandler : [ ( req , reply ) => app . authenticate ( req , reply ) ] } , async ( request , reply ) : Promise < UpdatedCardResponse > => {
10397 const userId = request . user . id ;
10498 const { id } = request . params ;
10599
@@ -117,7 +111,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
117111
118112 // ─── Delete Card ───
119113
120- app . delete ( '/:id' , async ( request : FastifyRequest < { Params : CardParams } > , reply : FastifyReply ) : Promise < void > => {
114+ app . delete < { Params : CardParams } > ( '/:id' , { preHandler : [ ( req , reply ) => app . authenticate ( req , reply ) ] } , async ( request , reply ) : Promise < void > => {
121115 const userId = request . user . id ;
122116 const { id } = request . params ;
123117
@@ -139,7 +133,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
139133 } ) ;
140134
141135 // ─── Set Default Card ───
142- app . put ( '/:id/default' , async ( request : FastifyRequest < { Params : CardParams } > , reply : FastifyReply ) : Promise < object | void > => {
136+ app . put < { Params : CardParams } > ( '/:id/default' , { preHandler : [ ( req , reply ) => app . authenticate ( req , reply ) ] } , async ( request , reply ) : Promise < object | void > => {
143137 const userId = request . user . id ;
144138 const { id } = request . params ;
145139
0 commit comments