2021-03-14 09:23:20 +01:00
|
|
|
import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core';
|
2020-12-28 10:11:25 +01:00
|
|
|
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
|
2021-02-14 17:02:35 +01:00
|
|
|
import {CustomErrorStateMatcher} from '@app/_helpers';
|
|
|
|
import {AuthService} from '@app/_services';
|
2021-02-17 19:22:06 +01:00
|
|
|
import {Router} from '@angular/router';
|
2020-12-28 10:11:25 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-auth',
|
|
|
|
templateUrl: './auth.component.html',
|
2021-03-14 09:23:20 +01:00
|
|
|
styleUrls: ['./auth.component.scss'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
2020-12-28 10:11:25 +01:00
|
|
|
})
|
2021-02-17 19:22:06 +01:00
|
|
|
export class AuthComponent implements OnInit {
|
2021-02-03 11:52:31 +01:00
|
|
|
keyForm: FormGroup;
|
2020-12-28 10:11:25 +01:00
|
|
|
submitted: boolean = false;
|
|
|
|
loading: boolean = false;
|
2021-02-03 11:52:31 +01:00
|
|
|
matcher = new CustomErrorStateMatcher();
|
2020-12-28 10:11:25 +01:00
|
|
|
|
|
|
|
constructor(
|
2021-02-03 11:52:31 +01:00
|
|
|
private authService: AuthService,
|
2020-12-28 10:11:25 +01:00
|
|
|
private formBuilder: FormBuilder,
|
2021-02-17 19:22:06 +01:00
|
|
|
private router: Router
|
2020-12-28 10:11:25 +01:00
|
|
|
) { }
|
|
|
|
|
2021-03-18 10:10:55 +01:00
|
|
|
async ngOnInit(): Promise<void> {
|
2021-02-03 11:52:31 +01:00
|
|
|
this.keyForm = this.formBuilder.group({
|
|
|
|
key: ['', Validators.required],
|
2020-12-28 10:11:25 +01:00
|
|
|
});
|
2021-04-29 07:29:54 +02:00
|
|
|
await this.authService.init();
|
2021-04-29 19:10:39 +02:00
|
|
|
// if (this.authService.privateKey !== undefined) {
|
|
|
|
// const setKey = await this.authService.setKey(this.authService.privateKey);
|
|
|
|
// }
|
|
|
|
// }
|
2021-02-03 11:52:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get keyFormStub(): any { return this.keyForm.controls; }
|
2020-12-28 10:11:25 +01:00
|
|
|
|
2021-03-18 10:10:55 +01:00
|
|
|
async onSubmit(): Promise<void> {
|
2020-12-28 10:11:25 +01:00
|
|
|
this.submitted = true;
|
|
|
|
|
2021-02-03 11:52:31 +01:00
|
|
|
if (this.keyForm.invalid) { return; }
|
2020-12-28 10:11:25 +01:00
|
|
|
|
|
|
|
this.loading = true;
|
2021-03-18 10:10:55 +01:00
|
|
|
await this.authService.setKey(this.keyFormStub.key.value);
|
2021-02-03 11:52:31 +01:00
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
login(): void {
|
2021-04-29 19:10:39 +02:00
|
|
|
// TODO check if we have privatekey
|
2021-04-29 07:29:54 +02:00
|
|
|
// Send us to home if we have a private key
|
|
|
|
// talk to meta somehow
|
|
|
|
// in the error interceptor if 401/403 handle it
|
|
|
|
// if 200 go /home
|
|
|
|
if (this.authService.getPrivateKey()) {
|
2021-02-17 19:22:06 +01:00
|
|
|
this.router.navigate(['/home']);
|
|
|
|
}
|
2021-02-03 11:52:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switchWindows(): void {
|
|
|
|
this.authService.sessionToken = undefined;
|
|
|
|
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';
|
|
|
|
}
|
2020-12-28 10:11:25 +01:00
|
|
|
}
|
|
|
|
}
|