2018-07-13 15:13:10 +02:00
|
|
|
import {
|
|
|
|
Input,
|
|
|
|
OnInit,
|
|
|
|
} from '@angular/core';
|
2018-04-04 15:19:21 +02:00
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
|
|
|
import { AuthResult } from '../../models/domain/authResult';
|
|
|
|
|
|
|
|
import { AuthService } from '../../abstractions/auth.service';
|
|
|
|
import { I18nService } from '../../abstractions/i18n.service';
|
2018-10-03 05:09:19 +02:00
|
|
|
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
|
2018-07-13 15:13:10 +02:00
|
|
|
import { StorageService } from '../../abstractions/storage.service';
|
2018-04-04 15:19:21 +02:00
|
|
|
|
2018-07-13 15:31:14 +02:00
|
|
|
import { Utils } from '../../misc/utils';
|
2018-07-13 15:28:45 +02:00
|
|
|
|
2018-07-13 15:13:10 +02:00
|
|
|
const Keys = {
|
|
|
|
rememberedEmail: 'rememberedEmail',
|
|
|
|
rememberEmail: 'rememberEmail',
|
|
|
|
};
|
|
|
|
|
|
|
|
export class LoginComponent implements OnInit {
|
2018-04-04 15:19:21 +02:00
|
|
|
@Input() email: string = '';
|
2018-07-13 15:13:10 +02:00
|
|
|
@Input() rememberEmail = true;
|
2018-04-04 15:19:21 +02:00
|
|
|
|
|
|
|
masterPassword: string = '';
|
|
|
|
showPassword: boolean = false;
|
|
|
|
formPromise: Promise<AuthResult>;
|
2018-07-13 16:44:47 +02:00
|
|
|
onSuccessfulLogin: () => Promise<any>;
|
|
|
|
onSuccessfulLoginNavigate: () => Promise<any>;
|
2018-04-04 15:19:21 +02:00
|
|
|
|
|
|
|
protected twoFactorRoute = '2fa';
|
|
|
|
protected successRoute = 'vault';
|
|
|
|
|
|
|
|
constructor(protected authService: AuthService, protected router: Router,
|
2018-10-03 06:03:49 +02:00
|
|
|
protected platformUtilsService: PlatformUtilsService, protected i18nService: I18nService,
|
|
|
|
private storageService: StorageService) { }
|
2018-07-13 15:13:10 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
if (this.email == null || this.email === '') {
|
|
|
|
this.email = await this.storageService.get<string>(Keys.rememberedEmail);
|
|
|
|
if (this.email == null) {
|
|
|
|
this.email = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.rememberEmail = await this.storageService.get<boolean>(Keys.rememberEmail);
|
|
|
|
if (this.rememberEmail == null) {
|
|
|
|
this.rememberEmail = true;
|
|
|
|
}
|
2018-07-13 15:28:45 +02:00
|
|
|
if (Utils.isBrowser) {
|
|
|
|
document.getElementById(this.email == null || this.email === '' ? 'email' : 'masterPassword').focus();
|
|
|
|
}
|
2018-07-13 15:13:10 +02:00
|
|
|
}
|
2018-04-04 15:19:21 +02:00
|
|
|
|
|
|
|
async submit() {
|
|
|
|
if (this.email == null || this.email === '') {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
2018-04-04 15:19:21 +02:00
|
|
|
this.i18nService.t('emailRequired'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.email.indexOf('@') === -1) {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
2018-04-04 15:19:21 +02:00
|
|
|
this.i18nService.t('invalidEmail'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.masterPassword == null || this.masterPassword === '') {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
2018-04-04 15:19:21 +02:00
|
|
|
this.i18nService.t('masterPassRequired'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.formPromise = this.authService.logIn(this.email, this.masterPassword);
|
|
|
|
const response = await this.formPromise;
|
2018-07-13 15:13:10 +02:00
|
|
|
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);
|
|
|
|
}
|
2018-04-04 15:19:21 +02:00
|
|
|
if (response.twoFactor) {
|
2018-10-03 06:03:49 +02:00
|
|
|
this.platformUtilsService.eventTrack('Logged In To Two-step');
|
2018-04-04 15:19:21 +02:00
|
|
|
this.router.navigate([this.twoFactorRoute]);
|
|
|
|
} else {
|
2018-07-13 16:44:47 +02:00
|
|
|
if (this.onSuccessfulLogin != null) {
|
|
|
|
this.onSuccessfulLogin();
|
2018-04-25 18:08:18 +02:00
|
|
|
}
|
2018-10-03 06:03:49 +02:00
|
|
|
this.platformUtilsService.eventTrack('Logged In');
|
2018-07-13 16:44:47 +02:00
|
|
|
if (this.onSuccessfulLoginNavigate != null) {
|
|
|
|
this.onSuccessfulLoginNavigate();
|
|
|
|
} else {
|
|
|
|
this.router.navigate([this.successRoute]);
|
|
|
|
}
|
2018-04-04 15:19:21 +02:00
|
|
|
}
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
|
|
|
|
togglePassword() {
|
2018-10-03 06:03:49 +02:00
|
|
|
this.platformUtilsService.eventTrack('Toggled Master Password on Login');
|
2018-04-04 15:19:21 +02:00
|
|
|
this.showPassword = !this.showPassword;
|
|
|
|
document.getElementById('masterPassword').focus();
|
|
|
|
}
|
|
|
|
}
|