2021-12-16 13:36:21 +01:00
|
|
|
import { animate, style, transition, trigger } from "@angular/animations";
|
|
|
|
import { Component, OnInit } from "@angular/core";
|
|
|
|
import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR } from "@angular/forms";
|
2021-11-09 17:01:22 +01:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
import { KeyConnectorService } from "jslib-common/abstractions/keyConnector.service";
|
|
|
|
import { UserVerificationService } from "jslib-common/abstractions/userVerification.service";
|
|
|
|
import { VerificationType } from "jslib-common/enums/verificationType";
|
|
|
|
import { Verification } from "jslib-common/types/verification";
|
2021-11-09 17:01:22 +01:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
@Component({
|
|
|
|
selector: "app-verify-master-password",
|
|
|
|
templateUrl: "verify-master-password.component.html",
|
|
|
|
providers: [
|
|
|
|
{
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
multi: true,
|
|
|
|
useExisting: VerifyMasterPasswordComponent,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
animations: [
|
|
|
|
trigger("sent", [
|
|
|
|
transition(":enter", [style({ opacity: 0 }), animate("100ms", style({ opacity: 1 }))]),
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
export class VerifyMasterPasswordComponent implements ControlValueAccessor, OnInit {
|
2022-02-22 15:39:11 +01:00
|
|
|
usesKeyConnector = false;
|
|
|
|
disableRequestOTP = false;
|
|
|
|
sentCode = false;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
|
|
|
secret = new FormControl("");
|
|
|
|
|
|
|
|
private onChange: (value: Verification) => void;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private keyConnectorService: KeyConnectorService,
|
|
|
|
private userVerificationService: UserVerificationService
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
this.usesKeyConnector = await this.keyConnectorService.getUsesKeyConnector();
|
|
|
|
this.processChanges(this.secret.value);
|
|
|
|
|
|
|
|
this.secret.valueChanges.subscribe((secret) => this.processChanges(secret));
|
|
|
|
}
|
|
|
|
|
|
|
|
async requestOTP() {
|
|
|
|
if (this.usesKeyConnector) {
|
|
|
|
this.disableRequestOTP = true;
|
|
|
|
try {
|
|
|
|
await this.userVerificationService.requestOTP();
|
|
|
|
this.sentCode = true;
|
|
|
|
} finally {
|
|
|
|
this.disableRequestOTP = false;
|
|
|
|
}
|
2021-11-09 17:01:22 +01:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
writeValue(obj: any): void {
|
|
|
|
this.secret.setValue(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
this.onChange = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
// Not implemented
|
|
|
|
}
|
|
|
|
|
|
|
|
setDisabledState?(isDisabled: boolean): void {
|
|
|
|
this.disableRequestOTP = isDisabled;
|
|
|
|
if (isDisabled) {
|
|
|
|
this.secret.disable();
|
|
|
|
} else {
|
|
|
|
this.secret.enable();
|
2021-11-09 17:01:22 +01:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2021-11-09 17:01:22 +01:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
private processChanges(secret: string) {
|
|
|
|
if (this.onChange == null) {
|
|
|
|
return;
|
2021-11-09 17:01:22 +01:00
|
|
|
}
|
2021-11-16 10:43:37 +01:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
this.onChange({
|
|
|
|
type: this.usesKeyConnector ? VerificationType.OTP : VerificationType.MasterPassword,
|
|
|
|
secret: secret,
|
|
|
|
});
|
|
|
|
}
|
2021-11-09 17:01:22 +01:00
|
|
|
}
|