89 lines
2.6 KiB
TypeScript
89 lines
2.6 KiB
TypeScript
import {AfterViewInit, Component, OnInit, Renderer2} from '@angular/core';
|
|
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
|
|
import {CustomErrorStateMatcher} from '../../_helpers';
|
|
import {AuthService} from '../../_services';
|
|
|
|
@Component({
|
|
selector: 'app-add-key',
|
|
templateUrl: './add-key.component.html',
|
|
styleUrls: ['./add-key.component.scss']
|
|
})
|
|
export class AddKeyComponent implements OnInit, AfterViewInit {
|
|
keyForm: FormGroup;
|
|
passphraseForm: FormGroup;
|
|
submitted: boolean = false;
|
|
loading: boolean = false;
|
|
matcher = new CustomErrorStateMatcher();
|
|
|
|
constructor(
|
|
private authService: AuthService,
|
|
private formBuilder: FormBuilder,
|
|
private renderer: Renderer2
|
|
) { }
|
|
|
|
ngAfterViewInit(): void {
|
|
console.log(window.location);
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.responseType = 'text';
|
|
xhr.open('GET', window.location.origin + '/privatekey.asc');
|
|
xhr.onload = (e) => {
|
|
if (xhr.status !== 200) {
|
|
console.warn('failed to autoload private key ciphertext');
|
|
return;
|
|
}
|
|
(document.getElementById('privateKeyAsc') as HTMLInputElement).value = xhr.responseText;
|
|
};
|
|
xhr.send();
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
this.keyForm = this.formBuilder.group({
|
|
key: ['', Validators.required],
|
|
});
|
|
this.passphraseForm = this.formBuilder.group({
|
|
passphrase: ['', Validators.required],
|
|
});
|
|
if (this.authService.privateKey !== undefined ) {
|
|
this.authService.setKey(this.authService.privateKey).then(r => {
|
|
if (this.authService.sessionToken !== undefined) {
|
|
this.authService.setState(
|
|
'click to perform login ' + this.authService.sessionLoginCount + ' with token ' + this.authService.sessionToken);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
get keyFormStub(): any { return this.keyForm.controls; }
|
|
get passphraseFormStub(): any { return this.passphraseForm.controls; }
|
|
|
|
onSubmit(): void {
|
|
this.submitted = true;
|
|
|
|
if (this.keyForm.invalid) { return; }
|
|
|
|
this.loading = true;
|
|
this.authService.setKey(this.keyFormStub.key.value).then();
|
|
}
|
|
|
|
login(): void {
|
|
// if (this.passphraseForm.invalid) { return; }
|
|
this.authService.login();
|
|
}
|
|
|
|
switchWindows(): void {
|
|
const divOne = document.getElementById('one');
|
|
const divTwo = document.getElementById('two');
|
|
this.toggleDisplay(divOne);
|
|
this.toggleDisplay(divTwo);
|
|
}
|
|
|
|
toggleDisplay(element: any): void {
|
|
const style = window.getComputedStyle(element).display;
|
|
if (style === 'block') {
|
|
element.style.display = 'none';
|
|
} else {
|
|
element.style.display = 'block';
|
|
}
|
|
}
|
|
}
|