@@ -54,7 +54,7 @@ const setupCompleteRouteOptions = {
5454 additionalProperties : false ,
5555 properties : {
5656 setupCode : { type : 'string' , minLength : 1 , maxLength : 128 } ,
57- token : { type : 'string' , minLength : TOKEN_MIN_LENGTH , maxLength : TOKEN_MAX_LENGTH } ,
57+ token : { type : 'string' , minLength : 1 , maxLength : TOKEN_MAX_LENGTH } ,
5858 } ,
5959 } ,
6060 } ,
@@ -69,12 +69,16 @@ const rotateTokenRouteOptions = {
6969 additionalProperties : false ,
7070 properties : {
7171 currentToken : { type : 'string' , minLength : 1 , maxLength : TOKEN_MAX_LENGTH } ,
72- nextToken : { type : 'string' , minLength : TOKEN_MIN_LENGTH , maxLength : TOKEN_MAX_LENGTH } ,
72+ nextToken : { type : 'string' , minLength : 1 , maxLength : TOKEN_MAX_LENGTH } ,
7373 } ,
7474 } ,
7575 } ,
7676} as const ;
7777
78+ function isTokenValidationError ( err : unknown ) : boolean {
79+ return err instanceof Error && / ^ T o k e n m u s t b e / . test ( err . message ) ;
80+ }
81+
7882export async function authRoutes ( app : FastifyInstance ) {
7983 app . get ( '/api/setup/status' , async ( req ) => {
8084 const token = bearerToken ( req ) ;
@@ -115,7 +119,7 @@ export async function authRoutes(app: FastifyInstance) {
115119 }
116120 return { success : true , data : { authenticated : true } } ;
117121 } catch ( err ) {
118- reply . status ( 429 ) ;
122+ reply . status ( isTokenValidationError ( err ) ? 400 : 429 ) ;
119123 return { success : false , error : err instanceof Error ? err . message : 'Setup failed' } ;
120124 }
121125 } ) ;
@@ -149,7 +153,7 @@ export async function authRoutes(app: FastifyInstance) {
149153 return { success : false , error : 'Current token is invalid' } ;
150154 }
151155 } catch ( err ) {
152- reply . status ( 409 ) ;
156+ reply . status ( isTokenValidationError ( err ) ? 400 : 409 ) ;
153157 return { success : false , error : err instanceof Error ? err . message : 'Token rotation failed' } ;
154158 }
155159 return { success : true , data : { rotated : true } } ;
0 commit comments