# All files / src/app/core/auth/login login.component.ts

68.57% Statements 24/35
73.33% Branches 11/15
40% Functions 2/5
67.74% Lines 21/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    646x
    6x

    6x

    6x
    6x
    6x

    6x
    2x
    2x

    2x
    2x
    2x
    2x

    6x

    3x
    3x
    2x

    3x

    3x

    6x

    6x
     import { Component, OnInit } from '@angular/core';
    import { Router } from '@angular/router';

    import { BsModalService } from 'ngx-bootstrap/modal';

    import { AuthService } from '../../../shared/api/auth.service';
    import { Credentials } from '../../../shared/models/credentials';
    import { AuthStorageService } from '../../../shared/services/auth-storage.service';

    @Component({
      selector: 'cd-login',
      template: require('./login.component.html'),
      styles: []
    })
    export class LoginComponent implements OnInit {
      model = new Credentials();
      isLoginActive = false;

      constructor(
        private authService: AuthService,
        private authStorageService: AuthStorageService,
        private bsModalService: BsModalService,
        private router: Router
      ) {}

      ngOnInit() {
        if (this.authStorageService.isLoggedIn()) {
          this.router.navigate(['']);
        } else {
          // Make sure all open modal dialogs are closed. This might be
          // necessary when the logged in user is redirected to the login
          // page after a 401.
          const modalsCount = this.bsModalService.getModalsCount();
          for (let i = 1; i <= modalsCount; i++) {
            this.bsModalService.hide(i);
          }
          let token = null;
          if (window.location.hash.indexOf('access_token=') !== -1) {
            token = window.location.hash.split('access_token=')[1];
            const uri = window.location.toString();
            window.history.replaceState({}, document.title, uri.split('?')[0]);
          }
          this.authService.check(token).subscribe((login: any) => {
            if (login.login_url) {
              if (login.login_url === '#/login') {
                this.isLoginActive = true;
              } else {
                window.location.replace(login.login_url);
              }
            } else {
              this.authStorageService.set(login.username, token, login.permissions);
              this.router.navigate(['']);
            }
          });
        }
      }

      login() {
        this.authService.login(this.model).then(() => {
          this.router.navigate(['']);
        });
      }
    }