This commit is contained in:
@@ -10,10 +10,12 @@ import { AdminLoginTransformer } from '../transformers/admin-login.transformer';
|
||||
import { UserLoginTransformer } from '../transformers/user-login.transformer';
|
||||
import { OtpService } from './otp.service';
|
||||
import { normalizePhoneToLocal } from 'src/modules/util/phone.util';
|
||||
import { CacheService } from 'src/modules/util/cache.service';
|
||||
import { PermissionService } from 'src/modules/roles/providers/permissions.service';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
readonly ADMIN_PERMISSIONS_KEY = 'admin-perms';
|
||||
readonly ADMIN_PERMISSIONS_EXPIRATION_TIME = 1800;
|
||||
|
||||
constructor(
|
||||
private readonly smsService: SmsService,
|
||||
@@ -21,8 +23,10 @@ export class AuthService {
|
||||
private readonly tokensService: TokensService,
|
||||
private readonly adminRepository: AdminRepository,
|
||||
private readonly otpService: OtpService,
|
||||
private readonly cacheService: CacheService,
|
||||
private readonly permissionService: PermissionService,
|
||||
) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
async requestOtp(dto: RequestOtpDto, isAdmin: boolean) {
|
||||
@@ -33,9 +37,9 @@ export class AuthService {
|
||||
}
|
||||
|
||||
const code = this.generateOtpCode();
|
||||
|
||||
|
||||
await this.otpService.set(phone, code)
|
||||
|
||||
|
||||
// await this.smsService.sendotp(phone, code);
|
||||
|
||||
return { code };
|
||||
@@ -82,6 +86,10 @@ export class AuthService {
|
||||
|
||||
const tokens = await this.tokensService.generateAccessAndRefreshToken(admin.id, true);
|
||||
|
||||
const permissionNames = admin.role.permissions.map(p => p.name);
|
||||
|
||||
|
||||
|
||||
const adminResponse = await AdminLoginTransformer.transform(admin);
|
||||
|
||||
return { tokens, admin: adminResponse };
|
||||
@@ -103,4 +111,30 @@ export class AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
async setAdminPermissionsInCache(adminId: string, permissionNames: string[]) {
|
||||
await this.cacheService.set(
|
||||
this.getAdminPermissionsCacheKey(adminId),
|
||||
JSON.stringify(permissionNames),
|
||||
this.ADMIN_PERMISSIONS_EXPIRATION_TIME,
|
||||
);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
async getAdminPermissionsFromCache(adminId: string): Promise<string[]> {
|
||||
const cacheKey = this.getAdminPermissionsCacheKey(adminId);
|
||||
const cached = await this.cacheService.get<string>(cacheKey);
|
||||
|
||||
if (cached !== undefined) {
|
||||
return JSON.parse(cached) as string[];
|
||||
}
|
||||
|
||||
const permissions = await this.permissionService.getAdminPermissionsName(adminId);
|
||||
await this.setAdminPermissionsInCache(adminId, permissions);
|
||||
|
||||
return permissions;
|
||||
}
|
||||
|
||||
private getAdminPermissionsCacheKey(adminId: string): string {
|
||||
return `admin:${adminId}:permissions`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user