jwt-auth.guard.ts 651 B

12345678910111213141516171819202122
  1. import { ExecutionContext, Injectable } from '@nestjs/common'
  2. import { Reflector } from '@nestjs/core'
  3. import { AuthGuard } from '@nestjs/passport'
  4. import { IS_PUBLIC_KEY } from './public.decorator'
  5. @Injectable()
  6. export class JwtAuthGuard extends AuthGuard('jwt') {
  7. constructor(private reflector: Reflector) {
  8. super()
  9. }
  10. canActivate(context: ExecutionContext) {
  11. const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
  12. context.getHandler(),
  13. context.getClass()
  14. ])
  15. if (isPublic) {
  16. return true
  17. }
  18. return super.canActivate(context)
  19. }
  20. }