1 import { Component, OnInit } from '@angular/core';
2 import { Router } from '@angular/router';
4 import { BsModalService } from 'ngx-bootstrap/modal';
6 import { AuthService } from '../../../shared/api/auth.service';
7 import { Credentials } from '../../../shared/models/credentials';
8 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
12 templateUrl: './login.component.html',
13 styleUrls: ['./login.component.scss']
15 export class LoginComponent implements OnInit {
16 model = new Credentials();
17 isLoginActive = false;
20 private authService: AuthService,
21 private authStorageService: AuthStorageService,
22 private bsModalService: BsModalService,
23 private router: Router
27 if (this.authStorageService.isLoggedIn()) {
28 this.router.navigate(['']);
30 // Make sure all open modal dialogs are closed. This might be
31 // necessary when the logged in user is redirected to the login
33 const modalsCount = this.bsModalService.getModalsCount();
34 for (let i = 1; i <= modalsCount; i++) {
35 this.bsModalService.hide(i);
38 if (window.location.hash.indexOf('access_token=') !== -1) {
39 token = window.location.hash.split('access_token=')[1];
40 const uri = window.location.toString();
41 window.history.replaceState({}, document.title, uri.split('?')[0]);
43 this.authService.check(token).subscribe((login: any) => {
44 if (login.login_url) {
45 if (login.login_url === '#/login') {
46 this.isLoginActive = true;
48 window.location.replace(login.login_url);
51 this.authStorageService.set(login.username, token, login.permissions);
52 this.router.navigate(['']);
59 this.authService.login(this.model).then(() => {
60 this.router.navigate(['']);