All files / auth/strategies refresh-token.strategy.ts

92.85% Statements 13/14
0% Branches 0/1
100% Functions 2/2
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 342x 2x 2x 2x 2x             2x         2x 2x   2x             2x 2x     2x      
import { Injectable, UnauthorizedException } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { PassportStrategy } from '@nestjs/passport'
import { ExtractJwt, Strategy } from 'passport-jwt'
import { UserDBService } from '../../db/services/user.service'
import { RefreshJWTPayload } from '../interfaces/refresh-jwt-payload.interface'
 
/**
 * @description Strategy for validating long living refresh tokens
 */
@Injectable()
export class RefreshTokenStrategy extends PassportStrategy(
  Strategy,
  'refresh-jwt',
) {
  constructor(
    private readonly configService: ConfigService,
    private readonly userService: UserDBService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: configService.get<string>('JWT_REFRESH_SECRET'),
    })
  }
 
  async validate(payload: RefreshJWTPayload) {
    const user = await this.userService.findOneById(payload.id)
    Iif (!user) {
      throw new UnauthorizedException()
    }
    return user
  }
}