cr
This commit is contained in:
@@ -4,15 +4,15 @@ import {
|
||||
ExecutionContext,
|
||||
UnauthorizedException,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { Request } from 'express';
|
||||
} from "@nestjs/common";
|
||||
import { Reflector } from "@nestjs/core";
|
||||
import { AuthGuard } from "@nestjs/passport";
|
||||
import { Request } from "express";
|
||||
import {
|
||||
IS_PUBLIC_KEY,
|
||||
ROLES_KEY,
|
||||
PERMISSIONS_KEY,
|
||||
} from '../../../common/decorators';
|
||||
} from "../../../common/decorators";
|
||||
|
||||
interface AuthenticatedUser {
|
||||
id: string;
|
||||
@@ -25,14 +25,14 @@ interface AuthenticatedUser {
|
||||
* JWT Auth Guard - Validates JWT token
|
||||
*/
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
export class JwtAuthGuard extends AuthGuard("jwt") {
|
||||
constructor(private reflector: Reflector) {
|
||||
super();
|
||||
}
|
||||
|
||||
canActivate(context: ExecutionContext) {
|
||||
const request = context.switchToHttp().getRequest<Request>();
|
||||
if (request?.method === 'OPTIONS') {
|
||||
if (request?.method === "OPTIONS") {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -55,10 +55,10 @@ export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
info: any,
|
||||
): TUser {
|
||||
if (err || !user) {
|
||||
if (info?.name === 'TokenExpiredError') {
|
||||
throw new UnauthorizedException('TOKEN_EXPIRED');
|
||||
if (info?.name === "TokenExpiredError") {
|
||||
throw new UnauthorizedException("TOKEN_EXPIRED");
|
||||
}
|
||||
throw err || new UnauthorizedException('AUTH_REQUIRED');
|
||||
throw err || new UnauthorizedException("AUTH_REQUIRED");
|
||||
}
|
||||
return user;
|
||||
}
|
||||
@@ -73,7 +73,7 @@ export class RolesGuard implements CanActivate {
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const req = context.switchToHttp().getRequest<Request>();
|
||||
if (req?.method === 'OPTIONS') {
|
||||
if (req?.method === "OPTIONS") {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ export class RolesGuard implements CanActivate {
|
||||
|
||||
const hasRole = requiredRoles.some((role) => user.roles.includes(role));
|
||||
if (!hasRole) {
|
||||
throw new ForbiddenException('PERMISSION_DENIED');
|
||||
throw new ForbiddenException("PERMISSION_DENIED");
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -110,7 +110,7 @@ export class PermissionsGuard implements CanActivate {
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const req = context.switchToHttp().getRequest<Request>();
|
||||
if (req?.method === 'OPTIONS') {
|
||||
if (req?.method === "OPTIONS") {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ export class PermissionsGuard implements CanActivate {
|
||||
);
|
||||
|
||||
if (!hasPermission) {
|
||||
throw new ForbiddenException('PERMISSION_DENIED');
|
||||
throw new ForbiddenException("PERMISSION_DENIED");
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user