cca-auth-module 0.1.91 → 0.1.93

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.
@@ -7,7 +7,7 @@ export declare class LoginUseCase implements IBaseService {
7
7
  private readonly jwtService;
8
8
  constructor(repository: AuthRepository, jwtService: JwtAuthService);
9
9
  initialize(): Promise<void>;
10
- execute(loginDTO: LoginDTO, providedAdminPassword: string, admin: boolean): Promise<{
10
+ execute(loginDTO: LoginDTO, providedAdminPassword?: string): Promise<{
11
11
  id: string;
12
12
  accessToken: string;
13
13
  expiresAt: number;
@@ -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) => Promise<AuthEntity>;
5
+ export declare const validateEmail: (email: string, repository: AuthRepository, isAdmin: boolean) => 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) => Promise<AuthEntity>;
9
+ export declare const validateLoginDTO: (data: LoginDTO, repository: AuthRepository, isAdmin: boolean) => Promise<AuthEntity>;
10
10
  export declare const validateAdminSecret: (secretPassword?: string) => Promise<void>;
@@ -1,9 +1,8 @@
1
- import { AuthEntity, UserEntity, UserRole } from "cca-entities";
1
+ import { AdminEntity, UserEntity, UserRole } from "cca-entities";
2
2
  import { IDecodedToken } from "./IDecodedToken";
3
3
  export interface IAuthService {
4
- validateUser(email: string, password: string): Promise<AuthEntity | null>;
5
- generateAccessToken(user: UserEntity, role: UserRole): string;
6
- generateRefreshToken(user: UserEntity): string;
4
+ generateAccessToken(user: UserEntity | AdminEntity, role: UserRole): string;
5
+ generateRefreshToken(user: UserEntity | AdminEntity): string;
7
6
  verifyAccessToken(token: string): Promise<IDecodedToken>;
8
7
  verifyRefreshToken(token: string): IDecodedToken;
9
8
  }
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { BaseRepository, IExtendedBaseRepository, IBaseService, BaseDatabase, BaseContainer } from 'cca-core';
2
2
  import { Request, Response, NextFunction } from 'express';
3
- import { AuthEntity, UserRole, UserEntity } from 'cca-entities';
3
+ import { AuthEntity, UserRole, UserEntity, AdminEntity } from 'cca-entities';
4
4
  import { Repository } from 'typeorm';
5
5
  import * as jwt from 'jsonwebtoken';
6
6
  import { JwtPayload } from 'jsonwebtoken';
@@ -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): Promise<AuthEntity | null>;
25
+ findByEmail(email: string, isAdmin?: boolean): 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>;
@@ -67,9 +67,8 @@ interface IDecodedToken extends JwtPayload {
67
67
  }
68
68
 
69
69
  interface IAuthService {
70
- validateUser(email: string, password: string): Promise<AuthEntity | null>;
71
- generateAccessToken(user: UserEntity, role: UserRole): string;
72
- generateRefreshToken(user: UserEntity): string;
70
+ generateAccessToken(user: UserEntity | AdminEntity, role: UserRole): string;
71
+ generateRefreshToken(user: UserEntity | AdminEntity): string;
73
72
  verifyAccessToken(token: string): Promise<IDecodedToken>;
74
73
  verifyRefreshToken(token: string): IDecodedToken;
75
74
  }
@@ -81,10 +80,9 @@ declare class JwtAuthService implements IBaseService, IAuthService {
81
80
  private loadConfig;
82
81
  initialize(): Promise<void>;
83
82
  private validateConfiguration;
84
- validateUser(email: string, password: string): Promise<AuthEntity | null>;
85
83
  private verifyJwtConfig;
86
- generateAccessToken(user: UserEntity, role: UserRole): string;
87
- generateRefreshToken(user: UserEntity): string;
84
+ generateAccessToken(user: UserEntity | AdminEntity, role: UserRole): string;
85
+ generateRefreshToken(user: UserEntity | AdminEntity): string;
88
86
  verifyToken(token: string, secret: string): Promise<IDecodedToken>;
89
87
  verifyAccessToken(token: string): Promise<IDecodedToken>;
90
88
  verifyRefreshToken(token: string): Promise<IDecodedToken>;
@@ -95,7 +93,7 @@ declare class LoginUseCase implements IBaseService {
95
93
  private readonly jwtService;
96
94
  constructor(repository: AuthRepository, jwtService: JwtAuthService);
97
95
  initialize(): Promise<void>;
98
- execute(loginDTO: LoginDTO, providedAdminPassword: string, admin: boolean): Promise<{
96
+ execute(loginDTO: LoginDTO, providedAdminPassword?: string): Promise<{
99
97
  id: string;
100
98
  accessToken: string;
101
99
  expiresAt: number;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { BaseRepository, IExtendedBaseRepository, IBaseService, BaseDatabase, BaseContainer } from 'cca-core';
2
2
  import { Request, Response, NextFunction } from 'express';
3
- import { AuthEntity, UserRole, UserEntity } from 'cca-entities';
3
+ import { AuthEntity, UserRole, UserEntity, AdminEntity } from 'cca-entities';
4
4
  import { Repository } from 'typeorm';
5
5
  import * as jwt from 'jsonwebtoken';
6
6
  import { JwtPayload } from 'jsonwebtoken';
@@ -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): Promise<AuthEntity | null>;
25
+ findByEmail(email: string, isAdmin?: boolean): 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>;
@@ -67,9 +67,8 @@ interface IDecodedToken extends JwtPayload {
67
67
  }
68
68
 
69
69
  interface IAuthService {
70
- validateUser(email: string, password: string): Promise<AuthEntity | null>;
71
- generateAccessToken(user: UserEntity, role: UserRole): string;
72
- generateRefreshToken(user: UserEntity): string;
70
+ generateAccessToken(user: UserEntity | AdminEntity, role: UserRole): string;
71
+ generateRefreshToken(user: UserEntity | AdminEntity): string;
73
72
  verifyAccessToken(token: string): Promise<IDecodedToken>;
74
73
  verifyRefreshToken(token: string): IDecodedToken;
75
74
  }
@@ -81,10 +80,9 @@ declare class JwtAuthService implements IBaseService, IAuthService {
81
80
  private loadConfig;
82
81
  initialize(): Promise<void>;
83
82
  private validateConfiguration;
84
- validateUser(email: string, password: string): Promise<AuthEntity | null>;
85
83
  private verifyJwtConfig;
86
- generateAccessToken(user: UserEntity, role: UserRole): string;
87
- generateRefreshToken(user: UserEntity): string;
84
+ generateAccessToken(user: UserEntity | AdminEntity, role: UserRole): string;
85
+ generateRefreshToken(user: UserEntity | AdminEntity): string;
88
86
  verifyToken(token: string, secret: string): Promise<IDecodedToken>;
89
87
  verifyAccessToken(token: string): Promise<IDecodedToken>;
90
88
  verifyRefreshToken(token: string): Promise<IDecodedToken>;
@@ -95,7 +93,7 @@ declare class LoginUseCase implements IBaseService {
95
93
  private readonly jwtService;
96
94
  constructor(repository: AuthRepository, jwtService: JwtAuthService);
97
95
  initialize(): Promise<void>;
98
- execute(loginDTO: LoginDTO, providedAdminPassword: string, admin: boolean): Promise<{
96
+ execute(loginDTO: LoginDTO, providedAdminPassword?: string): Promise<{
99
97
  id: string;
100
98
  accessToken: string;
101
99
  expiresAt: number;
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) => {
187
+ var validateEmail = /* @__PURE__ */ __name(async (email, repository, isAdmin) => {
188
188
  try {
189
189
  await schemas.email.validate(email?.trim().toLowerCase());
190
- const user = await repository.findByEmail(email);
190
+ const user = await repository.findByEmail(email, isAdmin);
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) => {
233
+ var validateLoginDTO = /* @__PURE__ */ __name(async (data, repository, isAdmin) => {
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);
239
+ const auth = await validateEmail(email, repository, isAdmin);
240
240
  if (!auth || !auth.password) {
241
241
  throw new NotFoundError("Invalid credentials");
242
242
  }
@@ -275,11 +275,15 @@ var _LoginUseCase = class _LoginUseCase {
275
275
  async initialize() {
276
276
  await (0, import_cca_core.validateRepository)(this.repository, (repo) => repo.getAll());
277
277
  }
278
- async execute(loginDTO, providedAdminPassword, admin) {
279
- if (admin) {
278
+ async execute(loginDTO, providedAdminPassword) {
279
+ if (providedAdminPassword) {
280
280
  await validateAdminSecret(providedAdminPassword);
281
281
  }
282
- const auth = await validateLoginDTO(loginDTO, this.repository);
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");
286
+ }
283
287
  const accessToken = this.jwtService.generateAccessToken(auth.user, auth.role);
284
288
  const expiresAt = (0, import_jwt_decode.jwtDecode)(accessToken).exp;
285
289
  return { id: auth.user.id, accessToken, expiresAt, enabled: auth.twoFactorEnabled };
@@ -473,11 +477,7 @@ var _RegisterUseCase = class _RegisterUseCase {
473
477
  const authEntity = mapper.map(dto, RegisterDTO, import_cca_entities3.AuthEntity);
474
478
  const userOrAdminEntity = isAdmin ? mapper.map(dto, RegisterDTO, import_cca_entities3.AdminEntity) : mapper.map(dto, RegisterDTO, import_cca_entities3.UserEntity);
475
479
  userOrAdminEntity.updatedAt = void 0;
476
- if (isAdmin) {
477
- authEntity.admin = userOrAdminEntity;
478
- } else {
479
- authEntity.user = userOrAdminEntity;
480
- }
480
+ authEntity.user = userOrAdminEntity;
481
481
  authEntity.password = hashedPassword;
482
482
  authEntity.refreshToken = "";
483
483
  return authEntity;
@@ -509,7 +509,7 @@ var _RefreshTokenUseCase = class _RefreshTokenUseCase {
509
509
  if (!authEntity) {
510
510
  return null;
511
511
  }
512
- const user = authEntity.role === "admin" ? authEntity.admin : authEntity.user;
512
+ const user = authEntity.user;
513
513
  const accessToken = this.service.generateAccessToken(user, authEntity.role);
514
514
  const newRefreshToken = this.service.generateRefreshToken(user);
515
515
  await this.repository.update(authEntity.id, {
@@ -640,18 +640,11 @@ var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
640
640
  const tokenPair = this.generateTokens(auth);
641
641
  await this.updateUserStatus(auth);
642
642
  await this.updateUserRefreshToken(auth, tokenPair.refreshToken);
643
- if (auth.admin) {
644
- return {
645
- token: tokenPair.accessToken,
646
- refreshToken: tokenPair.refreshToken,
647
- data: this.mapAdminToDTO(auth.admin)
648
- };
649
- }
650
643
  if (auth.user) {
651
644
  return {
652
645
  token: tokenPair.accessToken,
653
646
  refreshToken: tokenPair.refreshToken,
654
- data: this.mapUserToDTO(auth.user)
647
+ data: this.mapAdminToDTO(auth.user)
655
648
  };
656
649
  }
657
650
  return null;
@@ -749,7 +742,7 @@ var _AuthController = class _AuthController {
749
742
  this.login = /* @__PURE__ */ __name(async (req, res, next) => {
750
743
  try {
751
744
  const loginDTO = req.body;
752
- const result = await this.loginUseCase.execute(loginDTO, "", false);
745
+ const result = await this.loginUseCase.execute(loginDTO);
753
746
  const twoFactorEnabled = result.enabled ?? false;
754
747
  const loginData = {
755
748
  accessToken: result.accessToken,
@@ -776,7 +769,7 @@ var _AuthController = class _AuthController {
776
769
  if (!adminPassword) {
777
770
  throw new ForbiddenError("Admin password is required");
778
771
  }
779
- const result = await this.loginUseCase.execute(loginDTO, adminPassword, true);
772
+ const result = await this.loginUseCase.execute(loginDTO, adminPassword);
780
773
  const adminLoginData = {
781
774
  message: result,
782
775
  auth: this.createAuthData(
@@ -1001,9 +994,14 @@ var _AuthRepository = class _AuthRepository extends import_cca_core8.BaseReposit
1001
994
  constructor(repository) {
1002
995
  super(repository);
1003
996
  }
1004
- async findByEmail(email) {
1005
- const query = this.repository.createQueryBuilder("auth").leftJoinAndSelect("auth.user", "user").addSelect("auth.password").where("auth.email = :email", { email });
1006
- return await query.getOne();
997
+ async findByEmail(email, isAdmin) {
998
+ const queryBuilder = this.repository.createQueryBuilder("auth").addSelect("auth.password").where("auth.email = :email", { email });
999
+ if (isAdmin) {
1000
+ queryBuilder.leftJoinAndSelect("auth.admin", "admin");
1001
+ } else {
1002
+ queryBuilder.leftJoinAndSelect("auth.user", "user");
1003
+ }
1004
+ return await queryBuilder.getOne();
1007
1005
  }
1008
1006
  async create(entity) {
1009
1007
  return super.create(entity);
@@ -1068,7 +1066,6 @@ var AuthRepository = _AuthRepository;
1068
1066
 
1069
1067
  // src/infrastructure/services/JwtAuthService.ts
1070
1068
  var jwt = __toESM(require("jsonwebtoken"));
1071
- var bcrypt3 = __toESM(require("bcrypt"));
1072
1069
  var import_cca_core9 = require("cca-core");
1073
1070
  var _JwtAuthService = class _JwtAuthService {
1074
1071
  constructor(repository, config) {
@@ -1094,17 +1091,6 @@ var _JwtAuthService = class _JwtAuthService {
1094
1091
  throw new JwtError("JWT secrets required in config");
1095
1092
  }
1096
1093
  }
1097
- async validateUser(email, password) {
1098
- const user = await this.repository.findByEmail(email);
1099
- if (!user) {
1100
- throw new NotFoundError("Invalid credentials");
1101
- }
1102
- const validPassword = await bcrypt3.compare(password, user.password);
1103
- if (!validPassword) {
1104
- throw new ForbiddenError("Invalid credentials");
1105
- }
1106
- return user;
1107
- }
1108
1094
  verifyJwtConfig() {
1109
1095
  if (!this.jwtConfig) throw new JwtError("JWT config not loaded");
1110
1096
  }