import { CanActivate , ExecutionContext , Injectable } from '@nestjs/common' ; import { AuthGuard } from '@nestjs/passport' ; import { getNestExecutionContextRequest } from '~/utils/nest.util' ; /**
 * 区分游客和主人的守卫
 */ @ Injectable ( ) export class RolesGuard extends AuthGuard ( 'jwt' ) implements CanActivate { async canActivate ( context ) { let isMaster = false ; const request = this . getRequest ( context ) ; if ( request . headers [ 'authorization' ] ) { try { isMaster = ( await super . canActivate ( context ) ) as boolean ; } catch { } } request . isGuest = ! isMaster ; request . isMaster = isMaster ; return true ; } getRequest ( context ) { return getNestExecutionContextRequest ( context ) ; } }