1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/angular/components/login.component.ts

66 lines
2.4 KiB
TypeScript
Raw Normal View History

2018-04-04 15:47:43 +02:00
import { Input } from '@angular/core';
2018-04-04 15:19:21 +02:00
import { Router } from '@angular/router';
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import { AuthResult } from '../../models/domain/authResult';
import { AuthService } from '../../abstractions/auth.service';
import { I18nService } from '../../abstractions/i18n.service';
export class LoginComponent {
@Input() email: string = '';
masterPassword: string = '';
showPassword: boolean = false;
formPromise: Promise<AuthResult>;
2018-04-25 18:08:18 +02:00
onSuccessfullLogin: () => Promise<any>;
2018-04-04 15:19:21 +02:00
protected twoFactorRoute = '2fa';
protected successRoute = 'vault';
constructor(protected authService: AuthService, protected router: Router,
protected analytics: Angulartics2, protected toasterService: ToasterService,
2018-04-25 18:08:18 +02:00
protected i18nService: I18nService) { }
2018-04-04 15:19:21 +02:00
async submit() {
if (this.email == null || this.email === '') {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('emailRequired'));
return;
}
if (this.email.indexOf('@') === -1) {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('invalidEmail'));
return;
}
if (this.masterPassword == null || this.masterPassword === '') {
this.toasterService.popAsync('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;
if (response.twoFactor) {
this.analytics.eventTrack.next({ action: 'Logged In To Two-step' });
this.router.navigate([this.twoFactorRoute]);
} else {
2018-04-25 18:08:18 +02:00
if (this.onSuccessfullLogin != null) {
this.onSuccessfullLogin();
}
2018-04-04 15:19:21 +02:00
this.analytics.eventTrack.next({ action: 'Logged In' });
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();
}
}