From 68b8ad7e28d5a28dfe22a13836c9b3cea9ee8a1f Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 21 Jun 2018 14:28:00 -0400 Subject: [PATCH] change email components implemented --- src/app/settings/change-email.component.html | 40 ++++++++++++++- src/app/settings/change-email.component.ts | 53 +++++++++++++++++++- src/locales/en/messages.json | 24 +++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) diff --git a/src/app/settings/change-email.component.html b/src/app/settings/change-email.component.html index 520f25600b..05829f6b7e 100644 --- a/src/app/settings/change-email.component.html +++ b/src/app/settings/change-email.component.html @@ -1 +1,39 @@ -change your email +
+
+
+
+ + +
+
+ + +
+
+
+ +
+

{{'changeEmailDesc' | i18n : newEmail}}

+ +
+
+
+ + +
+
+
+
+ + +
diff --git a/src/app/settings/change-email.component.ts b/src/app/settings/change-email.component.ts index 2432dd6651..21f2fc460b 100644 --- a/src/app/settings/change-email.component.ts +++ b/src/app/settings/change-email.component.ts @@ -6,19 +6,70 @@ import { ToasterService } from 'angular2-toaster'; import { Angulartics2 } from 'angulartics2'; import { ApiService } from 'jslib/abstractions/api.service'; +import { CryptoService } from 'jslib/abstractions/crypto.service'; import { I18nService } from 'jslib/abstractions/i18n.service'; +import { MessagingService } from 'jslib/abstractions/messaging.service'; + +import { EmailRequest } from 'jslib/models/request/emailRequest'; +import { EmailTokenRequest } from 'jslib/models/request/emailTokenRequest'; @Component({ selector: 'app-change-email', templateUrl: 'change-email.component.html', }) export class ChangeEmailComponent { + masterPassword: string; + newEmail: string; + token: string; + tokenSent = false; + formPromise: Promise; constructor(private apiService: ApiService, private i18nService: I18nService, - private analytics: Angulartics2, private toasterService: ToasterService) { } + private analytics: Angulartics2, private toasterService: ToasterService, + private cryptoService: CryptoService, private messagingService: MessagingService) { } async submit() { + const hasKey = await this.cryptoService.hasKey(); + if (!hasKey) { + this.toasterService.popAsync('error', null, this.i18nService.t('updateKey')); + return; + } + this.newEmail = this.newEmail.toLowerCase(); + if (!this.tokenSent) { + const request = new EmailTokenRequest(); + request.newEmail = this.newEmail; + request.masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, null); + try { + this.formPromise = this.apiService.postEmailToken(request); + await this.formPromise; + this.tokenSent = true; + } catch { } + } else { + const request = new EmailRequest(); + request.token = this.token; + request.newEmail = this.newEmail; + request.masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, null); + const newKey = await this.cryptoService.makeKey(this.masterPassword, this.newEmail); + request.newMasterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, newKey); + const encKey = await this.cryptoService.getEncKey(); + const newEncKey = await this.cryptoService.encrypt(encKey.key, newKey); + request.key = newEncKey.encryptedString; + try { + this.formPromise = this.apiService.postEmail(request); + await this.formPromise; + this.reset(); + this.analytics.eventTrack.next({ action: 'Changed Email' }); + this.toasterService.popAsync('success', this.i18nService.t('emailChanged'), + this.i18nService.t('logBackIn')); + this.messagingService.send('logout'); + } catch { } + } + } + + reset() { + this.token = this.newEmail = this.masterPassword = null; + this.tokenSent = false; } } diff --git a/src/locales/en/messages.json b/src/locales/en/messages.json index 6db337eaca..5f929d3577 100644 --- a/src/locales/en/messages.json +++ b/src/locales/en/messages.json @@ -766,5 +766,29 @@ }, "accountUpdated": { "message": "Account Updated" + }, + "newEmail": { + "message": "New Email" + }, + "code": { + "message": "Code" + }, + "changeEmailDesc": { + "message": "We have emailed a verification code to $EMAIL$. Please check your email for this code and enter it below to finalize your the email address change.", + "placeholders": { + "email": { + "content": "$1", + "example": "john.smith@example.com" + } + } + }, + "loggedOutWarning": { + "message": "Proceeding will log you out of your current session, requiring you to log back in. Active sessions on other devices may continue to remain active for up to one hour." + }, + "emailChanged": { + "message": "Email Changed" + }, + "logBackIn": { + "message": "Please log back in." } }