import { Injectable , UnauthorizedException } from '@nestjs/common' ; import User from '../../authorization/entity/user.entity' ; import UserService from '../../authorization/service/user.service' ; import { TokenResponse } from '../../schema/graphql.schema' ; import { AuthenticationHelper } from '../authentication.helper' ; @ Injectable ( ) export class TokenService { constructor ( private userService , private authenticationHelper ) { } async refresh ( refreshToken ) { const response = this . authenticationHelper . validateAuthToken ( refreshToken ) ; const userRecord = await this . userService . getUserById ( response . sub ) ; if ( userRecord . refreshToken !== refreshToken ) { throw new UnauthorizedException ( ) ; } const token = this . authenticationHelper . generateTokenForUser ( userRecord ) ; token . refreshToken = refreshToken ; return token ; } async resetToken ( id ) { await this . userService . updateField ( id , 'refreshToken' , '' ) ; } async getNewToken ( userRecord ) { const token = this . authenticationHelper . generateTokenForUser ( userRecord ) ; await this . userService . updateField ( userRecord . id , 'refreshToken' , token . refreshToken ) ; return token ; } }