1
0
forked from Yara724/api

Added Joi for schema validation, tidied up envs

This commit is contained in:
SepehrYahyaee
2026-05-30 10:37:23 +03:30
parent 10df869efb
commit 6ac0bf060e
12 changed files with 176 additions and 59 deletions

View File

@@ -78,8 +78,7 @@ export class ActorAuthService {
res = await this.fieldExpertDbService.findOne({
_id: new Types.ObjectId(userId),
});
else
res = await this.fieldExpertDbService.findOne({ email: username });
else res = await this.fieldExpertDbService.findOne({ email: username });
break;
case RoleEnum.REGISTRAR:
if (username == null && userId)
@@ -139,7 +138,7 @@ export class ActorAuthService {
};
const access_token = this.jwtService.sign(payload, {
secret: `${process.env.SECRET}`,
secret: `${process.env.JWT_SECRET}`,
expiresIn: "1h",
});
@@ -159,9 +158,7 @@ export class ActorAuthService {
throw new UnauthorizedException("user not assigned to this role");
}
if (!(await this.hashService.compare(pass, user.password))) {
throw new UnauthorizedException(
"password is incorrect or access Denied",
);
throw new UnauthorizedException("password is incorrect or access Denied");
}
return user;
}
@@ -225,7 +222,7 @@ export class ActorAuthService {
firstName: body.firstName,
lastName: body.lastName,
phone: body.phone,
mobile:body.mobile,
mobile: body.mobile,
city: body.city,
state: body.state,
address: body.address,
@@ -445,13 +442,7 @@ export class ActorAuthService {
"state",
"address",
],
field_expert: [
"firstName",
"lastName",
"email",
"phone",
"mobile",
],
field_expert: ["firstName", "lastName", "email", "phone", "mobile"],
registrar: ["email"],
};
@@ -477,7 +468,11 @@ export class ActorAuthService {
}
// fetch user detail (document or plain object)
const document = await this.dynamicDbController(role, currentUser.role === "company" ? currentUser.username : null, userId);
const document = await this.dynamicDbController(
role,
currentUser.role === "company" ? currentUser.username : null,
userId,
);
if (!document) throw new NotFoundException("Profile not found");

View File

@@ -75,7 +75,7 @@ export class UserAuthService {
role: "user",
};
const accToken = this.jwtService.sign(payload, {
secret: `${process.env.SECRET}`,
secret: `${process.env.JWT_SECRET}`,
});
await this.userDbService.findOneAndUpdate(
{ username: user.username },
@@ -162,10 +162,7 @@ export class UserAuthService {
process.env.AUTH_SMS_TEMPLATE,
);
if (!ok) {
throw new HttpException(
"auth sms send failed",
HttpStatus.BAD_GATEWAY,
);
throw new HttpException("auth sms send failed", HttpStatus.BAD_GATEWAY);
}
this.logger.log(
`Auth OTP SMS accepted by provider phone=${mobile} otp=${otp}`,

View File

@@ -49,9 +49,9 @@ import { CaptchaModule } from "src/captcha/captcha.module";
{ name: ClaimCase.name, schema: ClaimCaseSchema },
]),
JwtModule.register({
signOptions: { expiresIn: "1h" },
signOptions: { expiresIn: "1h" }, // TODO: MAKE IT ENV
global: true,
secret: `${process.env.SECRET}`,
secret: `${process.env.JWT_SECRET}`,
}),
],
providers: [

View File

@@ -33,7 +33,7 @@ export class LocalActorAuthGuard implements CanActivate {
try {
const payload = await this.jwtService.verifyAsync(token, {
secret: `${process.env.SECRET}`,
secret: `${process.env.JWT_SECRET}`,
});
if (

View File

@@ -31,7 +31,7 @@ export class ClaimAccessGuard implements CanActivate {
try {
const payload = await this.jwtService.verifyAsync(token, {
secret: `${process.env.SECRET}`,
secret: `${process.env.JWT_SECRET}`,
});
// Allow users to pass through (they will be checked by service methods)
@@ -81,7 +81,10 @@ export class ClaimAccessGuard implements CanActivate {
throw new UnauthorizedException("Invalid role");
} catch (error) {
if (error instanceof ForbiddenException || error instanceof UnauthorizedException) {
if (
error instanceof ForbiddenException ||
error instanceof UnauthorizedException
) {
throw error;
}
throw new UnauthorizedException();
@@ -124,4 +127,3 @@ export class ClaimAccessGuard implements CanActivate {
return type === "Bearer" ? token : undefined;
}
}

View File

@@ -22,10 +22,13 @@ export class GlobalGuard implements CanActivate {
try {
const payload = await this.jwtService.verifyAsync(token, {
secret: `${process.env.SECRET}`,
secret: `${process.env.JWT_SECRET}`,
});
if (payload.role !== RoleEnum.USER && payload.role !== RoleEnum.FIELD_EXPERT) {
if (
payload.role !== RoleEnum.USER &&
payload.role !== RoleEnum.FIELD_EXPERT
) {
throw new UnauthorizedException();
}

View File

@@ -24,7 +24,7 @@ export class SettingsJwtGuard implements CanActivate {
try {
const payload = await this.jwtService.verifyAsync(token, {
secret: `${process.env.SECRET}`,
secret: `${process.env.JWT_SECRET}`,
});
(request as any).user = payload;
(request as any).identity = payload;