2018-06-21 17:47:23 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
import { Angulartics2 } from 'angulartics2';
|
|
|
|
|
|
|
|
import { ApiService } from 'jslib/abstractions/api.service';
|
2018-06-21 20:28:00 +02:00
|
|
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
2018-06-21 17:47:23 +02:00
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
2018-06-21 20:28:00 +02:00
|
|
|
import { MessagingService } from 'jslib/abstractions/messaging.service';
|
|
|
|
|
|
|
|
import { EmailRequest } from 'jslib/models/request/emailRequest';
|
|
|
|
import { EmailTokenRequest } from 'jslib/models/request/emailTokenRequest';
|
2018-06-21 17:47:23 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-change-email',
|
|
|
|
templateUrl: 'change-email.component.html',
|
|
|
|
})
|
|
|
|
export class ChangeEmailComponent {
|
2018-06-21 20:28:00 +02:00
|
|
|
masterPassword: string;
|
|
|
|
newEmail: string;
|
|
|
|
token: string;
|
|
|
|
tokenSent = false;
|
|
|
|
|
2018-06-21 17:47:23 +02:00
|
|
|
formPromise: Promise<any>;
|
|
|
|
|
|
|
|
constructor(private apiService: ApiService, private i18nService: I18nService,
|
2018-06-21 20:28:00 +02:00
|
|
|
private analytics: Angulartics2, private toasterService: ToasterService,
|
|
|
|
private cryptoService: CryptoService, private messagingService: MessagingService) { }
|
2018-06-21 17:47:23 +02:00
|
|
|
|
|
|
|
async submit() {
|
2018-06-21 20:28:00 +02:00
|
|
|
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 { }
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 17:47:23 +02:00
|
|
|
|
2018-06-21 20:28:00 +02:00
|
|
|
reset() {
|
|
|
|
this.token = this.newEmail = this.masterPassword = null;
|
|
|
|
this.tokenSent = false;
|
2018-06-21 17:47:23 +02:00
|
|
|
}
|
|
|
|
}
|