feat(auth): use reflector for service injection in guards

The commit refactors how services are injected in UserGuard and AdminGuard inside the auth module. By leveraging the Reflector class from NestJS, it improves the ability to retrieve metadata about these guards, potentially aiding in debugging and understanding code flow.
This commit is contained in:
Mathis H (Avnyr) 2024-07-15 11:41:39 +02:00
parent 007cee5951
commit dea5f8c48b
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -4,13 +4,18 @@ import { CredentialsService } from "src/credentials/credentials.service";
import { DrizzleService } from "src/drizzle/drizzle.service";
import { UsersTable } from "src/schema";
import { eq } from "drizzle-orm";
import { Reflector } from "@nestjs/core";
@Injectable()
export class UserGuard implements CanActivate {
constructor(
private readonly credentialService: CredentialsService,
private readonly databaseService: DrizzleService
) {}
private readonly credentialService: CredentialsService;
private readonly databaseService: DrizzleService;
constructor() {
const reflector = new Reflector();
this.credentialService = reflector.get<CredentialsService>('CredentialsService', UserGuard);
this.databaseService = reflector.get<DrizzleService>('DrizzleService', UserGuard);
}
async canActivate(
context: ExecutionContext,
@ -44,10 +49,14 @@ export class UserGuard implements CanActivate {
@Injectable()
export class AdminGuard implements CanActivate {
constructor(
private readonly credentialService: CredentialsService,
private readonly databaseService: DrizzleService
) {}
private readonly credentialService: CredentialsService;
private readonly databaseService: DrizzleService;
constructor() {
const reflector = new Reflector();
this.credentialService = reflector.get<CredentialsService>('CredentialsService', UserGuard);
this.databaseService = reflector.get<DrizzleService>('DrizzleService', UserGuard);
}
async canActivate(
context: ExecutionContext,