cca-auth-module 0.1.66 → 0.1.68

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.
@@ -4,4 +4,5 @@ export declare class UserDTO {
4
4
  name: string;
5
5
  email: string;
6
6
  role: UserRole;
7
+ profileImageUrl?: string;
7
8
  }
@@ -6,5 +6,4 @@ export declare class LoginAdminUseCase implements IBaseService {
6
6
  constructor(repository: AuthRepository);
7
7
  initialize(): Promise<void>;
8
8
  execute(loginDTO: LoginDTO, adminPassword: string): Promise<string>;
9
- private validateLogin;
10
9
  }
@@ -11,5 +11,4 @@ export declare class LoginUseCase implements IBaseService {
11
11
  id: string;
12
12
  accessToken: string;
13
13
  }>;
14
- private validateLogin;
15
14
  }
@@ -3,12 +3,12 @@ import { AuthEntity, UserRole } from "cca-entities";
3
3
  import { AuthRepository } from "../../infrastructure/repository/AuthRepository";
4
4
  export declare class RegisterUseCase implements IBaseService {
5
5
  private readonly repository;
6
- private static readonly SALT_ROUNDS;
6
+ private readonly SALT_ROUNDS;
7
7
  constructor(repository: AuthRepository);
8
8
  initialize(): Promise<void>;
9
9
  execute(email: string, name: string, password: string, role?: UserRole, adminPassword?: string): Promise<AuthEntity | undefined>;
10
10
  private _normalizeInput;
11
11
  private _hashPassword;
12
12
  private _validateAdminRegistration;
13
- private _buildAuthEntity;
13
+ private _buildMappedAuthEntity;
14
14
  }
package/dist/index.d.mts CHANGED
@@ -35,14 +35,14 @@ declare class AuthRepository extends BaseRepository<AuthEntity> implements IExte
35
35
 
36
36
  declare class RegisterUseCase implements IBaseService {
37
37
  private readonly repository;
38
- private static readonly SALT_ROUNDS;
38
+ private readonly SALT_ROUNDS;
39
39
  constructor(repository: AuthRepository);
40
40
  initialize(): Promise<void>;
41
41
  execute(email: string, name: string, password: string, role?: UserRole, adminPassword?: string): Promise<AuthEntity | undefined>;
42
42
  private _normalizeInput;
43
43
  private _hashPassword;
44
44
  private _validateAdminRegistration;
45
- private _buildAuthEntity;
45
+ private _buildMappedAuthEntity;
46
46
  }
47
47
 
48
48
  declare class LoginDTO {
@@ -98,7 +98,6 @@ declare class LoginUseCase implements IBaseService {
98
98
  id: string;
99
99
  accessToken: string;
100
100
  }>;
101
- private validateLogin;
102
101
  }
103
102
 
104
103
  declare class LoginAdminUseCase implements IBaseService {
@@ -106,7 +105,6 @@ declare class LoginAdminUseCase implements IBaseService {
106
105
  constructor(repository: AuthRepository);
107
106
  initialize(): Promise<void>;
108
107
  execute(loginDTO: LoginDTO, adminPassword: string): Promise<string>;
109
- private validateLogin;
110
108
  }
111
109
 
112
110
  declare class LogoutUseCase implements IBaseService {
@@ -192,6 +190,7 @@ declare class UserDTO {
192
190
  name: string;
193
191
  email: string;
194
192
  role: UserRole;
193
+ profileImageUrl?: string;
195
194
  }
196
195
 
197
196
  declare class TwoFactorVerifyUseCase implements IBaseService {
package/dist/index.d.ts CHANGED
@@ -35,14 +35,14 @@ declare class AuthRepository extends BaseRepository<AuthEntity> implements IExte
35
35
 
36
36
  declare class RegisterUseCase implements IBaseService {
37
37
  private readonly repository;
38
- private static readonly SALT_ROUNDS;
38
+ private readonly SALT_ROUNDS;
39
39
  constructor(repository: AuthRepository);
40
40
  initialize(): Promise<void>;
41
41
  execute(email: string, name: string, password: string, role?: UserRole, adminPassword?: string): Promise<AuthEntity | undefined>;
42
42
  private _normalizeInput;
43
43
  private _hashPassword;
44
44
  private _validateAdminRegistration;
45
- private _buildAuthEntity;
45
+ private _buildMappedAuthEntity;
46
46
  }
47
47
 
48
48
  declare class LoginDTO {
@@ -98,7 +98,6 @@ declare class LoginUseCase implements IBaseService {
98
98
  id: string;
99
99
  accessToken: string;
100
100
  }>;
101
- private validateLogin;
102
101
  }
103
102
 
104
103
  declare class LoginAdminUseCase implements IBaseService {
@@ -106,7 +105,6 @@ declare class LoginAdminUseCase implements IBaseService {
106
105
  constructor(repository: AuthRepository);
107
106
  initialize(): Promise<void>;
108
107
  execute(loginDTO: LoginDTO, adminPassword: string): Promise<string>;
109
- private validateLogin;
110
108
  }
111
109
 
112
110
  declare class LogoutUseCase implements IBaseService {
@@ -192,6 +190,7 @@ declare class UserDTO {
192
190
  name: string;
193
191
  email: string;
194
192
  role: UserRole;
193
+ profileImageUrl?: string;
195
194
  }
196
195
 
197
196
  declare class TwoFactorVerifyUseCase implements IBaseService {
package/dist/index.js CHANGED
@@ -181,7 +181,7 @@ var schemas = {
181
181
  /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]/,
182
182
  "Password must contain uppercase, lowercase, number and special character"
183
183
  ),
184
- role: yup.string().required("Role is required").oneOf(Object.values(import_cca_entities.UserRole), "Invalid role specified")
184
+ role: yup.string().oneOf(Object.values(import_cca_entities.UserRole), "Invalid role specified")
185
185
  };
186
186
  var validateEmail = /* @__PURE__ */ __name(async (email, repository) => {
187
187
  try {
@@ -231,7 +231,10 @@ var validateRegisterDTO = /* @__PURE__ */ __name(async (auth, repository) => {
231
231
  }, "validateRegisterDTO");
232
232
  var validateLoginDTO = /* @__PURE__ */ __name(async (data, repository) => {
233
233
  const { email, role, password } = data;
234
- await Promise.all([schemas.role.validate(role), schemas.password.validate(password)]);
234
+ if (role) {
235
+ await schemas.role.validate(role);
236
+ }
237
+ await schemas.password.validate(password);
235
238
  const auth = await validateEmail(email, repository);
236
239
  if (!auth || !auth.password) {
237
240
  throw new NotFoundError("Invalid credentials");
@@ -272,14 +275,10 @@ var _LoginUseCase = class _LoginUseCase {
272
275
  await (0, import_cca_core.validateRepository)(this.repository, (repo) => repo.getAll());
273
276
  }
274
277
  async execute(loginDTO) {
275
- const auth = await this.validateLogin(loginDTO);
278
+ const auth = await validateLoginDTO(loginDTO, this.repository);
276
279
  const accessToken = this.jwtService.generateAccessToken(auth.user, auth.role);
277
280
  return { id: auth.user.id, accessToken };
278
281
  }
279
- async validateLogin(loginDTO) {
280
- const auth = await validateLoginDTO(loginDTO, this.repository);
281
- return auth;
282
- }
283
282
  };
284
283
  __name(_LoginUseCase, "LoginUseCase");
285
284
  var LoginUseCase = _LoginUseCase;
@@ -294,13 +293,9 @@ var _LoginAdminUseCase = class _LoginAdminUseCase {
294
293
  await (0, import_cca_core2.validateRepository)(this.repository, (repo) => repo.getAll());
295
294
  }
296
295
  async execute(loginDTO, adminPassword) {
297
- const auth = await this.validateLogin(loginDTO, adminPassword);
298
- return auth.user.id;
299
- }
300
- async validateLogin(loginDTO, adminPassword) {
301
296
  const auth = await validateLoginDTO(loginDTO, this.repository);
302
297
  await validateAdminSecret(adminPassword);
303
- return auth;
298
+ return auth.user.id;
304
299
  }
305
300
  };
306
301
  __name(_LoginAdminUseCase, "LoginAdminUseCase");
@@ -362,6 +357,9 @@ __decorateClass([
362
357
  __decorateClass([
363
358
  (0, import_classes.AutoMap)()
364
359
  ], _UserDTO.prototype, "role", 2);
360
+ __decorateClass([
361
+ (0, import_classes.AutoMap)()
362
+ ], _UserDTO.prototype, "profileImageUrl", 2);
365
363
  var UserDTO = _UserDTO;
366
364
 
367
365
  // src/application/dtos/AdminDTO.ts
@@ -413,7 +411,8 @@ function createUserMappings(mapper2) {
413
411
  UserDTO,
414
412
  (0, import_core.forMember)((dest) => dest.id, (0, import_core.mapFrom)((src) => src.id)),
415
413
  (0, import_core.forMember)((dest) => dest.name, (0, import_core.mapFrom)((src) => src.name)),
416
- (0, import_core.forMember)((dest) => dest.email, (0, import_core.mapFrom)((src) => src.email))
414
+ (0, import_core.forMember)((dest) => dest.email, (0, import_core.mapFrom)((src) => src.email)),
415
+ (0, import_core.forMember)((dest) => dest.profileImageUrl, (0, import_core.mapFrom)((src) => getProfileImageUrl(src)))
417
416
  );
418
417
  (0, import_core.createMap)(
419
418
  mapper2,
@@ -423,6 +422,11 @@ function createUserMappings(mapper2) {
423
422
  (0, import_core.forMember)((dest) => dest.name, (0, import_core.mapFrom)((src) => src.name)),
424
423
  (0, import_core.forMember)((dest) => dest.email, (0, import_core.mapFrom)((src) => src.email))
425
424
  );
425
+ const getProfileImageUrl = /* @__PURE__ */ __name((src) => {
426
+ const image = src.images?.[0];
427
+ if (!image) return void 0;
428
+ return image.mdUrl ?? image.smUrl ?? image.lgUrl ?? image.thumbUrl ?? image.originalUrl ?? image.xlUrl;
429
+ }, "getProfileImageUrl");
426
430
  }
427
431
  __name(createUserMappings, "createUserMappings");
428
432
 
@@ -436,26 +440,21 @@ createUserMappings(mapper);
436
440
  var _RegisterUseCase = class _RegisterUseCase {
437
441
  constructor(repository) {
438
442
  this.repository = repository;
443
+ this.SALT_ROUNDS = 10;
439
444
  }
440
445
  async initialize() {
441
446
  await (0, import_cca_core4.validateRepository)(this.repository, (repo) => repo.getAll());
442
447
  }
443
448
  async execute(email, name, password, role = import_cca_entities3.UserRole.GUEST, adminPassword) {
444
449
  try {
445
- const normalizedDTO = this._normalizeInput({
446
- email,
447
- name,
448
- password,
449
- role,
450
- adminPassword
451
- });
450
+ const normalizedDTO = this._normalizeInput({ email, name, password, role, adminPassword });
452
451
  const isAdminUser = await this._validateAdminRegistration(
453
452
  normalizedDTO.role,
454
453
  normalizedDTO.adminPassword
455
454
  );
456
455
  await validateRegisterDTO(normalizedDTO, this.repository);
457
456
  const hashedPassword = await this._hashPassword(normalizedDTO.password);
458
- const authEntity = await this._buildAuthEntity(normalizedDTO, hashedPassword, isAdminUser);
457
+ const authEntity = this._buildMappedAuthEntity(normalizedDTO, hashedPassword, isAdminUser);
459
458
  return this.repository.create(authEntity);
460
459
  } catch (error) {
461
460
  throw new RegistrationError(
@@ -467,36 +466,30 @@ var _RegisterUseCase = class _RegisterUseCase {
467
466
  return {
468
467
  name: dto.name.trim(),
469
468
  email: dto.email.trim().toLowerCase(),
470
- role: dto.role,
471
469
  password: dto.password.trim(),
470
+ role: dto.role,
472
471
  adminPassword: dto.adminPassword?.trim()
473
472
  };
474
473
  }
475
474
  async _hashPassword(password) {
476
- return bcrypt2.hash(password, _RegisterUseCase.SALT_ROUNDS);
475
+ return bcrypt2.hash(password, this.SALT_ROUNDS);
477
476
  }
478
477
  async _validateAdminRegistration(role, adminPassword) {
479
- if (role !== import_cca_entities3.UserRole.ADMIN) {
480
- return false;
481
- }
478
+ if (role !== import_cca_entities3.UserRole.ADMIN) return false;
482
479
  if (!adminPassword) {
483
480
  throw new UnauthorizedError("Admin password is required for admin registration");
484
481
  }
485
482
  await validateAdminSecret(adminPassword);
486
483
  return true;
487
484
  }
488
- async _buildAuthEntity(dto, hashedPassword, isAdmin) {
489
- let authEntity;
485
+ _buildMappedAuthEntity(dto, hashedPassword, isAdmin) {
486
+ const authEntity = mapper.map(dto, RegisterDTO, import_cca_entities3.AuthEntity);
487
+ const userOrAdminEntity = isAdmin ? mapper.map(dto, RegisterDTO, import_cca_entities3.AdminEntity) : mapper.map(dto, RegisterDTO, import_cca_entities3.UserEntity);
488
+ userOrAdminEntity.updatedAt = void 0;
490
489
  if (isAdmin) {
491
- authEntity = mapper.map(dto, RegisterDTO, import_cca_entities3.AuthEntity);
492
- const adminEntity = mapper.map(dto, RegisterDTO, import_cca_entities3.AdminEntity);
493
- adminEntity.updatedAt = void 0;
494
- authEntity.admin = adminEntity;
490
+ authEntity.admin = userOrAdminEntity;
495
491
  } else {
496
- authEntity = mapper.map(dto, RegisterDTO, import_cca_entities3.AuthEntity);
497
- const userEntity = mapper.map(dto, RegisterDTO, import_cca_entities3.UserEntity);
498
- userEntity.updatedAt = void 0;
499
- authEntity.user = userEntity;
492
+ authEntity.user = userOrAdminEntity;
500
493
  }
501
494
  authEntity.password = hashedPassword;
502
495
  authEntity.refreshToken = "";
@@ -504,7 +497,6 @@ var _RegisterUseCase = class _RegisterUseCase {
504
497
  }
505
498
  };
506
499
  __name(_RegisterUseCase, "RegisterUseCase");
507
- _RegisterUseCase.SALT_ROUNDS = 10;
508
500
  var RegisterUseCase = _RegisterUseCase;
509
501
 
510
502
  // src/application/useCase/RefreshTokenUseCase.ts
@@ -743,7 +735,7 @@ var _AuthController = class _AuthController {
743
735
  constructor(loginUseCase, adminLoginUseCase, logoutUseCase, registerUseCase, refreshTokenUseCase, twoFactorSetupUseCase, twoFactorEnableUseCase, twoFactorVerifyUseCase, twoFactorDisableUseCase) {
744
736
  this.login = /* @__PURE__ */ __name(async (req, res, next) => {
745
737
  try {
746
- const { adminPassword, ...loginDTO } = req.body;
738
+ const loginDTO = req.body;
747
739
  const result = await this.loginUseCase.execute(loginDTO);
748
740
  res.status(201).json(
749
741
  {