cic-staff-client/src/app/auth/auth.component.ts

78 lines
2.1 KiB
TypeScript
Raw Normal View History

import {Component, OnInit} from '@angular/core';
2020-12-28 10:11:25 +01:00
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {CustomErrorStateMatcher} from '@app/_helpers';
import {AuthService} from '@app/_services';
import {Router} from '@angular/router';
2020-12-28 10:11:25 +01:00
@Component({
selector: 'app-auth',
templateUrl: './auth.component.html',
styleUrls: ['./auth.component.scss']
})
export class AuthComponent implements OnInit {
2021-02-03 11:52:31 +01:00
keyForm: FormGroup;
stateForm: 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,
private router: Router
2020-12-28 10:11:25 +01:00
) { }
ngOnInit(): 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
});
this.stateForm = this.formBuilder.group({
state: '',
2021-02-03 11:52:31 +01:00
});
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; }
2020-12-28 10:11:25 +01:00
onSubmit(): void {
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-02-03 11:52:31 +01:00
this.authService.setKey(this.keyFormStub.key.value).then();
this.loading = false;
}
login(): void {
const loginStatus = this.authService.login();
if (loginStatus) {
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
}
}