import { Injectable, CanActivate, ExecutionContext, HttpException, HttpStatus } from '@nestjs/common'

@Injectable()
export class AdminGuard implements CanActivate {
  canActivate(context: ExecutionContext): boolean {
    const request = context.switchToHttp().getRequest()
    const user = request.user
    if (!user) {
      throw new HttpException('未登录', HttpStatus.UNAUTHORIZED)
    }
    if (user.role !== 'admin') {
      throw new HttpException('无权限访问', HttpStatus.FORBIDDEN)
    }
    return true
  }
}
