import { Inject , Injectable } from '@nestjs/common' ; import { ConfigType } from '@nestjs/config' ; import { JwtService } from '@nestjs/jwt' ; import { Response } from 'express' ; import ms from 'ms' ; import { jwtConfig } from '../../../config/jwt.config' ; import { JwtPayload } from './jwt-access.strategy' ; import { JwtRefreshPayload } from './jwt-refresh.strategy' ; @ Injectable ( ) export class JwtAuthService { constructor ( private readonly jwtService , @ Inject ( jwtConfig . KEY ) private readonly jwtConfigService ) { } verifyRefreshToken ( refreshToken ) { return this . jwtService . verify < JwtRefreshPayload > ( refreshToken , { secret : this . jwtConfigService . refreshTokenSecret , } ) ; } createToken ( id ) { const payload = { uid : id } ; return { accessToken : this . jwtService . sign ( payload , { secret : this . jwtConfigService . accessTokenSecret , expiresIn : this . jwtConfigService . accessTokenExpiresIn , } ) , refreshToken : this . jwtService . sign ( payload , { secret : this . jwtConfigService . refreshTokenSecret , expiresIn : this . jwtConfigService . refreshTokenExpiresIn , } ) , } ; } setAccessToken ( res , accessToken ) { res . cookie ( 'access_token' , accessToken , { httpOnly : true , maxAge : ms ( this . jwtConfigService . refreshTokenExpiresIn ) , domain : process . env . NODE_ENV === 'production' ? '.develofolio.com' : 'localhost' , } ) ; } setRefreshToken ( res , refreshToken ) { res . cookie ( 'refresh_token' , refreshToken , { httpOnly : true , maxAge : ms ( this . jwtConfigService . refreshTokenExpiresIn ) , path : '/jwt/refresh' , domain : process . env . NODE_ENV === 'production' ? '.develofolio.com' : 'localhost' , } ) ; } removeToken ( res ) { res . cookie ( 'access_token' , '' , { httpOnly : true , maxAge : 0 , domain : process . env . NODE_ENV === 'production' ? '.develofolio.com' : 'localhost' , } ) ; res . cookie ( 'refresh_token' , '' , { httpOnly : true , path : '/jwt/refresh' , maxAge : 0 , domain : process . env . NODE_ENV === 'production' ? '.develofolio.com' : 'localhost' , } ) ; } }