import { HttpException , Injectable } from '@nestjs/common' ; import { Client , ClientProxy , Transport } from '@nestjs/microservices' ; import * as jwt from 'jsonwebtoken' ; import { AuthConstants } from './auth.constants' ; import { CreateAuthUserDto , VerifyAuthUserByEmailDto } from './auth.dto' ; import { IAccessToken , IAuthUser , TokenTypeEnum } from './auth.interfaces' ; @ Injectable ( ) export class AuthService { @ Client ( { options : { host : 'auth' , port : 3000 } , transport : Transport . TCP , } ) private client : ClientProxy ; public async getAuthUser ( id ) { return this . client . send < IAuthUser > ( { cmd : 'getAuthUser' } , id ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public async createAuthUser ( createAuthUserDto ) { return this . client . send < IAuthUser > ( { cmd : 'createAuthUser' } , createAuthUserDto ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public async verifyAuthUserByEmail ( user ) { return this . client . send < IAuthUser > ( { cmd : 'verifyAuthUserByEmail' } , user ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public validateAccessToken ( token ) { return jwt . verify ( token , process . env . JWT_SECRET , { issuer : AuthConstants . access_token . options . issuer , } ) as IAccessToken ; } public createAccessTokenFromAuthUser ( user ) { const payload = { email : user . email , id : user . id , roles : [ user . role ] , type : TokenTypeEnum . CLIENT , } ; return jwt . sign ( payload , process . env . JWT_SECRET , AuthConstants . access_token . options ) ; } }