Skip to content
Snippets Groups Projects
login.component.ts 2.87 KiB
Newer Older
paflov's avatar
paflov committed
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { CustomtextService } from 'iqb-components';
import { ActivatedRoute, Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { MainDataService } from '../../maindata.service';
import { AuthData } from '../../app.interfaces';
import { BackendService } from '../../backend.service';

@Component({
  templateUrl: './login.component.html',
  styles: [
    'mat-card {margin: 10px;}',
    '.mat-card-gray {background-color: lightgray}'
paflov's avatar
paflov committed
export class LoginComponent implements OnInit, OnDestroy {
paf's avatar
paf committed
  private routingSubscription: Subscription = null;
  problemText = '';

  loginForm = new FormGroup({
    name: new FormControl(LoginComponent.oldLoginName, [Validators.required, Validators.minLength(3)]),
    pw: new FormControl('')
  });

  systemAnnouncement: string;

  constructor(
    public mds: MainDataService,
    public cts: CustomtextService,
    private bs: BackendService,
    private router: Router,
    private route: ActivatedRoute
  ) { }

  ngOnInit(): void {
    this.mds.setSpinnerOff();
    this.routingSubscription = this.route.params.subscribe(params => {
paflov's avatar
paflov committed
      this.returnTo = params.returnTo;
paf's avatar
paf committed
    });
    setTimeout(() => { // the timeout is  avery temporary fix.- after upgrading to iqb-components 3, it can be removed
      this.systemAnnouncement = this.cts.getCustomText('system_announcement', '-');
    }, 500);
paflov's avatar
paflov committed
  login(): void {
    const loginData = this.loginForm.value;
paflov's avatar
paflov committed
    LoginComponent.oldLoginName = loginData.name;
    this.mds.setSpinnerOn();
paflov's avatar
paflov committed
    this.bs.login(loginData.name, loginData.pw).subscribe(
      authData => {
        this.mds.setSpinnerOff();
        this.problemText = '';
        if (typeof authData === 'number') {
          const errCode = authData as number;
          if (errCode === 400) {
            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);
            this.router.navigateByUrl(this.returnTo).then(navOk => {
              if (!navOk) {
                this.router.navigate(['/r']);
              }
            });
            this.router.navigate(['/r']);
paflov's avatar
paflov committed
  ngOnDestroy(): void {
    if (this.routingSubscription !== null) {
      this.routingSubscription.unsubscribe();
    }
  }
}