2018-01-16 21:58:17 +01:00
|
|
|
import * as template from './login.component.html';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
2018-01-23 22:58:32 +01:00
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
2018-01-30 23:52:56 +01:00
|
|
|
import { Angulartics2 } from 'angulartics2';
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
|
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-31 05:34:45 +01:00
|
|
|
import { ValidationService } from '../services/validation.service';
|
|
|
|
|
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-01-23 22:58:32 +01:00
|
|
|
email: string = '';
|
|
|
|
masterPassword: string = '';
|
2018-01-31 05:34:45 +01:00
|
|
|
loading: boolean;
|
2018-01-23 22:58:32 +01:00
|
|
|
|
2018-01-30 23:52:56 +01:00
|
|
|
constructor(private authService: AuthService, private router: Router, private analytics: Angulartics2,
|
2018-01-31 05:34:45 +01:00
|
|
|
private toasterService: ToasterService, private i18nService: I18nService,
|
|
|
|
private validationService: ValidationService) { }
|
2018-01-23 22:58:32 +01:00
|
|
|
|
|
|
|
async onSubmit() {
|
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
|
|
|
this.loading = true;
|
|
|
|
try {
|
|
|
|
const response = await this.authService.logIn(this.email, this.masterPassword);
|
|
|
|
this.loading = false;
|
|
|
|
if (response.twoFactor) {
|
|
|
|
this.analytics.eventTrack.next({ action: 'Logged In To Two-step' });
|
|
|
|
this.router.navigate(['twoFactor']);
|
|
|
|
// TODO: pass 2fa info
|
|
|
|
} else {
|
|
|
|
this.analytics.eventTrack.next({ action: 'Logged In' });
|
|
|
|
this.router.navigate(['vault']);
|
|
|
|
// TODO: sync on load to vault?
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.loading = false;
|
|
|
|
this.validationService.showError(e);
|
2018-01-30 23:52:56 +01:00
|
|
|
}
|
2018-01-23 22:58:32 +01:00
|
|
|
}
|
2018-01-16 21:58:17 +01:00
|
|
|
}
|