cca-auth-module 0.1.96 → 0.1.97

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.
@@ -18,7 +18,6 @@ export declare class TwoFactorVerifyUseCase implements IBaseService {
18
18
  data?: AdminDTO | UserDTO;
19
19
  } | null>;
20
20
  private mapAdminToDTO;
21
- private mapUserToDTO;
22
21
  private updateUserStatus;
23
22
  private updateUserRefreshToken;
24
23
  private generateTokens;
@@ -2,9 +2,9 @@ import { AuthEntity } from "cca-entities";
2
2
  import { AuthRepository } from "../../infrastructure/repository/AuthRepository";
3
3
  import { RegisterDTO } from "../dtos/RegisterDTO";
4
4
  import { LoginDTO } from "../dtos/LoginDTO";
5
- export declare const validateEmail: (email: string, repository: AuthRepository, isAdmin: boolean) => Promise<AuthEntity>;
5
+ export declare const validateEmail: (email: string, repository: AuthRepository) => Promise<AuthEntity>;
6
6
  export declare const validatePassword: (password?: string) => Promise<void>;
7
7
  export declare const validateEmailUniqueness: (repository: AuthRepository, email: string, excludeUserId?: string) => Promise<void>;
8
8
  export declare const validateRegisterDTO: (auth: RegisterDTO, repository: AuthRepository) => Promise<void>;
9
- export declare const validateLoginDTO: (data: LoginDTO, repository: AuthRepository, isAdmin: boolean) => Promise<AuthEntity>;
9
+ export declare const validateLoginDTO: (data: LoginDTO, repository: AuthRepository) => Promise<AuthEntity>;
10
10
  export declare const validateAdminSecret: (secretPassword?: string) => Promise<void>;
package/dist/index.d.mts CHANGED
@@ -22,7 +22,7 @@ declare const authConfig: (configSource: ConfigSource) => void;
22
22
 
23
23
  declare class AuthRepository extends BaseRepository<AuthEntity> implements IExtendedBaseRepository<AuthEntity> {
24
24
  constructor(repository: Repository<AuthEntity>);
25
- findByEmail(email: string, isAdmin?: boolean): Promise<AuthEntity | null>;
25
+ findByEmail(email: string): Promise<AuthEntity | null>;
26
26
  create(entity: Omit<AuthEntity, "createdAt">): Promise<AuthEntity>;
27
27
  findByUserId(userId: string): Promise<AuthEntity | null>;
28
28
  findByUseAdminId(userId: string, isAdmin?: boolean): Promise<AuthEntity | null>;
@@ -198,7 +198,6 @@ declare class TwoFactorVerifyUseCase implements IBaseService {
198
198
  data?: AdminDTO | UserDTO;
199
199
  } | null>;
200
200
  private mapAdminToDTO;
201
- private mapUserToDTO;
202
201
  private updateUserStatus;
203
202
  private updateUserRefreshToken;
204
203
  private generateTokens;
package/dist/index.d.ts CHANGED
@@ -22,7 +22,7 @@ declare const authConfig: (configSource: ConfigSource) => void;
22
22
 
23
23
  declare class AuthRepository extends BaseRepository<AuthEntity> implements IExtendedBaseRepository<AuthEntity> {
24
24
  constructor(repository: Repository<AuthEntity>);
25
- findByEmail(email: string, isAdmin?: boolean): Promise<AuthEntity | null>;
25
+ findByEmail(email: string): Promise<AuthEntity | null>;
26
26
  create(entity: Omit<AuthEntity, "createdAt">): Promise<AuthEntity>;
27
27
  findByUserId(userId: string): Promise<AuthEntity | null>;
28
28
  findByUseAdminId(userId: string, isAdmin?: boolean): Promise<AuthEntity | null>;
@@ -198,7 +198,6 @@ declare class TwoFactorVerifyUseCase implements IBaseService {
198
198
  data?: AdminDTO | UserDTO;
199
199
  } | null>;
200
200
  private mapAdminToDTO;
201
- private mapUserToDTO;
202
201
  private updateUserStatus;
203
202
  private updateUserRefreshToken;
204
203
  private generateTokens;
package/dist/index.js CHANGED
@@ -184,10 +184,10 @@ var schemas = {
184
184
  ),
185
185
  role: yup.string().oneOf(Object.values(import_cca_entities.UserRole), "Invalid role specified")
186
186
  };
187
- var validateEmail = /* @__PURE__ */ __name(async (email, repository, isAdmin) => {
187
+ var validateEmail = /* @__PURE__ */ __name(async (email, repository) => {
188
188
  try {
189
189
  await schemas.email.validate(email?.trim().toLowerCase());
190
- const user = await repository.findByEmail(email, isAdmin);
190
+ const user = await repository.findByEmail(email);
191
191
  if (!user) {
192
192
  throw new NotFoundError(
193
193
  "The email address or password is incorrect. Please retry"
@@ -230,13 +230,13 @@ var validateRegisterDTO = /* @__PURE__ */ __name(async (auth, repository) => {
230
230
  validatePassword(password)
231
231
  ]);
232
232
  }, "validateRegisterDTO");
233
- var validateLoginDTO = /* @__PURE__ */ __name(async (data, repository, isAdmin) => {
233
+ var validateLoginDTO = /* @__PURE__ */ __name(async (data, repository) => {
234
234
  const { email, role, password } = data;
235
235
  if (role) {
236
236
  await schemas.role.validate(role);
237
237
  }
238
238
  await schemas.password.validate(password);
239
- const auth = await validateEmail(email, repository, isAdmin);
239
+ const auth = await validateEmail(email, repository);
240
240
  if (!auth || !auth.password) {
241
241
  throw new NotFoundError("Invalid credentials");
242
242
  }
@@ -276,17 +276,18 @@ var _LoginUseCase = class _LoginUseCase {
276
276
  await (0, import_cca_core.validateRepository)(this.repository, (repo) => repo.getAll());
277
277
  }
278
278
  async execute(loginDTO, providedAdminPassword) {
279
- if (providedAdminPassword) {
279
+ const isAdmin = !!providedAdminPassword;
280
+ if (isAdmin) {
280
281
  await validateAdminSecret(providedAdminPassword);
281
282
  }
282
- const isAdmin = !!providedAdminPassword;
283
- const auth = await validateLoginDTO(loginDTO, this.repository, isAdmin);
284
- if (!auth.user) {
285
- throw new NotFoundError("User account not found or inactive");
283
+ const auth = await validateLoginDTO(loginDTO, this.repository);
284
+ const account = isAdmin ? auth.admin : auth.user;
285
+ if (!account) {
286
+ throw new NotFoundError(`${isAdmin ? "Admin" : "User"} account not found or inactive`);
286
287
  }
287
- const accessToken = this.jwtService.generateAccessToken(auth.user, auth.role);
288
+ const accessToken = this.jwtService.generateAccessToken(account, auth.role);
288
289
  const expiresAt = (0, import_jwt_decode.jwtDecode)(accessToken).exp;
289
- return { id: auth.user.id, accessToken, expiresAt, enabled: auth.twoFactorEnabled };
290
+ return { id: account.id, accessToken, expiresAt, enabled: auth.twoFactorEnabled };
290
291
  }
291
292
  };
292
293
  __name(_LoginUseCase, "LoginUseCase");
@@ -499,27 +500,17 @@ var _RefreshTokenUseCase = class _RefreshTokenUseCase {
499
500
  async execute(refreshToken) {
500
501
  try {
501
502
  const decoded = await this.service.verifyRefreshToken(refreshToken);
502
- if (!decoded.userId) {
503
- return null;
504
- }
505
- let authEntity = await this.repository.findByUseAdminId(decoded.userId);
506
- if (!authEntity) {
507
- authEntity = await this.repository.findByUseAdminId(decoded.userId, true);
508
- }
509
- if (!authEntity) {
510
- return null;
511
- }
512
- const user = authEntity.user;
513
- const accessToken = this.service.generateAccessToken(user, authEntity.role);
514
- const newRefreshToken = this.service.generateRefreshToken(user);
515
- await this.repository.update(authEntity.id, {
516
- refreshToken: newRefreshToken
517
- });
518
- return {
519
- accessToken,
520
- refreshToken: newRefreshToken
521
- };
503
+ if (!decoded.userId) return null;
504
+ const authEntity = await this.repository.findByUseAdminId(decoded.userId);
505
+ if (!authEntity) return null;
506
+ const account = authEntity.user ?? authEntity.admin;
507
+ if (!account) return null;
508
+ const accessToken = this.service.generateAccessToken(account, authEntity.role);
509
+ const newRefreshToken = this.service.generateRefreshToken(account);
510
+ await this.repository.update(authEntity.id, { refreshToken: newRefreshToken });
511
+ return { accessToken, refreshToken: newRefreshToken };
522
512
  } catch (error) {
513
+ console.error("Refresh token failed:", error);
523
514
  return null;
524
515
  }
525
516
  }
@@ -652,12 +643,17 @@ var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
652
643
  mapAdminToDTO(admin) {
653
644
  return mapper.map(admin, import_cca_entities4.AdminEntity, AdminDTO);
654
645
  }
655
- mapUserToDTO(user) {
656
- return mapper.map(user, import_cca_entities4.UserEntity, UserDTO);
657
- }
646
+ // private mapUserToDTO(user: UserEntity): UserDTO {
647
+ // return mapper.map(user, UserEntity, UserDTO);
648
+ // }
658
649
  async updateUserStatus(auth) {
659
- auth.user.lastLoginAt = /* @__PURE__ */ new Date();
660
- auth.user.isActive = true;
650
+ const account = auth.user ?? auth.admin;
651
+ if (account) {
652
+ account.lastLoginAt = /* @__PURE__ */ new Date();
653
+ account.isActive = true;
654
+ } else {
655
+ throw new NotFoundError("User or Admin account not found for AuthEntity");
656
+ }
661
657
  await this.authRepository.update(auth.id, auth);
662
658
  }
663
659
  async updateUserRefreshToken(auth, refreshToken) {
@@ -665,10 +661,15 @@ var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
665
661
  await this.authRepository.update(auth.id, { refreshToken });
666
662
  }
667
663
  generateTokens(auth) {
668
- return {
669
- accessToken: this.jwtService.generateAccessToken(auth.user, auth.role),
670
- refreshToken: this.jwtService.generateRefreshToken(auth.user)
671
- };
664
+ const account = auth.user ?? auth.admin;
665
+ if (account) {
666
+ return {
667
+ accessToken: this.jwtService.generateAccessToken(account, auth.role),
668
+ refreshToken: this.jwtService.generateRefreshToken(account)
669
+ };
670
+ } else {
671
+ throw new NotFoundError("User or Admin account not found for AuthEntity");
672
+ }
672
673
  }
673
674
  };
674
675
  __name(_TwoFactorVerifyUseCase, "TwoFactorVerifyUseCase");
@@ -994,15 +995,8 @@ var _AuthRepository = class _AuthRepository extends import_cca_core8.BaseReposit
994
995
  constructor(repository) {
995
996
  super(repository);
996
997
  }
997
- async findByEmail(email, isAdmin) {
998
+ async findByEmail(email) {
998
999
  const result = await this.repository.createQueryBuilder("auth").leftJoinAndSelect("auth.user", "user").leftJoinAndSelect("auth.admin", "admin").addSelect("auth.password").where("auth.email = :email", { email }).getOne();
999
- console.log("findByEmail result:", {
1000
- found: !!result,
1001
- hasPassword: !!result?.password,
1002
- hasUser: !!result?.user,
1003
- hasAdmin: !!result?.admin,
1004
- email: result?.email
1005
- });
1006
1000
  return result;
1007
1001
  }
1008
1002
  async create(entity) {
@@ -1027,8 +1021,11 @@ var _AuthRepository = class _AuthRepository extends import_cca_core8.BaseReposit
1027
1021
  if (!auth) {
1028
1022
  throw new NotFoundError("Auth not found");
1029
1023
  }
1030
- auth.refreshToken = "";
1031
- auth.user.isActive = false;
1024
+ const account = auth.user ?? auth.admin;
1025
+ if (account) {
1026
+ auth.refreshToken = "";
1027
+ account.isActive = false;
1028
+ }
1032
1029
  await this.update(auth.id, auth);
1033
1030
  }
1034
1031
  async updateTwoFactorSecret(userId, secret) {