developer239/nest-rest-api

View on GitHub
src/modules/auth/jwt/jwt.service.ts

Summary

Maintainability
A
0 mins
Test Coverage
D
66%
import { Injectable, UnauthorizedException } from '@nestjs/common'
import { PassportStrategy } from '@nestjs/passport'
import { InjectRepository } from '@nestjs/typeorm'
import { Strategy, ExtractJwt } from 'passport-jwt'
import { IJwtPayload } from 'src/modules/auth/jwt/jwt.types'
import { User } from 'src/modules/auth/user/user.entity'
import { UserRepository } from 'src/modules/auth/user/user.repository'
import { EnvService } from 'src/modules/env/env.service'

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(
    @InjectRepository(UserRepository)
    private readonly userRepository: UserRepository,
    private readonly envService: EnvService
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: envService.getString('JWT_SECRET'),
    })
  }

  async validate(payload: IJwtPayload): Promise<User> {
    const { username } = payload
    const user = await this.userRepository.findOne({ username })

    if (!user) {
      throw new UnauthorizedException()
    }

    return user
  }
}