import { Injectable } from '@nestjs/common' ; import { sign , verify , SignOptions } from 'jsonwebtoken' ; import { AUTH_CREDENTIALS_INVALID , UnauthenticatedException } from '@lib/exceptions' ; import { api } from '@grpc-proto/user/user.types' ; import { JWT_EXPIRE } from '@auth/env' ; interface IDecodedUserData { id : string ; email : string ; } const env = process . env ; @ Injectable ( ) export class JwtCertsService { public addToken ( user , expiresIn = + JWT_EXPIRE ) { if ( ! user ) { throw new UnauthenticatedException ( AUTH_CREDENTIALS_INVALID ) ; } const options = { algorithm : 'RS256' , } ; if ( expiresIn ) { options . expiresIn = expiresIn ; } const payload = { id : user . id , email : user . email , } ; return sign ( payload , env . JWT_PRIV , { expiresIn , algorithm : 'RS256' , } ) ; } public verifyToken ( token ) { try { return verify ( token , env . JWT_PUB , { algorithms : [ 'RS256' ] , } ) as IDecodedUserData ; } catch ( ignored ) { throw new UnauthenticatedException ( ) ; } } }