@@ -90,7 +90,9 @@ export async function buildApp():Promise<FastifyInstance> {
9090 // ─── Auth Decorator ───
9191 app . decorate ( 'authenticate' , async function ( request : any , reply : any ) {
9292 try {
93- await request . jwtVerify ( ) ;
93+ // Ensure the verified payload is assigned to `request.user` like the original plugin.
94+ const payload = await request . jwtVerify ( ) ;
95+ if ( payload ) request . user = payload ;
9496 } catch ( error ) {
9597 reply . status ( 401 ) . send ( { error : 'Unauthorized' } ) ;
9698 }
@@ -100,8 +102,8 @@ export async function buildApp():Promise<FastifyInstance> {
100102 await app . register ( authRoutes , { prefix : '/auth' } ) ;
101103 await app . register ( profileRoutes , { prefix : '/api/profiles' } ) ;
102104 await app . register ( cardRoutes , { prefix : '/api/cards' } ) ;
105+ // Public routes: standardise on `/api/u` (remove duplicate `/api/public`).
103106 await app . register ( publicRoutes , { prefix : '/api/u' } ) ;
104- await app . register ( publicRoutes , { prefix : '/api/public' } ) ;
105107 await app . register ( followRoutes , { prefix : '/api/follow' } ) ;
106108 await app . register ( connectRoutes , { prefix : '/api/connect' } ) ;
107109 await app . register ( analyticsRoutes , { prefix : '/api/analytics' } ) ;
@@ -118,5 +120,20 @@ type HealthResponse = {
118120app . get ( '/health' , async ( ) : Promise < HealthResponse > => {
119121 return { status : 'ok' } ;
120122} ) ;
123+
124+ // Centralized error handler: log and return a consistent 500 shape for unhandled errors.
125+ app . setErrorHandler ( ( error , request , reply ) => {
126+ app . log . error ( { err : error } , 'Unhandled error' ) ;
127+ // Also print to console to aid test diagnostics when logger is disabled.
128+ // This helps surface stack traces in CI/test runs.
129+ // eslint-disable-next-line no-console
130+ console . error ( error ) ;
131+ // If headers were already sent, fall back to default behaviour.
132+ if ( reply . sent ) {
133+ return ;
134+ }
135+ // Keep response shape consistent across the API.
136+ reply . status ( 500 ) . send ( { error : 'Internal server error' } ) ;
137+ } ) ;
121138 return app ;
122139}
0 commit comments