import { HttpErrorResponse } from '@angular/common/http'; import { Component } from '@angular/core'; import { Validators } from '@angular/forms'; import { AdminUserResources } from '@core/resources/admin-user.resources'; import { ApplicantService } from '@core/services/auth-user/applicant.service'; import { PortalDeterminationService } from '@core/services/portal-determination.service'; import { ClientUserService } from '@features/client-user/client-user.service'; import { EmailExtensionValidator, PanelTypes, TypeSafeFormBuilder, TypeSafeFormGroup } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { NotifierService } from '@yourcause/common/notifier'; @Component({ selector: 'gc-forgot-password', templateUrl: './forgot-password.component.html', styleUrls: ['./forgot-password.component.scss'] }) export class ForgotPasswordComponent { email: string; PanelTypes = PanelTypes; forgotPasswordForm: TypeSafeFormGroup<{ emailAddress: string }>; emailSent = false; constructor ( private logger: LogService, private portal: PortalDeterminationService, private applicantService: ApplicantService, private clientUserService: ClientUserService, private adminUserResource: AdminUserResources, public fb: TypeSafeFormBuilder, private notifier: NotifierService, private i18n: I18nService ) { this.forgotPasswordForm = fb.group({ emailAddress: ['', [ EmailExtensionValidator, Validators.required ] ] }); this.handleSubmit = this.handleSubmit.bind(this); } get isManager () { return this.portal.isManager; } get isPlatform () { return this.portal.isPlatform; } get passwordService () { if (this.isManager) { return this.clientUserService; } else if (this.isPlatform) { return this.adminUserResource; } else { return this.applicantService; } } async handleSubmit () { const email = this.forgotPasswordForm.value.emailAddress; try { await this.passwordService.requestPasswordEmail(email); this.notifier.success(this.i18n.translate( 'GLOBAL:notificationPasswordResetLinkSent', {}, 'A password reset link has been sent to ' ) + email); //Update Reset PW toastr language to align with OWASP this.emailSent = true; } catch (err) { const e = err as HttpErrorResponse; if (e.error && e.error.message === 'Email does not exist.') { this.notifier.error(this.i18n.translate( 'AUTH:textEmailDoesNotExist', {}, 'Email does not exist.' )); } else { this.logger.error(e); this.notifier.error(this.i18n.translate( 'GLOBAL:notificationErrorResettingPassword', {}, 'There was an error requesting a password reset' )); throw e; } } } }