import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
  handleRequest<User = unknown>(err: unknown, user: User, info: unknown): User {
    if (err) {
      throw err;
    }

    if (!user) {
      const errorMessage = info instanceof Error ? info.message : 'Unauthorized';
      throw new UnauthorizedException(errorMessage);
    }

    return user;
  }
}
