mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-25 12:33:48 +00:00
feat: admin endpoints
This commit is contained in:
22
src/modules/auth/guards/admin.guard.ts
Normal file
22
src/modules/auth/guards/admin.guard.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common'
|
||||
|
||||
import { JwtAuthGuard } from './jwt-auth.guard'
|
||||
|
||||
@Injectable()
|
||||
export class AdminGuard extends JwtAuthGuard {
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const canActivate = await super.canActivate(context)
|
||||
|
||||
if (!canActivate) {
|
||||
return false
|
||||
}
|
||||
const request = context.switchToHttp().getRequest()
|
||||
const user = request.user
|
||||
|
||||
if (!user.isAdmin) {
|
||||
throw new UnauthorizedException('You do not have permission to access this resource')
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user