cca-auth-module 0.1.53 → 0.1.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -164,30 +164,10 @@ var createConfigInstance = /* @__PURE__ */ __name(async () => {
164
164
 
165
165
  // src/infrastructure/container/createAuthContainer.ts
166
166
  var import_cca_core11 = require("cca-core");
167
- var import_cca_entities6 = require("cca-entities");
167
+ var import_cca_entities5 = require("cca-entities");
168
168
 
169
169
  // src/application/useCase/LoginUseCase.ts
170
170
  var import_cca_core = require("cca-core");
171
- var import_cca_entities3 = require("cca-entities");
172
-
173
- // src/application/dtos/UserDTO.ts
174
- var import_classes = require("@automapper/classes");
175
- var _UserDTO = class _UserDTO {
176
- };
177
- __name(_UserDTO, "UserDTO");
178
- __decorateClass([
179
- (0, import_classes.AutoMap)()
180
- ], _UserDTO.prototype, "id", 2);
181
- __decorateClass([
182
- (0, import_classes.AutoMap)()
183
- ], _UserDTO.prototype, "name", 2);
184
- __decorateClass([
185
- (0, import_classes.AutoMap)()
186
- ], _UserDTO.prototype, "email", 2);
187
- __decorateClass([
188
- (0, import_classes.AutoMap)()
189
- ], _UserDTO.prototype, "role", 2);
190
- var UserDTO = _UserDTO;
191
171
 
192
172
  // src/application/validators/authValidation.ts
193
173
  var yup = __toESM(require("yup"));
@@ -282,6 +262,73 @@ var validateAdminSecret = /* @__PURE__ */ __name(async (secretPassword) => {
282
262
  }
283
263
  }, "validateAdminSecret");
284
264
 
265
+ // src/application/useCase/LoginUseCase.ts
266
+ var _LoginUseCase = class _LoginUseCase {
267
+ constructor(repository) {
268
+ this.repository = repository;
269
+ }
270
+ async initialize() {
271
+ await (0, import_cca_core.validateRepository)(this.repository, (repo) => repo.getAll());
272
+ }
273
+ async execute(loginDTO) {
274
+ const auth = await this.validateLogin(loginDTO);
275
+ return auth.user.id;
276
+ }
277
+ async validateLogin(loginDTO) {
278
+ const auth = await validateLoginDTO(loginDTO, this.repository);
279
+ return auth;
280
+ }
281
+ };
282
+ __name(_LoginUseCase, "LoginUseCase");
283
+ var LoginUseCase = _LoginUseCase;
284
+
285
+ // src/application/useCase/LoginAdminUseCase.ts
286
+ var import_cca_core2 = require("cca-core");
287
+ var _LoginAdminUseCase = class _LoginAdminUseCase {
288
+ constructor(repository) {
289
+ this.repository = repository;
290
+ }
291
+ async initialize() {
292
+ await (0, import_cca_core2.validateRepository)(this.repository, (repo) => repo.getAll());
293
+ }
294
+ async execute(loginDTO, adminPassword) {
295
+ const auth = await this.validateLogin(loginDTO, adminPassword);
296
+ return auth.user.id;
297
+ }
298
+ async validateLogin(loginDTO, adminPassword) {
299
+ const auth = await validateLoginDTO(loginDTO, this.repository);
300
+ await validateAdminSecret(adminPassword);
301
+ return auth;
302
+ }
303
+ };
304
+ __name(_LoginAdminUseCase, "LoginAdminUseCase");
305
+ var LoginAdminUseCase = _LoginAdminUseCase;
306
+
307
+ // src/application/useCase/LogoutUseCase.ts
308
+ var import_cca_core3 = require("cca-core");
309
+ var _LogoutUseCase = class _LogoutUseCase {
310
+ constructor(repository) {
311
+ this.repository = repository;
312
+ }
313
+ async initialize() {
314
+ await (0, import_cca_core3.validateRepository)(this.repository, (repo) => repo.getAll());
315
+ }
316
+ async execute(authId) {
317
+ try {
318
+ await this.repository.logout(authId);
319
+ } catch (error) {
320
+ new NotFoundError("Auth not found");
321
+ }
322
+ }
323
+ };
324
+ __name(_LogoutUseCase, "LogoutUseCase");
325
+ var LogoutUseCase = _LogoutUseCase;
326
+
327
+ // src/application/useCase/RegisterUseCase.ts
328
+ var import_cca_core4 = require("cca-core");
329
+ var bcrypt2 = __toESM(require("bcrypt"));
330
+ var import_cca_entities3 = require("cca-entities");
331
+
285
332
  // src/application/mappers/utils/mapper.ts
286
333
  var import_core2 = require("@automapper/core");
287
334
  var import_classes3 = require("@automapper/classes");
@@ -296,6 +343,25 @@ var _RegisterDTO = class _RegisterDTO {
296
343
  __name(_RegisterDTO, "RegisterDTO");
297
344
  var RegisterDTO = _RegisterDTO;
298
345
 
346
+ // src/application/dtos/UserDTO.ts
347
+ var import_classes = require("@automapper/classes");
348
+ var _UserDTO = class _UserDTO {
349
+ };
350
+ __name(_UserDTO, "UserDTO");
351
+ __decorateClass([
352
+ (0, import_classes.AutoMap)()
353
+ ], _UserDTO.prototype, "id", 2);
354
+ __decorateClass([
355
+ (0, import_classes.AutoMap)()
356
+ ], _UserDTO.prototype, "name", 2);
357
+ __decorateClass([
358
+ (0, import_classes.AutoMap)()
359
+ ], _UserDTO.prototype, "email", 2);
360
+ __decorateClass([
361
+ (0, import_classes.AutoMap)()
362
+ ], _UserDTO.prototype, "role", 2);
363
+ var UserDTO = _UserDTO;
364
+
299
365
  // src/application/dtos/AdminDTO.ts
300
366
  var import_classes2 = require("@automapper/classes");
301
367
  var _AdminDTO = class _AdminDTO {
@@ -357,127 +423,7 @@ var mapper = (0, import_core2.createMapper)({
357
423
  });
358
424
  createUserMappings(mapper);
359
425
 
360
- // src/application/useCase/LoginUseCase.ts
361
- var _LoginUseCase = class _LoginUseCase {
362
- constructor(repository, authService) {
363
- this.repository = repository;
364
- this.authService = authService;
365
- }
366
- async initialize() {
367
- await (0, import_cca_core.validateRepository)(this.repository, (repo) => repo.getAll());
368
- }
369
- async execute(loginDTO) {
370
- const auth = await this.validateLogin(loginDTO);
371
- const token = await this.handleAuthentication(auth);
372
- const userDTO = this.mapUserToDTO(auth.user);
373
- return { token, user: userDTO };
374
- }
375
- async validateLogin(loginDTO) {
376
- const auth = await validateLoginDTO(loginDTO, this.repository);
377
- return auth;
378
- }
379
- async handleAuthentication(auth) {
380
- const token = this.generateTokens(auth);
381
- await this.updateUserStatus(auth);
382
- await this.updateUserRefreshToken(auth, token.refreshToken);
383
- return token;
384
- }
385
- generateTokens(auth) {
386
- return {
387
- accessToken: this.authService.generateAccessToken(auth.user, auth.role),
388
- refreshToken: this.authService.generateRefreshToken(auth.user)
389
- };
390
- }
391
- async updateUserStatus(auth) {
392
- auth.user.lastLoginAt = /* @__PURE__ */ new Date();
393
- auth.user.isActive = true;
394
- await this.repository.update(auth.id, auth);
395
- }
396
- async updateUserRefreshToken(auth, refreshToken) {
397
- auth.refreshToken = refreshToken;
398
- await this.repository.update(auth.id, { refreshToken });
399
- }
400
- mapUserToDTO(user) {
401
- return mapper.map(user, import_cca_entities3.UserEntity, UserDTO);
402
- }
403
- };
404
- __name(_LoginUseCase, "LoginUseCase");
405
- var LoginUseCase = _LoginUseCase;
406
-
407
- // src/application/useCase/LoginAdminUseCase.ts
408
- var import_cca_core2 = require("cca-core");
409
- var import_cca_entities4 = require("cca-entities");
410
- var _LoginAdminUseCase = class _LoginAdminUseCase {
411
- constructor(repository, authService) {
412
- this.repository = repository;
413
- this.authService = authService;
414
- }
415
- async initialize() {
416
- await (0, import_cca_core2.validateRepository)(this.repository, (repo) => repo.getAll());
417
- }
418
- async execute(loginDTO, adminPassword) {
419
- const auth = await this.validateLogin(loginDTO, adminPassword);
420
- const token = await this.handleAuthentication(auth);
421
- const userDTO = this.mapUserToDTO(auth.admin);
422
- return { token, user: userDTO };
423
- }
424
- async validateLogin(loginDTO, adminPassword) {
425
- const auth = await validateLoginDTO(loginDTO, this.repository);
426
- await validateAdminSecret(adminPassword);
427
- return auth;
428
- }
429
- async handleAuthentication(auth) {
430
- const token = this.generateTokens(auth);
431
- await this.updateUserStatus(auth);
432
- await this.updateUserRefreshToken(auth, token.refreshToken);
433
- return token;
434
- }
435
- generateTokens(auth) {
436
- return {
437
- accessToken: this.authService.generateAccessToken(auth.admin, auth.role),
438
- refreshToken: this.authService.generateRefreshToken(auth.admin)
439
- };
440
- }
441
- async updateUserStatus(auth) {
442
- auth.admin.lastLoginAt = /* @__PURE__ */ new Date();
443
- auth.admin.isActive = true;
444
- await this.repository.update(auth.id, auth);
445
- }
446
- async updateUserRefreshToken(auth, refreshToken) {
447
- auth.refreshToken = refreshToken;
448
- await this.repository.update(auth.id, { refreshToken });
449
- }
450
- mapUserToDTO(admin) {
451
- return mapper.map(admin, import_cca_entities4.AdminEntity, AdminDTO);
452
- }
453
- };
454
- __name(_LoginAdminUseCase, "LoginAdminUseCase");
455
- var LoginAdminUseCase = _LoginAdminUseCase;
456
-
457
- // src/application/useCase/LogoutUseCase.ts
458
- var import_cca_core3 = require("cca-core");
459
- var _LogoutUseCase = class _LogoutUseCase {
460
- constructor(repository) {
461
- this.repository = repository;
462
- }
463
- async initialize() {
464
- await (0, import_cca_core3.validateRepository)(this.repository, (repo) => repo.getAll());
465
- }
466
- async execute(authId) {
467
- try {
468
- await this.repository.logout(authId);
469
- } catch (error) {
470
- new NotFoundError("Auth not found");
471
- }
472
- }
473
- };
474
- __name(_LogoutUseCase, "LogoutUseCase");
475
- var LogoutUseCase = _LogoutUseCase;
476
-
477
426
  // src/application/useCase/RegisterUseCase.ts
478
- var import_cca_core4 = require("cca-core");
479
- var bcrypt2 = __toESM(require("bcrypt"));
480
- var import_cca_entities5 = require("cca-entities");
481
427
  var _RegisterUseCase = class _RegisterUseCase {
482
428
  constructor(repository) {
483
429
  this.SALT_ROUNDS = 10;
@@ -486,7 +432,7 @@ var _RegisterUseCase = class _RegisterUseCase {
486
432
  async initialize() {
487
433
  await (0, import_cca_core4.validateRepository)(this.repository, (repo) => repo.getAll());
488
434
  }
489
- async execute(email, name, password, role = import_cca_entities5.UserRole.GUEST, adminPassword) {
435
+ async execute(email, name, password, role = import_cca_entities3.UserRole.GUEST, adminPassword) {
490
436
  try {
491
437
  const normalizedDTO = this.normalizeAuthDTO({
492
438
  email,
@@ -495,7 +441,7 @@ var _RegisterUseCase = class _RegisterUseCase {
495
441
  role,
496
442
  adminPassword
497
443
  });
498
- if (role === import_cca_entities5.UserRole.ADMIN && adminPassword) {
444
+ if (role === import_cca_entities3.UserRole.ADMIN && adminPassword) {
499
445
  await validateAdminSecret(adminPassword);
500
446
  }
501
447
  await validateRegisterDTO(normalizedDTO, this.repository);
@@ -522,8 +468,8 @@ var _RegisterUseCase = class _RegisterUseCase {
522
468
  return await bcrypt2.hash(password, this.SALT_ROUNDS);
523
469
  }
524
470
  async createAuthEntity(dto, hashedPassword) {
525
- const authEntity = mapper.map(dto, RegisterDTO, import_cca_entities5.AuthEntity);
526
- const userEntity = mapper.map(dto, RegisterDTO, import_cca_entities5.UserEntity);
471
+ const authEntity = mapper.map(dto, RegisterDTO, import_cca_entities3.AuthEntity);
472
+ const userEntity = mapper.map(dto, RegisterDTO, import_cca_entities3.UserEntity);
527
473
  authEntity.password = hashedPassword;
528
474
  authEntity.refreshToken = "";
529
475
  authEntity.user = userEntity;
@@ -547,6 +493,7 @@ var _RefreshTokenUseCase = class _RefreshTokenUseCase {
547
493
  async execute(refreshToken) {
548
494
  try {
549
495
  const decoded = await this.service.verifyRefreshToken(refreshToken);
496
+ console.log("Decoded refresh token:", decoded);
550
497
  const auth = decoded.userId ? await this.repository.findById(decoded.userId) : null;
551
498
  if (!auth || auth.refreshToken !== refreshToken) {
552
499
  return null;
@@ -558,13 +505,9 @@ var _RefreshTokenUseCase = class _RefreshTokenUseCase {
558
505
  });
559
506
  return { accessToken, refreshToken: newRefreshToken };
560
507
  } catch (error) {
561
- console.error("Error in RefreshTokenUseCase:", error);
562
- return null;
508
+ return error;
563
509
  }
564
510
  }
565
- async verityToken(token) {
566
- return await this.service.verifyAccessToken(token);
567
- }
568
511
  };
569
512
  __name(_RefreshTokenUseCase, "RefreshTokenUseCase");
570
513
  var RefreshTokenUseCase = _RefreshTokenUseCase;
@@ -646,12 +589,13 @@ var TwoFactorEnableUseCase = _TwoFactorEnableUseCase;
646
589
 
647
590
  // src/application/useCase/TwoFactorVerifyUseCase.ts
648
591
  var import_cca_core7 = require("cca-core");
592
+ var import_cca_entities4 = require("cca-entities");
649
593
  var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
650
594
  constructor(twoFactorService, authRepository, jwtService) {
651
- this.isInitialized = false;
652
595
  this.twoFactorService = twoFactorService;
653
596
  this.authRepository = authRepository;
654
597
  this.jwtService = jwtService;
598
+ this.isInitialized = false;
655
599
  }
656
600
  async initialize() {
657
601
  if (this.isInitialized) return;
@@ -660,7 +604,6 @@ var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
660
604
  this.jwtService.initialize(),
661
605
  (0, import_cca_core7.validateRepository)(this.authRepository, (repo) => repo.getAll())
662
606
  ]);
663
- 4;
664
607
  this.isInitialized = true;
665
608
  }
666
609
  async execute(dto) {
@@ -669,18 +612,53 @@ var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
669
612
  }
670
613
  const { userId, token } = dto;
671
614
  if (!userId || !token) {
672
- throw new TwoFactorError("user ID and token are required");
615
+ throw new TwoFactorError("User ID and token are required.");
673
616
  }
674
617
  const auth = await this.authRepository.findByUserId(userId);
675
618
  if (!auth || !auth.twoFactorSecret || !auth.twoFactorEnabled) {
676
- throw new TwoFactorError("Invalid request");
619
+ throw new TwoFactorError("Invalid request.");
677
620
  }
678
621
  const isValid = this.twoFactorService.verifyToken(token, auth.twoFactorSecret);
679
622
  if (!isValid) {
680
- throw new TwoFactorError("Invalid verification code");
623
+ throw new TwoFactorError("Invalid verification code.");
624
+ }
625
+ const tokenPair = this.generateTokens(auth);
626
+ await this.updateUserStatus(auth);
627
+ await this.updateUserRefreshToken(auth, tokenPair.refreshToken);
628
+ if (auth.admin) {
629
+ return {
630
+ token: tokenPair.accessToken,
631
+ refreshToken: tokenPair.refreshToken,
632
+ data: this.mapAdminToDTO(auth.admin)
633
+ };
634
+ }
635
+ if (auth.user) {
636
+ return {
637
+ token: tokenPair.accessToken,
638
+ refreshToken: tokenPair.refreshToken,
639
+ data: this.mapUserToDTO(auth.user)
640
+ };
681
641
  }
642
+ return null;
643
+ }
644
+ mapAdminToDTO(admin) {
645
+ return mapper.map(admin, import_cca_entities4.AdminEntity, AdminDTO);
646
+ }
647
+ mapUserToDTO(user) {
648
+ return mapper.map(user, import_cca_entities4.UserEntity, UserDTO);
649
+ }
650
+ async updateUserStatus(auth) {
651
+ auth.user.lastLoginAt = /* @__PURE__ */ new Date();
652
+ auth.user.isActive = true;
653
+ await this.authRepository.update(auth.id, auth);
654
+ }
655
+ async updateUserRefreshToken(auth, refreshToken) {
656
+ auth.refreshToken = refreshToken;
657
+ await this.authRepository.update(auth.id, { refreshToken });
658
+ }
659
+ generateTokens(auth) {
682
660
  return {
683
- token: this.jwtService.generateAccessToken(auth.user, auth.role),
661
+ accessToken: this.jwtService.generateAccessToken(auth.user, auth.role),
684
662
  refreshToken: this.jwtService.generateRefreshToken(auth.user)
685
663
  };
686
664
  }
@@ -730,8 +708,16 @@ var _AuthController = class _AuthController {
730
708
  this.login = /* @__PURE__ */ __name(async (req, res, next) => {
731
709
  try {
732
710
  const { adminPassword, ...loginDTO } = req.body;
733
- const result = await this.loginUseCase.execute(loginDTO);
734
- res.status(201).json(result);
711
+ const id = await this.loginUseCase.execute(loginDTO);
712
+ res.status(201).json(
713
+ {
714
+ status: "pending",
715
+ message: "Enter 2FA code",
716
+ data: {
717
+ userId: id
718
+ }
719
+ }
720
+ );
735
721
  } catch (error) {
736
722
  next(error);
737
723
  }
@@ -760,6 +746,7 @@ var _AuthController = class _AuthController {
760
746
  try {
761
747
  const { email, name, password, role, adminPassword } = req.body;
762
748
  await this.registerUseCase.execute(email, name, password, role, adminPassword);
749
+ res.status(200).json({ status: "success" });
763
750
  } catch (error) {
764
751
  next(error);
765
752
  }
@@ -769,9 +756,6 @@ var _AuthController = class _AuthController {
769
756
  const result = await this.refreshTokenUseCase.execute(refreshToken);
770
757
  res.json(result);
771
758
  }, "refreshToken");
772
- this.verifyToken = /* @__PURE__ */ __name(async (token) => {
773
- return await this.refreshTokenUseCase.verityToken(token);
774
- }, "verifyToken");
775
759
  this.setup2FA = /* @__PURE__ */ __name(async (req, res, next) => {
776
760
  try {
777
761
  const userId = req.auth.id;
@@ -783,11 +767,7 @@ var _AuthController = class _AuthController {
783
767
  }, "setup2FA");
784
768
  this.enable2FA = /* @__PURE__ */ __name(async (req, res, next) => {
785
769
  try {
786
- console.log("enable2FA req", req);
787
- console.log("enable2FA called", req.body);
788
- console.log("req.auth", req.auth);
789
- console.log("req.auth.id", req.auth?.id);
790
- const userId = req.auth.id;
770
+ const userId = req.auth?.id;
791
771
  const dto = req.body;
792
772
  await this.twoFactorEnableUseCase.execute(userId, dto);
793
773
  res.status(200).json({ message: "Two-factor authentication has been enabled successfully" });
@@ -812,7 +792,10 @@ var _AuthController = class _AuthController {
812
792
  const userId = req.auth.id;
813
793
  const dto = req.body;
814
794
  await this.twoFactorDisableUseCase.execute(userId, dto);
815
- res.status(200).json({ message: "Two-factor authentication has been disabled successfully" });
795
+ res.status(200).json({
796
+ status: "success",
797
+ message: "Two-factor authentication has been disabled successfully"
798
+ });
816
799
  } catch (error) {
817
800
  next(error);
818
801
  }
@@ -982,8 +965,11 @@ var _JwtAuthService = class _JwtAuthService {
982
965
  }
983
966
  async verifyToken(token, secret) {
984
967
  try {
968
+ console.log("Verifying token:", token);
969
+ console.log("Using secret:", secret);
985
970
  return jwt.verify(token, secret);
986
- } catch {
971
+ } catch (error) {
972
+ console.error("Error verifying token:", error);
987
973
  throw new UnauthorizedError();
988
974
  }
989
975
  }
@@ -1094,7 +1080,7 @@ var TwoFactorService = _TwoFactorService;
1094
1080
  async function createAuthContainer(database) {
1095
1081
  const container = new import_cca_core11.BaseContainer({ database });
1096
1082
  const authRepository = new AuthRepository(
1097
- database.getRepository(import_cca_entities6.AuthEntity)
1083
+ database.getRepository(import_cca_entities5.AuthEntity)
1098
1084
  );
1099
1085
  container.registerRepository("AuthRepository", authRepository);
1100
1086
  const jwtAuthService = new JwtAuthService(authRepository);
@@ -1103,10 +1089,9 @@ async function createAuthContainer(database) {
1103
1089
  const twoFactorService = new TwoFactorService(configData);
1104
1090
  container.registerService("TwoFactorService", twoFactorService);
1105
1091
  const requireComplete2FA = new RequireComplete2FA(jwtAuthService);
1106
- const loginUseCase = new LoginUseCase(authRepository, jwtAuthService);
1092
+ const loginUseCase = new LoginUseCase(authRepository);
1107
1093
  const loginAdminUseCase = new LoginAdminUseCase(
1108
- authRepository,
1109
- jwtAuthService
1094
+ authRepository
1110
1095
  );
1111
1096
  const logoutUseCase = new LogoutUseCase(authRepository);
1112
1097
  const registerUseCase = new RegisterUseCase(authRepository);