import { OnModuleInit, Inject } from '@nestjs/common'; import { ClientGrpc } from '@nestjs/microservices'; import { DucServicesNames, AuthServiceInterface, AuthSignInRequest, AuthRefreshAccessTokenRequest, AuthConfirmationRequest, AuthResendConfirmationCodeRequest, AuthEnableTotpMfaRequest, AuthDisableTotpMfaRequest, AuthDismissTotpMfaRequest, AuthVerifyTotpMfaRequest, } from '@victorradael/protospack'; 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 }: AuthSignInRequest): Promise { console.log('AuthClientService', 'SignIn'); return new Promise((resolve, reject) => { this.authService.signIn({ username, password, totp }).subscribe({ next: resolve, //error: (err) => reject(err.details), error: (err) => { console.log(err); reject(err.details); }, complete() { console.log('done'); }, }); }); } async enableTotpMFA({ accessToken, password, }: AuthEnableTotpMfaRequest): Promise { console.log('AuthClientService', 'enableTotpMFA'); return new Promise((resolve, reject) => { this.authService.enableTotpMfa({ accessToken, password }).subscribe({ next: resolve, error: (err) => reject(err.details), complete() { console.log('done'); }, }); }); } async disableTotpMFA({ accessToken, password, }: AuthDisableTotpMfaRequest): Promise { console.log('AuthClientService', 'disableTotpMFA'); return new Promise((resolve, reject) => { this.authService.disableTotpMfa({ accessToken, password }).subscribe({ next: resolve, error: (err) => reject(err.details), complete() { console.log('done'); }, }); }); } async dismissTotpMFA({ accessToken, }: AuthDismissTotpMfaRequest): Promise { console.log('AuthClientService', 'dismissTotpMFA'); return new Promise((resolve, reject) => { this.authService.dismissTotpMfa({ accessToken }).subscribe({ next: resolve, error: (err) => reject(err.details), complete() { console.log('done'); }, }); }); } async verifyTotp({ accessToken, totp, }: AuthVerifyTotpMfaRequest): Promise { console.log('AuthClientService', 'disableTotpMFA'); return new Promise((resolve, reject) => { this.authService.verifyTotp({ accessToken, totp }).subscribe({ next: resolve, error: (err) => reject(err.details), complete() { console.log('done'); }, }); }); } 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', 'resendConfirmationCode'); const authResendConfirmationCodeRequestReturn = await new Promise( (resolve, reject) => { this.authService.resendConfirmationCode({ 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, }: AuthRefreshAccessTokenRequest): Promise { console.log('AuthClientService', 'RefreshAccessToken'); return new Promise((resolve, reject) => { this.authService .refreshAccessToken({ username, refreshToken }) .subscribe({ next: resolve, error: (err) => reject(err.details), complete() { console.log('done'); }, }); }); } }