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 | import { Injectable, UnauthorizedException } from '@nestjs/common'; import { AUTH_ERRORS } from 'src/exception/exception.const'; import { UserSessionRepository } from 'src/modules/common/database/mongo/auth/repositories/user-session.repository'; import { decodeAccessToken } from '../../shared/auth.util'; @Injectable() export class ValidateService { constructor(private readonly userSessionRepository: UserSessionRepository) {} async validateJwtToken(jwtToken: string) { Iif (!jwtToken) throw new UnauthorizedException(AUTH_ERRORS.ACCESS_TOKEN_NOT_FOUND); const { payload, metadata } = decodeAccessToken(jwtToken); Iif (!payload || !metadata) throw new UnauthorizedException(AUTH_ERRORS.INVALID_ACCESS_TOKEN); /** check token TTL */ Iif (!metadata.exp) throw new UnauthorizedException(AUTH_ERRORS.INVALID_ACCESS_TOKEN); const TTL = Math.round(new Date().getTime() / 1000); Iif (metadata.exp < TTL) throw new UnauthorizedException(AUTH_ERRORS.ACCESS_TOKEN_EXPIRED); /** check valid of payload */ const { userId } = payload; Iif (!userId || (userId && !(await this.userSessionRepository.checkExistedByUserId(userId)))) throw new UnauthorizedException(AUTH_ERRORS.USER_NOT_EXISTED); return { payload }; } } |