This commit is contained in:
LeoMortari
2025-09-15 01:41:45 -03:00
parent 6866acda89
commit 77777f0aab

View File

@@ -7,18 +7,26 @@ import type { JwtPayload } from './keycloak.strategy';
export class KeycloakAuthGuard extends AuthGuard('jwt') { export class KeycloakAuthGuard extends AuthGuard('jwt') {
private readonly logger = new Logger(KeycloakAuthGuard.name); private readonly logger = new Logger(KeycloakAuthGuard.name);
handleRequest<TUser = JwtPayload>(err: any, user: JwtPayload): TUser { handleRequest<TUser = JwtPayload>(
this.logger.log('KeycloakAuthGuard.handleRequest'); err: unknown,
console.log(user); user: JwtPayload | false,
console.log(err); info: unknown,
context: import('@nestjs/common').ExecutionContext,
): TUser {
this.logger.log(`User: ${JSON.stringify(user)}`);
this.logger.log(`Error: ${JSON.stringify(err)}`);
this.logger.log(`Info: ${JSON.stringify(info)}`);
if (err || !user) { if (err || !user) {
if (err instanceof UnauthorizedException) { if (err instanceof UnauthorizedException) {
throw err; throw err;
} }
throw new UnauthorizedException('Usuário não autenticado'); throw new UnauthorizedException('Usuário não autenticado');
} }
return user as unknown as TUser; const request = context.switchToHttp().getRequest<{ user?: JwtPayload }>();
request.user = user;
return user as TUser;
} }
} }