Skip to content

Commit

Permalink
fix: forbidden
Browse files Browse the repository at this point in the history
  • Loading branch information
1tpp committed Aug 3, 2023
1 parent b308465 commit f45e6bc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
2 changes: 0 additions & 2 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ export class AuthService {
password: await bcrypt.hashSync(password, salt),
});

console.log(newUser);

return await this.generateToken({
userId: newUser._id,
role: newUser.role,
Expand Down
14 changes: 7 additions & 7 deletions src/common/guards/roles.guard.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';

import { ROLES_KEY } from '../decorators/roles.decorator';
import { Role } from '../enums/role.enum';

@Injectable()
export class RolesGuard implements CanActivate {
constructor(private reflector: Reflector) {}

canActivate(context: ExecutionContext): boolean {
const requiredRoles = this.reflector.getAllAndOverride<Role[]>(ROLES_KEY, [
context.getHandler(),
context.getClass(),
]);
const requiredRoles = this.reflector.getAllAndOverride<string[]>(
ROLES_KEY,
[context.getHandler(), context.getClass()],
);

if (!requiredRoles) {
return true;
}

const { user } = context.switchToHttp().getRequest();
return requiredRoles.some((role) => user.roles?.includes(role));
return requiredRoles.some((role) => user.role?.includes(role));
}
}
6 changes: 3 additions & 3 deletions src/tickets/tickets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class TicketsController {
constructor(private readonly ticketsService: TicketsService) {}

@Post()
@Roles(Role.Provider)
@Roles(Role.Provider, Role.Admin)
@UseGuards(RolesGuard)
async create(@Body() createTicketDto: CreateTicketDto) {
const newTicket = await this.ticketsService.create(createTicketDto);
Expand Down Expand Up @@ -61,7 +61,7 @@ export class TicketsController {
}

@Post()
@Roles(Role.Provider)
@Roles(Role.Provider, Role.Admin)
@UseGuards(RolesGuard)
@Patch(':id')
async update(
Expand All @@ -77,7 +77,7 @@ export class TicketsController {
}

@Post()
@Roles(Role.Provider)
@Roles(Role.Provider, Role.Admin)
@UseGuards(RolesGuard)
@Delete(':id')
async remove(@Param('id') id: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { UsersService } from './users.service';
export class UsersController {
constructor(private readonly usersService: UsersService) {}

@Roles(Role.Provider, Role.User)
@Roles(Role.Provider, Role.User, Role.Admin)
@UseGuards(RolesGuard)
@Get('/profile')
async getProfile(@GetUser() currentUser) {
Expand Down

0 comments on commit f45e6bc

Please sign in to comment.