@@ -5,6 +5,7 @@ import { Request, Response } from 'express';
55import { Roles } from '../../common/decorators/roles.decorator' ;
66import { RoleEnum } from '../../common/enums/role.enum' ;
77import { maskEmail } from '../../common/helpers/logging.helper' ;
8+ import { ErrorCode } from '../error-code/error-codes' ;
89import {
910 BetterAuth2FAResponse ,
1011 BetterAuthSignInResponse ,
@@ -203,7 +204,7 @@ export class CoreBetterAuthResolver {
203204
204205 const api = this . betterAuthService . getApi ( ) ;
205206 if ( ! api ) {
206- throw new BadRequestException ( 'Better-Auth API not available' ) ;
207+ throw new BadRequestException ( ErrorCode . BETTERAUTH_API_NOT_AVAILABLE ) ;
207208 }
208209
209210 // Try to sign in, with automatic legacy user migration
@@ -239,7 +240,7 @@ export class CoreBetterAuthResolver {
239240 }
240241
241242 if ( ! response ) {
242- throw new UnauthorizedException ( 'Invalid credentials' ) ;
243+ throw new UnauthorizedException ( ErrorCode . INVALID_CREDENTIALS ) ;
243244 }
244245
245246 // Check for 2FA requirement
@@ -271,7 +272,7 @@ export class CoreBetterAuthResolver {
271272 } ;
272273 }
273274
274- throw new UnauthorizedException ( 'Invalid credentials' ) ;
275+ throw new UnauthorizedException ( ErrorCode . INVALID_CREDENTIALS ) ;
275276 } catch ( error ) {
276277 this . logger . debug (
277278 `[SignIn] Sign-in failed for ${ maskEmail ( email ) } : ${ error instanceof Error ? error . message : 'Unknown error' } ` ,
@@ -291,7 +292,7 @@ export class CoreBetterAuthResolver {
291292 }
292293 }
293294
294- throw new UnauthorizedException ( 'Invalid credentials' ) ;
295+ throw new UnauthorizedException ( ErrorCode . INVALID_CREDENTIALS ) ;
295296 }
296297 }
297298
@@ -308,7 +309,7 @@ export class CoreBetterAuthResolver {
308309 } ) ) as BetterAuthSignInResponse | null ;
309310
310311 if ( ! response || ! hasUser ( response ) ) {
311- throw new UnauthorizedException ( 'Invalid credentials' ) ;
312+ throw new UnauthorizedException ( ErrorCode . INVALID_CREDENTIALS ) ;
312313 }
313314
314315 if ( requires2FA ( response ) ) {
@@ -348,7 +349,7 @@ export class CoreBetterAuthResolver {
348349
349350 const api = this . betterAuthService . getApi ( ) ;
350351 if ( ! api ) {
351- throw new BadRequestException ( 'Better-Auth API not available' ) ;
352+ throw new BadRequestException ( ErrorCode . BETTERAUTH_API_NOT_AVAILABLE ) ;
352353 }
353354
354355 try {
@@ -361,7 +362,7 @@ export class CoreBetterAuthResolver {
361362 } ) ) as BetterAuthSignUpResponse | null ;
362363
363364 if ( ! response ) {
364- throw new BadRequestException ( 'Sign-up failed' ) ;
365+ throw new BadRequestException ( ErrorCode . SIGNUP_FAILED ) ;
365366 }
366367
367368 if ( hasUser ( response ) ) {
@@ -379,14 +380,14 @@ export class CoreBetterAuthResolver {
379380 } ;
380381 }
381382
382- throw new BadRequestException ( 'Sign-up failed' ) ;
383+ throw new BadRequestException ( ErrorCode . SIGNUP_FAILED ) ;
383384 } catch ( error ) {
384385 const errorMessage = error instanceof Error ? error . message : 'Unknown error' ;
385386 this . logger . debug ( `Sign-up error: ${ errorMessage } ` ) ;
386387 if ( errorMessage . includes ( 'already exists' ) ) {
387- throw new BadRequestException ( 'User with this email already exists' ) ;
388+ throw new BadRequestException ( ErrorCode . EMAIL_ALREADY_EXISTS ) ;
388389 }
389- throw new BadRequestException ( 'Sign-up failed' ) ;
390+ throw new BadRequestException ( ErrorCode . SIGNUP_FAILED ) ;
390391 }
391392 }
392393
@@ -429,12 +430,12 @@ export class CoreBetterAuthResolver {
429430 this . ensureEnabled ( ) ;
430431
431432 if ( ! this . betterAuthService . isTwoFactorEnabled ( ) ) {
432- throw new BadRequestException ( 'Two-factor authentication is not enabled' ) ;
433+ throw new BadRequestException ( ErrorCode . TWO_FACTOR_NOT_ENABLED_SERVER ) ;
433434 }
434435
435436 const api = this . betterAuthService . getApi ( ) ;
436437 if ( ! api ) {
437- throw new BadRequestException ( 'Better-Auth API not available' ) ;
438+ throw new BadRequestException ( ErrorCode . BETTERAUTH_API_NOT_AVAILABLE ) ;
438439 }
439440
440441 try {
@@ -450,7 +451,7 @@ export class CoreBetterAuthResolver {
450451 } ;
451452
452453 if ( ! twoFactorApi ?. verifyTotp ) {
453- throw new BadRequestException ( '2FA verification method not available' ) ;
454+ throw new BadRequestException ( ErrorCode . TWO_FACTOR_METHOD_NOT_AVAILABLE ) ;
454455 }
455456
456457 const response = await twoFactorApi . verifyTotp ( {
@@ -469,10 +470,10 @@ export class CoreBetterAuthResolver {
469470 } ;
470471 }
471472
472- throw new UnauthorizedException ( 'Invalid 2FA code' ) ;
473+ throw new UnauthorizedException ( ErrorCode . INVALID_2FA_CODE ) ;
473474 } catch ( error ) {
474475 this . logger . debug ( `2FA verification error: ${ error instanceof Error ? error . message : 'Unknown error' } ` ) ;
475- throw new UnauthorizedException ( 'Invalid 2FA code' ) ;
476+ throw new UnauthorizedException ( ErrorCode . INVALID_2FA_CODE ) ;
476477 }
477478 }
478479
@@ -546,7 +547,7 @@ export class CoreBetterAuthResolver {
546547 this . ensureEnabled ( ) ;
547548
548549 if ( ! this . betterAuthService . isTwoFactorEnabled ( ) ) {
549- throw new BadRequestException ( 'Two-factor authentication is not enabled on this server' ) ;
550+ throw new BadRequestException ( ErrorCode . TWO_FACTOR_NOT_ENABLED_SERVER ) ;
550551 }
551552
552553 const api = this . betterAuthService . getApi ( ) ;
@@ -564,7 +565,7 @@ export class CoreBetterAuthResolver {
564565 } ;
565566
566567 if ( ! twoFactorApi ?. disable ) {
567- throw new BadRequestException ( '2FA disable method not available' ) ;
568+ throw new BadRequestException ( ErrorCode . TWO_FACTOR_METHOD_NOT_AVAILABLE ) ;
568569 }
569570
570571 const response = await twoFactorApi . disable ( {
@@ -591,7 +592,7 @@ export class CoreBetterAuthResolver {
591592 this . ensureEnabled ( ) ;
592593
593594 if ( ! this . betterAuthService . isTwoFactorEnabled ( ) ) {
594- throw new BadRequestException ( 'Two-factor authentication is not enabled on this server' ) ;
595+ throw new BadRequestException ( ErrorCode . TWO_FACTOR_NOT_ENABLED_SERVER ) ;
595596 }
596597
597598 const api = this . betterAuthService . getApi ( ) ;
@@ -609,7 +610,7 @@ export class CoreBetterAuthResolver {
609610 } ;
610611
611612 if ( ! twoFactorApi ?. generateBackupCodes ) {
612- throw new BadRequestException ( 'Generate backup codes method not available' ) ;
613+ throw new BadRequestException ( ErrorCode . TWO_FACTOR_METHOD_NOT_AVAILABLE ) ;
613614 }
614615
615616 const response = await twoFactorApi . generateBackupCodes ( { headers } ) ;
@@ -731,7 +732,7 @@ export class CoreBetterAuthResolver {
731732 this . ensureEnabled ( ) ;
732733
733734 if ( ! this . betterAuthService . isPasskeyEnabled ( ) ) {
734- throw new BadRequestException ( 'Passkey authentication is not enabled on this server' ) ;
735+ throw new BadRequestException ( ErrorCode . PASSKEY_NOT_ENABLED_SERVER ) ;
735736 }
736737
737738 const api = this . betterAuthService . getApi ( ) ;
@@ -749,7 +750,7 @@ export class CoreBetterAuthResolver {
749750 } ;
750751
751752 if ( ! passkeyApi ?. deletePasskey ) {
752- throw new BadRequestException ( 'Delete passkey method not available' ) ;
753+ throw new BadRequestException ( ErrorCode . TWO_FACTOR_METHOD_NOT_AVAILABLE ) ;
753754 }
754755
755756 const response = await passkeyApi . deletePasskey ( {
@@ -773,9 +774,7 @@ export class CoreBetterAuthResolver {
773774 */
774775 protected ensureEnabled ( ) : void {
775776 if ( ! this . betterAuthService . isEnabled ( ) ) {
776- throw new BadRequestException (
777- 'Better-Auth is not enabled. Check that betterAuth.enabled is not set to false in your environment.' ,
778- ) ;
777+ throw new BadRequestException ( ErrorCode . BETTERAUTH_DISABLED ) ;
779778 }
780779 }
781780
0 commit comments