refactor: Simplify jwksRsa import and enhance username extraction logic in JwtAuthGuard
This commit is contained in:
parent
7b43387a0c
commit
3577925932
@ -1,11 +1,11 @@
|
||||
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import * as jwt from "jsonwebtoken";
|
||||
import JwksRsa, * as jwksRsa from "jwks-rsa";
|
||||
import * as jwksRsa from "jwks-rsa";
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
private jwksClient: JwksRsa.JwksClient;
|
||||
private jwksClient: jwksRsa.JwksClient;
|
||||
|
||||
constructor(configService: ConfigService) {
|
||||
this.jwksClient = jwksRsa({
|
||||
|
@ -14,7 +14,7 @@ export class JwtAuthGuard implements CanActivate {
|
||||
private readonly userService: UserService,
|
||||
private readonly authService: AuthService,
|
||||
private readonly configService: ConfigService,
|
||||
) {}
|
||||
) { }
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
@ -30,7 +30,9 @@ export class JwtAuthGuard implements CanActivate {
|
||||
let user = await this.userService.findOrCreate({
|
||||
id: jwtPayload.sub.toString(),
|
||||
username:
|
||||
jwtPayload[this.configService.get("auth.usernameField")],
|
||||
jwtPayload[this.configService.get("auth.usernameField")] ||
|
||||
jwtPayload["preferred_username"] ||
|
||||
jwtPayload["email"],
|
||||
});
|
||||
|
||||
request.user = user;
|
||||
|
Loading…
Reference in New Issue
Block a user