import { Injectable, ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
  constructor(private reflector: Reflector) {
    super();
  }

  canActivate(context: ExecutionContext) {
    // 可以通过装饰器设置是否需要认证
    const requireAuth = this.reflector.getAllAndOverride<boolean>('requireAuth', [
      context.getHandler(),
      context.getClass(),
    ]);

    if (requireAuth === false) {
      return true;
    }

    return super.canActivate(context);
  }
}
