Fix role guard functionality

master
nicekid1 2 months ago
parent a44695e5fd
commit 5e1fe81273
  1. 9
      src/guard/role.guard.ts
  2. 3
      src/users/users.controller.ts
  3. 2
      src/users/users.service.ts

@ -6,20 +6,25 @@ export class RoleGuard implements CanActivate {
constructor( constructor(
private jwtService: JwtService, private jwtService: JwtService,
) {} ) {}
async canActivate(context: ExecutionContext): Promise<boolean> { async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest(); const request = context.switchToHttp().getRequest();
const token = request.headers["authorization"]?.split(" ")[1]; const token = request.headers["authorization"]?.split(" ")[1];
if (!token) throw new UnauthorizedException("Authorization token is missing"); if (!token) throw new UnauthorizedException("Authorization token is missing");
try { try {
const decoded = this.jwtService.verify(token, { secret: process.env.JWT_SECRET }); const decoded = this.jwtService.verify(token, { secret: process.env.JWT_SECRET });
const userRole = decoded.role; const userRole = decoded.role;
if (userRole !== "admin") { if (userRole !== "admin") {
throw new UnauthorizedException("You do not have the required role"); throw new UnauthorizedException(`You do not have the required role. Current role: ${userRole}`);
} }
request.user = decoded; request.user = decoded;
return true; return true;
} catch (error) { } catch (error) {
throw new UnauthorizedException("Invalid or expired token"); console.log(error);
throw new UnauthorizedException(error.response);
} }
} }
} }

@ -5,6 +5,7 @@ import { CreateUserDto } from "./dto/create-user.dto";
import { LoginUserDto } from "./dto/login-user.dto"; import { LoginUserDto } from "./dto/login-user.dto";
import { JwtAuthGuard } from "src/guard/auth.guard"; import { JwtAuthGuard } from "src/guard/auth.guard";
import { UpdateUserDto } from "./dto/update-user.dto"; import { UpdateUserDto } from "./dto/update-user.dto";
import { RoleGuard } from "src/guard/role.guard";
@Controller("user") @Controller("user")
export class UsersController { export class UsersController {
@ -35,7 +36,7 @@ export class UsersController {
return this.usersService.editProfile(userId, updateUserDto); return this.usersService.editProfile(userId, updateUserDto);
} }
//get users list (admin) //get users list (admin)
@UseGuards(JwtAuthGuard) @UseGuards(RoleGuard)
@Get("users") @Get("users")
async findAll(): Promise<User[]> { async findAll(): Promise<User[]> {
return this.usersService.findAll(); return this.usersService.findAll();

@ -50,7 +50,7 @@ export class UsersService {
} }
const token = this.jwtService.sign( const token = this.jwtService.sign(
{ id: user.id }, { id: user.id,role:user.role },
{ {
secret: this.configService.get<string>("JWT_SECRET"), secret: this.configService.get<string>("JWT_SECRET"),
expiresIn: this.configService.get<string | number>("JWT_EXPIRES") || "1h", expiresIn: this.configService.get<string | number>("JWT_EXPIRES") || "1h",

Loading…
Cancel
Save