From 7b4d0a71de640ce14c3c446bfd2e8ce36635db3f Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 4 Apr 2018 09:19:21 -0400 Subject: [PATCH] share login component --- package-lock.json | 9 +++ package.json | 1 + src/angular/components/login.component.ts | 67 +++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/angular/components/login.component.ts diff --git a/package-lock.json b/package-lock.json index 4a8f70fe13..9cab90a199 100644 --- a/package-lock.json +++ b/package-lock.json @@ -189,6 +189,15 @@ "integrity": "sha512-/ndYYbV/7WZx6ujm6avFUqfb+FKbrx7oT+3mYj8i0o9N26Ug+BseFjy6oRnlVVedl39yRP6hhea81QgKmoYbbQ==", "dev": true }, + "angulartics2": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/angulartics2/-/angulartics2-5.0.1.tgz", + "integrity": "sha512-QYBp7km7xTf/57zKKnYreM0OQ1Pq0kd4L9HJTC79vy7+RG1XqrkA944jTGKDERLWtjEAlQuSyZMS9J5IZZ56sw==", + "dev": true, + "requires": { + "tslib": "1.9.0" + } + }, "argparse": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", diff --git a/package.json b/package.json index 8a4632c076..6ec69a7ab1 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "@types/node-forge": "0.7.1", "@types/webcrypto": "0.0.28", "angular2-toaster": "4.0.2", + "angulartics2": "5.0.1", "core-js": "2.4.1", "lunr": "2.1.6", "node-forge": "0.7.1", diff --git a/src/angular/components/login.component.ts b/src/angular/components/login.component.ts new file mode 100644 index 0000000000..951490e79f --- /dev/null +++ b/src/angular/components/login.component.ts @@ -0,0 +1,67 @@ +import { + Component, + EventEmitter, + Input, +} from '@angular/core'; +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'; +import { SyncService } from '../../abstractions/sync.service'; + +export class LoginComponent { + @Input() email: string = ''; + + masterPassword: string = ''; + showPassword: boolean = false; + formPromise: Promise; + + protected twoFactorRoute = '2fa'; + protected successRoute = 'vault'; + + constructor(protected authService: AuthService, protected router: Router, + protected analytics: Angulartics2, protected toasterService: ToasterService, + protected i18nService: I18nService, protected syncService: SyncService) { } + + 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 { + this.syncService.fullSync(true); + 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(); + } +}