import { ExecutionContext , Injectable } from '@nestjs/common' ; import { Reflector } from '@nestjs/core' ; import { AuthGuard } from '@nestjs/passport' ; import { HttpUnauthorizedError } from '../../../common/errors/unauthorized.error' ; import { Permission } from '../../../common/generated-types' ; import { PERMISSIONS_METADATA_KEY } from '../../decorators/allow.decorator' ; class classdesc example UseGuards @ Injectable ( ) export class JwtAuthGuard extends AuthGuard ( 'jwt' ) { constructor ( private reflector ) { super ( ) ; } canActivate ( context ) { const permissions = this . reflector . get < Permission [ ] > ( PERMISSIONS_METADATA_KEY , context . getHandler ( ) ) ; const isPublic = ! ! permissions && permissions . includes ( Permission . Public ) ; const hasOwnerPermission = ! ! permissions && permissions . includes ( Permission . Owner ) ; if ( ! permissions || isPublic ) { console . log ( '没设置权限' ) ; return true ; } else { return super . canActivate ( context ) ; } } function description handleRequest ( error , user , errInfo ) { if ( user && ! error && ! errInfo ) { return user ; } else { throw error || new HttpUnauthorizedError ( undefined , errInfo && errInfo . message ) ; } } }