cca-auth-module 0.1.96 → 0.1.98
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/application/useCase/TwoFactorVerifyUseCase.d.ts +0 -1
- package/dist/application/validators/authValidation.d.ts +2 -2
- package/dist/index.d.mts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +48 -53
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +49 -54
- package/dist/index.mjs.map +1 -1
- package/dist/infrastructure/repository/AuthRepository.d.ts +1 -1
- package/package.json +1 -1
|
@@ -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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
279
|
+
const isAdmin = !!providedAdminPassword;
|
|
280
|
+
if (isAdmin) {
|
|
280
281
|
await validateAdminSecret(providedAdminPassword);
|
|
281
282
|
}
|
|
282
|
-
const
|
|
283
|
-
const
|
|
284
|
-
if (!
|
|
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(
|
|
288
|
+
const accessToken = this.jwtService.generateAccessToken(account, auth.role);
|
|
288
289
|
const expiresAt = (0, import_jwt_decode.jwtDecode)(accessToken).exp;
|
|
289
|
-
return { id:
|
|
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
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
if (!
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
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
|
-
|
|
657
|
-
}
|
|
646
|
+
// private mapUserToDTO(user: UserEntity): UserDTO {
|
|
647
|
+
// return mapper.map(user, UserEntity, UserDTO);
|
|
648
|
+
// }
|
|
658
649
|
async updateUserStatus(auth) {
|
|
659
|
-
auth.user
|
|
660
|
-
|
|
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
|
-
|
|
669
|
-
|
|
670
|
-
|
|
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,24 +995,15 @@ var _AuthRepository = class _AuthRepository extends import_cca_core8.BaseReposit
|
|
|
994
995
|
constructor(repository) {
|
|
995
996
|
super(repository);
|
|
996
997
|
}
|
|
997
|
-
async findByEmail(email
|
|
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) {
|
|
1009
1003
|
return super.create(entity);
|
|
1010
1004
|
}
|
|
1011
1005
|
async findByUserId(userId) {
|
|
1012
|
-
|
|
1013
|
-
return await query.getOne();
|
|
1014
|
-
;
|
|
1006
|
+
return await this.repository.createQueryBuilder("auth").leftJoinAndSelect("auth.user", "user").leftJoinAndSelect("auth.admin", "admin").addSelect("auth.twoFactorSecret").where("user.id = :id OR admin.id = :id", { userId }).getOne();
|
|
1015
1007
|
}
|
|
1016
1008
|
async findByUseAdminId(userId, isAdmin = false) {
|
|
1017
1009
|
const query = this.repository.createQueryBuilder("auth").addSelect("auth.twoFactorSecret");
|
|
@@ -1027,8 +1019,11 @@ var _AuthRepository = class _AuthRepository extends import_cca_core8.BaseReposit
|
|
|
1027
1019
|
if (!auth) {
|
|
1028
1020
|
throw new NotFoundError("Auth not found");
|
|
1029
1021
|
}
|
|
1030
|
-
auth.
|
|
1031
|
-
|
|
1022
|
+
const account = auth.user ?? auth.admin;
|
|
1023
|
+
if (account) {
|
|
1024
|
+
auth.refreshToken = "";
|
|
1025
|
+
account.isActive = false;
|
|
1026
|
+
}
|
|
1032
1027
|
await this.update(auth.id, auth);
|
|
1033
1028
|
}
|
|
1034
1029
|
async updateTwoFactorSecret(userId, secret) {
|