@@ -126,6 +126,7 @@ const verifyWebAuthnRegistration = async (req: Request, res: Response) => {
126126 }
127127
128128 if ( ! verifiedUser . email || ! attestationResponse ) {
129+ logger . warn ( 'Missing verified user email or attestation response' ) ;
129130 await AuthEvent . create ( {
130131 user_id : null ,
131132 type : 'registration_failed' ,
@@ -154,6 +155,7 @@ const verifyWebAuthnRegistration = async (req: Request, res: Response) => {
154155
155156 const expectedChallenge = user . challenge ;
156157 if ( ! expectedChallenge ) {
158+ logger . error ( 'Unexpected user challegnge supplied.' ) ;
157159 await AuthEvent . create ( {
158160 user_id : user . id ,
159161 type : 'registration_suspicous' ,
@@ -223,6 +225,7 @@ const verifyWebAuthnRegistration = async (req: Request, res: Response) => {
223225 await user . update ( {
224226 challenge : null ,
225227 lastLogin : new Date ( ) ,
228+ verified : true ,
226229 } ) ;
227230
228231 logger . info ( `Passkey credential saved successfully for user: ${ verifiedUser . email } ` ) ;
@@ -253,11 +256,6 @@ const verifyWebAuthnRegistration = async (req: Request, res: Response) => {
253256
254257 const token = await signAccessToken ( session . id , user . id , user . roles ) ;
255258
256- user . challenge = '' ;
257- user . verified = true ;
258-
259- await user . save ( ) ;
260-
261259 if ( token && refreshToken ) {
262260 await AuthEvent . create ( {
263261 user_id : user . id ,
@@ -287,9 +285,10 @@ const verifyWebAuthnRegistration = async (req: Request, res: Response) => {
287285 refreshTtl : parseDurationToSeconds ( refresh_token_ttl || '1h' ) ,
288286 } ) ;
289287 }
288+ return res . status ( 500 ) . json ( { error : 'Unknown error verifying passkey' } ) ;
290289 } catch ( err ) {
291290 logger . error ( `Error in verifyWebAuthnRegistration: ${ err } ` ) ;
292- return res . status ( 500 ) . json ( { message : 'Unknown error verifying passkey' } ) ;
291+ return res . status ( 500 ) . json ( { error : 'Unknown error verifying passkey' } ) ;
293292 }
294293} ;
295294
@@ -338,7 +337,7 @@ const generateWebAuthn = async (req: Request, res: Response) => {
338337 user_agent : req . headers [ 'user-agent' ] ,
339338 metadata : { reason : 'No credentials' } ,
340339 } ) ;
341- logger . info ( 'Valid user with no credentials' ) ;
340+ logger . error ( 'Valid user with no credentials' ) ;
342341 return res . status ( 401 ) . send ( 'Credentials not found' ) ;
343342 }
344343
@@ -392,6 +391,7 @@ const verifyWebAuthn = async (req: Request, res: Response) => {
392391
393392 try {
394393 const { assertionResponse } = req . body ;
394+
395395 const email = verifiedUser . email ;
396396 const phone = verifiedUser . phone ;
397397 let user = verifiedUser ;
@@ -409,14 +409,15 @@ const verifyWebAuthn = async (req: Request, res: Response) => {
409409 }
410410
411411 if ( ! user || ! user . challenge ) {
412+ logger . error ( 'User or user challenge missing' ) ;
412413 await AuthEventService . log ( {
413414 userId : user . id ,
414415 type : 'webauthn_login_failed' ,
415416 req,
416417 metadata : { reason : 'No user or user challenge' } ,
417418 } ) ;
418419
419- return res . status ( 401 ) . json ( { message : 'Authentication failed.' } ) ;
420+ return res . status ( 401 ) . json ( { error : 'Authentication failed.' } ) ;
420421 }
421422
422423 const cred = await Credential . findOne ( {
@@ -433,7 +434,7 @@ const verifyWebAuthn = async (req: Request, res: Response) => {
433434 metadata : { reason : 'No credential' } ,
434435 } ) ;
435436
436- return res . status ( 401 ) . json ( { message : 'Authentication failed.' } ) ;
437+ return res . status ( 401 ) . json ( { error : 'Authentication failed.' } ) ;
437438 }
438439
439440 const expectedChallenge = user . challenge ;
@@ -467,7 +468,7 @@ const verifyWebAuthn = async (req: Request, res: Response) => {
467468 metadata : { reason : 'Incorrect passkey' } ,
468469 } ) ;
469470
470- return res . status ( 500 ) . json ( { message : 'Internal server error' } ) ;
471+ return res . status ( 500 ) . json ( { error : 'Internal server error' } ) ;
471472 }
472473
473474 if ( verification . verified ) {
@@ -539,7 +540,7 @@ const verifyWebAuthn = async (req: Request, res: Response) => {
539540 user_agent : req . headers [ 'user-agent' ] ,
540541 metadata : { reason : 'Verification failed' } ,
541542 } ) ;
542- res . status ( 401 ) . send ( 'Authentication failed' ) ;
543+ res . status ( 401 ) . json ( { error : 'Authentication failed' } ) ;
543544 return ;
544545 }
545546 } catch ( error ) {
@@ -551,7 +552,7 @@ const verifyWebAuthn = async (req: Request, res: Response) => {
551552 user_agent : req . headers [ 'user-agent' ] ,
552553 metadata : { reason : 'Catch all error' } ,
553554 } ) ;
554- res . status ( 500 ) . json ( { message : 'Internal Server error' } ) ;
555+ res . status ( 500 ) . json ( { error : 'Internal Server error' } ) ;
555556 return ;
556557 }
557558} ;
0 commit comments