Newer
Older

mechtelm
committed
import {Component, OnDestroy, OnInit} from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
import {MainDataService} from '../../maindata.service';
import {CustomtextService} from 'iqb-components';
import {ActivatedRoute, Router} from '@angular/router';
import {Subscription} from 'rxjs';
import {AuthData} from '../../app.interfaces';
import {BackendService} from '../../backend.service';

mechtelm
committed
@Component({
templateUrl: './login.component.html',
styles: [

mechtelm
committed
'mat-card {margin: 10px;}',
'.mat-card-gray {background-color: lightgray}'

mechtelm
committed
]
})
export class LoginComponent implements OnInit, OnDestroy {
static oldLoginName = '';

mechtelm
committed
returnTo = '';

mechtelm
committed
loginForm = new FormGroup({
name: new FormControl(LoginComponent.oldLoginName, [Validators.required, Validators.minLength(3)]),
pw: new FormControl('')
});
constructor(
public mds: MainDataService,
public cts: CustomtextService,
private bs: BackendService,

mechtelm
committed
private router: Router,
private route: ActivatedRoute
) { }
ngOnInit(): void {

mechtelm
committed
this.routingSubscription = this.route.params.subscribe(params => {
this.returnTo = params['returnTo'];

mechtelm
committed
}
login() {
const loginData = this.loginForm.value;
LoginComponent.oldLoginName = loginData['name'];
this.bs.login(loginData['name'], loginData['pw']).subscribe(
this.problemText = '';
if (typeof authData === 'number') {
const errCode = authData as number;
if (errCode === 400) {

mechtelm
committed
this.problemText = 'Anmeldedaten sind nicht gültig. Bitte nocheinmal versuchen!';
} else if (errCode === 202 || errCode === 204) {
this.problemText = 'Anmeldedaten sind gültig, aber es sind keine Arbeitsbereiche oder Tests freigegeben.';
} else {
this.problemText = 'Problem bei der Anmeldung.';
// app.interceptor will show error message
const authDataTyped = authData as AuthData;
this.mds.setAuthData(authDataTyped);
if (this.returnTo) {
this.router.navigateByUrl(this.returnTo).then(navOk => {
if (!navOk) {
this.router.navigate(['/r']);
}
});