Skip to content

Commit b192fe0

Browse files
authored
Merge pull request #481 from lenneTech/develop
Release 11.10.5
2 parents d2ca8c9 + e9ad782 commit b192fe0

File tree

6 files changed

+141
-41
lines changed

6 files changed

+141
-41
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lenne.tech/nest-server",
3-
"version": "11.10.4",
3+
"version": "11.10.5",
44
"description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
55
"keywords": [
66
"node",

spectaql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ servers:
1111
info:
1212
title: lT Nest Server
1313
description: Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).
14-
version: 11.10.4
14+
version: 11.10.5
1515
contact:
1616
name: lenne.Tech GmbH
1717
url: https://lenne.tech

src/core/modules/better-auth/core-better-auth.controller.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Roles } from '../../common/decorators/roles.decorator';
2020
import { RoleEnum } from '../../common/enums/role.enum';
2121
import { maskEmail, maskToken } from '../../common/helpers/logging.helper';
2222
import { ConfigService } from '../../common/services/config.service';
23+
import { ErrorCode } from '../error-code/error-codes';
2324
import { BetterAuthSignInResponse, hasSession, hasUser, requires2FA } from './better-auth.types';
2425
import { BetterAuthSessionUser, CoreBetterAuthUserMapper } from './core-better-auth-user.mapper';
2526
import { sendWebResponse, toWebRequest } from './core-better-auth-web.helper';
@@ -227,7 +228,7 @@ export class CoreBetterAuthController {
227228

228229
const api = this.betterAuthService.getApi();
229230
if (!api) {
230-
throw new BadRequestException('Better-Auth API not available');
231+
throw new BadRequestException(ErrorCode.BETTERAUTH_API_NOT_AVAILABLE);
231232
}
232233

233234
// Step 1: Try legacy user migration BEFORE Better Auth handles the request
@@ -255,7 +256,7 @@ export class CoreBetterAuthController {
255256
})) as BetterAuthSignInResponse | null;
256257

257258
if (!response) {
258-
throw new UnauthorizedException('Invalid credentials');
259+
throw new UnauthorizedException(ErrorCode.INVALID_CREDENTIALS);
259260
}
260261

261262
// Check for 2FA requirement
@@ -268,7 +269,7 @@ export class CoreBetterAuthController {
268269
// We need to modify the request body to use the normalized password
269270
const authInstance = this.betterAuthService.getInstance();
270271
if (!authInstance) {
271-
throw new InternalServerErrorException('Better-Auth not initialized');
272+
throw new InternalServerErrorException(ErrorCode.BETTERAUTH_NOT_INITIALIZED);
272273
}
273274

274275
// Create a modified request body with normalized password
@@ -311,7 +312,7 @@ export class CoreBetterAuthController {
311312
// Check if response indicates an error
312313
const responseAny = response as any;
313314
if (responseAny?.error || responseAny?.code === 'CREDENTIAL_ACCOUNT_NOT_FOUND') {
314-
throw new UnauthorizedException('Invalid credentials');
315+
throw new UnauthorizedException(ErrorCode.INVALID_CREDENTIALS);
315316
}
316317

317318
if (hasUser(response)) {
@@ -334,7 +335,7 @@ export class CoreBetterAuthController {
334335
return this.processCookies(res, result);
335336
}
336337

337-
throw new UnauthorizedException('Invalid credentials');
338+
throw new UnauthorizedException(ErrorCode.INVALID_CREDENTIALS);
338339
} catch (error) {
339340
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
340341
this.logger.debug(`Sign-in error: ${errorMessage}`);
@@ -343,7 +344,7 @@ export class CoreBetterAuthController {
343344
throw error;
344345
}
345346

346-
throw new UnauthorizedException('Invalid credentials');
347+
throw new UnauthorizedException(ErrorCode.INVALID_CREDENTIALS);
347348
}
348349
}
349350

@@ -380,7 +381,7 @@ export class CoreBetterAuthController {
380381

381382
const api = this.betterAuthService.getApi();
382383
if (!api) {
383-
throw new BadRequestException('Better-Auth API not available');
384+
throw new BadRequestException(ErrorCode.BETTERAUTH_API_NOT_AVAILABLE);
384385
}
385386

386387
// Normalize password to SHA256 format for consistency with Legacy Auth
@@ -396,7 +397,7 @@ export class CoreBetterAuthController {
396397
});
397398

398399
if (!response) {
399-
throw new BadRequestException('Sign-up failed');
400+
throw new BadRequestException(ErrorCode.SIGNUP_FAILED);
400401
}
401402

402403
if (hasUser(response)) {
@@ -419,14 +420,14 @@ export class CoreBetterAuthController {
419420
return this.processCookies(res, result);
420421
}
421422

422-
throw new BadRequestException('Sign-up failed');
423+
throw new BadRequestException(ErrorCode.SIGNUP_FAILED);
423424
} catch (error) {
424425
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
425426
this.logger.debug(`Sign-up error: ${errorMessage}`);
426427
if (errorMessage.includes('already exists')) {
427-
throw new BadRequestException('User with this email already exists');
428+
throw new BadRequestException(ErrorCode.EMAIL_ALREADY_EXISTS);
428429
}
429-
throw new BadRequestException('Sign-up failed');
430+
throw new BadRequestException(ErrorCode.SIGNUP_FAILED);
430431
}
431432
}
432433

@@ -557,7 +558,7 @@ export class CoreBetterAuthController {
557558
*/
558559
protected ensureEnabled(): void {
559560
if (!this.betterAuthService.isEnabled()) {
560-
throw new BadRequestException('Better-Auth is not enabled');
561+
throw new BadRequestException(ErrorCode.BETTERAUTH_DISABLED);
561562
}
562563
}
563564

@@ -735,7 +736,7 @@ export class CoreBetterAuthController {
735736

736737
const authInstance = this.betterAuthService.getInstance();
737738
if (!authInstance) {
738-
throw new InternalServerErrorException('Better-Auth not initialized');
739+
throw new InternalServerErrorException(ErrorCode.BETTERAUTH_NOT_INITIALIZED);
739740
}
740741

741742
this.logger.debug(`Forwarding to Better Auth: ${req.method} ${req.path}`);

src/core/modules/better-auth/core-better-auth.resolver.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Request, Response } from 'express';
55
import { Roles } from '../../common/decorators/roles.decorator';
66
import { RoleEnum } from '../../common/enums/role.enum';
77
import { maskEmail } from '../../common/helpers/logging.helper';
8+
import { ErrorCode } from '../error-code/error-codes';
89
import {
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

Comments
 (0)