1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-29 07:59:42 +01:00
bitwarden-browser/src/app/accounts/login.component.ts

79 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-01-16 21:58:17 +01:00
import * as template from './login.component.html';
import {
Component,
2018-02-02 18:31:21 +01:00
ComponentFactoryResolver,
ViewChild,
ViewContainerRef,
2018-01-16 21:58:17 +01:00
} from '@angular/core';
import { Router } from '@angular/router';
2018-01-30 23:52:56 +01:00
import { ToasterService } from 'angular2-toaster';
2018-02-08 16:37:54 +01:00
import { Angulartics2 } from 'angulartics2';
2018-01-30 23:52:56 +01:00
2018-02-02 18:31:21 +01:00
import { ModalComponent } from '../modal.component';
2018-02-08 16:37:54 +01:00
import { EnvironmentComponent } from './environment.component';
2018-02-02 18:31:21 +01:00
2018-02-02 04:59:04 +01:00
import { AuthResult } from 'jslib/models/domain/authResult';
2018-01-23 05:37:36 +01:00
import { AuthService } from 'jslib/abstractions/auth.service';
2018-01-30 23:52:56 +01:00
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-01-23 05:37:36 +01:00
2018-01-16 21:58:17 +01:00
@Component({
selector: 'app-login',
2018-01-24 15:26:59 +01:00
template: template,
2018-01-16 21:58:17 +01:00
})
2018-01-30 23:52:56 +01:00
export class LoginComponent {
2018-02-02 18:31:21 +01:00
@ViewChild('environment', { read: ViewContainerRef }) environmentModal: ViewContainerRef;
email: string = '';
masterPassword: string = '';
2018-02-02 04:59:04 +01:00
formPromise: Promise<AuthResult>;
2018-01-30 23:52:56 +01:00
constructor(private authService: AuthService, private router: Router, private analytics: Angulartics2,
2018-02-02 18:31:21 +01:00
private toasterService: ToasterService, private i18nService: I18nService,
private componentFactoryResolver: ComponentFactoryResolver) { }
2018-01-31 20:19:21 +01:00
async submit() {
2018-01-30 23:52:56 +01:00
if (this.email == null || this.email === '') {
2018-01-31 05:34:45 +01:00
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
2018-01-30 23:52:56 +01:00
this.i18nService.t('emailRequired'));
return;
}
if (this.email.indexOf('@') === -1) {
2018-01-31 05:34:45 +01:00
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
2018-01-30 23:52:56 +01:00
this.i18nService.t('invalidEmail'));
return;
}
if (this.masterPassword == null || this.masterPassword === '') {
2018-01-31 05:34:45 +01:00
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
2018-01-30 23:52:56 +01:00
this.i18nService.t('masterPassRequired'));
return;
}
2018-01-31 05:34:45 +01:00
try {
2018-01-31 06:10:14 +01:00
this.formPromise = this.authService.logIn(this.email, this.masterPassword);
const response = await this.formPromise;
2018-01-31 05:34:45 +01:00
if (response.twoFactor) {
this.analytics.eventTrack.next({ action: 'Logged In To Two-step' });
2018-02-01 04:54:13 +01:00
this.router.navigate(['2fa']);
2018-01-31 05:34:45 +01:00
} else {
this.analytics.eventTrack.next({ action: 'Logged In' });
this.router.navigate(['vault']);
// TODO: sync on load to vault?
}
2018-01-31 06:10:14 +01:00
} catch { }
}
2018-02-02 18:31:21 +01:00
settings() {
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
const modal = this.environmentModal.createComponent(factory).instance;
const childComponent = modal.show<EnvironmentComponent>(EnvironmentComponent,
this.environmentModal);
childComponent.onSaved.subscribe(() => {
modal.close();
});
}
2018-01-16 21:58:17 +01:00
}