import { Input, OnInit, } from '@angular/core'; import { Router } from '@angular/router'; import { Angulartics2 } from 'angulartics2'; import { AuthResult } from '../../models/domain/authResult'; import { AuthService } from '../../abstractions/auth.service'; import { I18nService } from '../../abstractions/i18n.service'; import { PlatformUtilsService } from '../../abstractions/platformUtils.service'; import { StorageService } from '../../abstractions/storage.service'; import { Utils } from '../../misc/utils'; const Keys = { rememberedEmail: 'rememberedEmail', rememberEmail: 'rememberEmail', }; export class LoginComponent implements OnInit { @Input() email: string = ''; @Input() rememberEmail = true; masterPassword: string = ''; showPassword: boolean = false; formPromise: Promise; onSuccessfulLogin: () => Promise; onSuccessfulLoginNavigate: () => Promise; protected twoFactorRoute = '2fa'; protected successRoute = 'vault'; constructor(protected authService: AuthService, protected router: Router, protected analytics: Angulartics2, protected platformUtilsService: PlatformUtilsService, protected i18nService: I18nService, private storageService: StorageService) { } async ngOnInit() { if (this.email == null || this.email === '') { this.email = await this.storageService.get(Keys.rememberedEmail); if (this.email == null) { this.email = ''; } } this.rememberEmail = await this.storageService.get(Keys.rememberEmail); if (this.rememberEmail == null) { this.rememberEmail = true; } if (Utils.isBrowser) { document.getElementById(this.email == null || this.email === '' ? 'email' : 'masterPassword').focus(); } } async submit() { if (this.email == null || this.email === '') { this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), this.i18nService.t('emailRequired')); return; } if (this.email.indexOf('@') === -1) { this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), this.i18nService.t('invalidEmail')); return; } if (this.masterPassword == null || this.masterPassword === '') { this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), this.i18nService.t('masterPassRequired')); return; } try { this.formPromise = this.authService.logIn(this.email, this.masterPassword); const response = await this.formPromise; await this.storageService.save(Keys.rememberEmail, this.rememberEmail); if (this.rememberEmail) { await this.storageService.save(Keys.rememberedEmail, this.email); } else { await this.storageService.remove(Keys.rememberedEmail); } if (response.twoFactor) { this.analytics.eventTrack.next({ action: 'Logged In To Two-step' }); this.router.navigate([this.twoFactorRoute]); } else { if (this.onSuccessfulLogin != null) { this.onSuccessfulLogin(); } this.analytics.eventTrack.next({ action: 'Logged In' }); if (this.onSuccessfulLoginNavigate != null) { this.onSuccessfulLoginNavigate(); } else { this.router.navigate([this.successRoute]); } } } catch { } } togglePassword() { this.analytics.eventTrack.next({ action: 'Toggled Master Password on Login' }); this.showPassword = !this.showPassword; document.getElementById('masterPassword').focus(); } }