import { OnModuleInit, Inject } from '@nestjs/common'; import { ClientGrpc } from '@nestjs/microservices'; import { AuthConfirmationRequest, AuthDisableTotpMfaRequest, AuthEnableTotpMfaRequest, AuthResendConfirmationCodeRequest, AuthServiceInterface, AuthVerifyTotpMfaRequest, DucServicesNames, } from '@victorradael/protospack'; import { ISignIn, IRefreshAccessToken } from './interfaces'; export class AuthClientService implements OnModuleInit { private authService: AuthServiceInterface; constructor( @Inject('AUTH_PACKAGE') private readonly grpcClient: ClientGrpc, ) {} onModuleInit() { this.authService = this.grpcClient.getService( DucServicesNames.AuthProtoService, ); } async signIn({ username, password, totp }: ISignIn): Promise { console.log('AuthClientService', 'SignIn'); const tokens = await new Promise((resolve, reject) => { this.authService.signIn({ username, password, totp }).subscribe({ next(x) { resolve(x); }, error(err) { console.log('Observable Error'); reject(err); }, complete() { console.log('done'); }, }); }) .then((res) => res) .catch((err) => { throw new Error(err); }); return tokens; } async enableTotpMFA({ username, password, }: AuthEnableTotpMfaRequest): Promise { console.log('AuthClientService', 'enableTotpMFA'); const tokens = await new Promise((resolve, reject) => { this.authService.enableTotpMfa({ username, password }).subscribe({ next(x) { resolve(x); }, error(err) { console.log('Observable Error'); reject(err); }, complete() { console.log('done'); }, }); }) .then((res) => res) .catch((err) => { throw new Error(err); }); return tokens; } async disableTotpMFA({ username, password, totp, }: AuthDisableTotpMfaRequest): Promise { console.log('AuthClientService', 'disableTotpMFA'); const tokens = await new Promise((resolve, reject) => { this.authService.disableTotpMfa({ username, password, totp }).subscribe({ next(x) { resolve(x); }, error(err) { console.log('Observable Error'); reject(err); }, complete() { console.log('done'); }, }); }) .then((res) => res) .catch((err) => { throw new Error(err); }); return tokens; } async verifyTotp({ username, accessToken, totp, }: AuthVerifyTotpMfaRequest): Promise { console.log('AuthClientService', 'disableTotpMFA'); const tokens = await new Promise((resolve, reject) => { this.authService.verifyTotp({ username, accessToken, totp }).subscribe({ next(x) { resolve(x); }, error(err) { console.log('Observable Error'); reject(err); }, complete() { console.log('done'); }, }); }) .then((res) => res) .catch((err) => { throw new Error(err); }); return tokens; } async confirmRegister({ username, code, }: AuthConfirmationRequest): Promise { console.log('AuthClientService', 'confirmRegister'); const tokens = await new Promise((resolve, reject) => { this.authService.confirmRegister({ username, code }).subscribe({ next(x) { resolve(x); }, error(err) { console.log('Observable Error'); reject(err); }, complete() { console.log('done'); }, }); }) .then((res) => res) .catch((err) => { throw new Error(err); }); return tokens; } async resendeConfirmationCode({ username, }: AuthResendConfirmationCodeRequest): Promise { console.log('AuthClientService', 'resendeConfirmationCode'); const authResendConfirmationCodeRequestReturn = await new Promise( (resolve, reject) => { this.authService.resendeConfirmationCode({ username }).subscribe({ next(x) { resolve(x); }, error(err) { console.log('Observable Error'); reject(err); }, complete() { console.log('done'); }, }); }, ) .then((res) => res) .catch((err) => { throw new Error(err); }); return authResendConfirmationCodeRequestReturn; } async refreshAccessToken({ username, refreshToken, }: IRefreshAccessToken): Promise { console.log('AuthClientService', 'RefreshAccessToken'); const tokens = await new Promise((resolve, reject) => { this.authService .refreshAccessToken({ username, refreshToken }) .subscribe({ next(x) { resolve(x); }, error(err) { console.log('Observable Error'); reject(err); }, complete() { console.log('done'); }, }); }) .then((res) => res) .catch((err) => { throw new Error(err); }); return tokens; } }