import { HttpErrorResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { VerificationCodeResources } from '@core/resources/verification-code.resources'; import { UserTypes } from '@core/typings/client-user.typing'; import { ClientUserService } from '@features/client-user/client-user.service'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { NotifierService } from '@yourcause/common/notifier'; import { ApplicantService } from '../auth-user/applicant.service'; import { DeepLinkingService } from '../deep-linking.service'; import { PortalDeterminationService } from '../portal-determination.service'; import { AuthBehaviors } from './token-behaviors'; import { TokenService } from './token.service'; @Injectable({ providedIn: 'root' }) export class VerificationCodeService { constructor ( private logger: LogService, private verificationCodeResources: VerificationCodeResources, private notifier: NotifierService, private i18n: I18nService, private tokenService: TokenService, private portal: PortalDeterminationService, private deepLinkingService: DeepLinkingService, private behavior: AuthBehaviors, private clientUserService: ClientUserService, private applicantService: ApplicantService ) { } async loginWithVerificationCode ( email: string, password: string, code: string, rememberMe: boolean ): Promise<{ codeInvalid: boolean; codeExpired: boolean; }> { let codeInvalid = false; let codeExpired = false; try { await this.tokenService.login( email, password, code, rememberMe ); if (this.portal.isManager) { await this.clientUserService.getUser(true, true); } else { await this.applicantService.getApplicant(true); } this.deepLinkingService.attemptDeepLink(this.behavior.current.postLoginRedirect); } catch (err) { const e = err as HttpErrorResponse; this.logger.error(e); switch (e.error?.message) { case 'Security_Code_Invalid': codeInvalid = true; break; case 'Security_Code_Expired': codeExpired = true; break; default: this.notifier.error(this.i18n.translate( 'AUTH.errorLoggingIn', {}, 'There was an error logging in' )); break; } } return { codeExpired, codeInvalid }; } async expireAllSessions ( email: string, password: string, userType: UserTypes ) { try { await this.verificationCodeResources.expireAllSessions( email, password, userType ); return true; } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'common:textErrorEndingAllSessions', {}, 'There was an error ending all sessions' )); return false; } } async resendVerificationCode (email: string, userType: UserTypes) { try { await this.verificationCodeResources.resendVerificationCode( email, userType ); this.notifier.success(this.i18n.translate( 'common:notificationSuccessResendSecurityCode', {}, 'Successfully resent security code' )); } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'common:notificationErrorResendSecurityCode', {}, 'There was an error resending the security code' )); } } }