2018-06-28 05:55:50 +02:00
|
|
|
import { Component } from '@angular/core';
|
2018-06-27 22:56:11 +02:00
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
import { Angulartics2 } from 'angulartics2';
|
|
|
|
|
|
|
|
import { ApiService } from 'jslib/abstractions/api.service';
|
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
|
|
|
import { UserService } from 'jslib/abstractions/user.service';
|
|
|
|
|
2018-06-28 04:09:50 +02:00
|
|
|
import { TwoFactorEmailRequest } from 'jslib/models/request/twoFactorEmailRequest';
|
2018-06-27 22:56:11 +02:00
|
|
|
|
|
|
|
import { TwoFactorProviderType } from 'jslib/enums/twoFactorProviderType';
|
|
|
|
import { UpdateTwoFactorEmailRequest } from 'jslib/models/request/updateTwoFactorEmailRequest';
|
|
|
|
import { TwoFactorEmailResponse } from 'jslib/models/response/twoFactorEmailResponse';
|
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
import { TwoFactorBaseComponent } from './two-factor-base.component';
|
|
|
|
|
2018-06-27 22:56:11 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-two-factor-email',
|
|
|
|
templateUrl: 'two-factor-email.component.html',
|
|
|
|
})
|
2018-06-28 05:55:50 +02:00
|
|
|
export class TwoFactorEmailComponent extends TwoFactorBaseComponent {
|
2018-07-18 23:10:26 +02:00
|
|
|
type = TwoFactorProviderType.Email;
|
2018-06-27 22:56:11 +02:00
|
|
|
email: string;
|
|
|
|
token: string;
|
|
|
|
sentEmail: string;
|
|
|
|
formPromise: Promise<any>;
|
|
|
|
emailPromise: Promise<any>;
|
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
constructor(apiService: ApiService, i18nService: I18nService,
|
|
|
|
analytics: Angulartics2, toasterService: ToasterService,
|
|
|
|
platformUtilsService: PlatformUtilsService, private userService: UserService) {
|
2018-07-18 23:10:26 +02:00
|
|
|
super(apiService, i18nService, analytics, toasterService, platformUtilsService);
|
2018-06-28 05:55:50 +02:00
|
|
|
}
|
2018-06-27 22:56:11 +02:00
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
auth(authResponse: any) {
|
|
|
|
super.auth(authResponse);
|
|
|
|
return this.processResponse(authResponse.response);
|
2018-06-27 22:56:11 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
submit() {
|
2018-06-27 22:56:11 +02:00
|
|
|
if (this.enabled) {
|
2018-06-28 05:55:50 +02:00
|
|
|
return super.disable(this.formPromise);
|
2018-06-27 22:56:11 +02:00
|
|
|
} else {
|
2018-06-28 05:55:50 +02:00
|
|
|
return this.enable();
|
2018-06-27 22:56:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendEmail() {
|
|
|
|
try {
|
|
|
|
const request = new TwoFactorEmailRequest(this.email, this.masterPasswordHash);
|
|
|
|
this.emailPromise = this.apiService.postTwoFactorEmailSetup(request);
|
|
|
|
await this.emailPromise;
|
|
|
|
this.sentEmail = this.email;
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
protected enable() {
|
2018-06-27 22:56:11 +02:00
|
|
|
const request = new UpdateTwoFactorEmailRequest();
|
|
|
|
request.masterPasswordHash = this.masterPasswordHash;
|
|
|
|
request.email = this.email;
|
|
|
|
request.token = this.token;
|
2018-06-28 05:55:50 +02:00
|
|
|
|
|
|
|
return super.enable(async () => {
|
2018-06-27 22:56:11 +02:00
|
|
|
this.formPromise = this.apiService.putTwoFactorEmail(request);
|
|
|
|
const response = await this.formPromise;
|
|
|
|
await this.processResponse(response);
|
2018-06-28 05:55:50 +02:00
|
|
|
});
|
2018-06-27 22:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async processResponse(response: TwoFactorEmailResponse) {
|
|
|
|
this.token = null;
|
|
|
|
this.email = response.email;
|
|
|
|
this.enabled = response.enabled;
|
|
|
|
if (!this.enabled && (this.email == null || this.email === '')) {
|
|
|
|
this.email = await this.userService.getEmail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|