6ab08d2dddb9a701ba2395e6517b49df95d4f9e0
[eliot.git] / blueprints / common / eliot-ui / frontend-src / src / app / _helpers / auth.guard.ts
1 import { Injectable } from '@angular/core';
2 import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
3 import { Observable } from 'rxjs';
4
5 import { AuthenticationService } from './../_services/authentication.service';
6
7 @Injectable({
8   providedIn: 'root'
9 })
10 export class AuthGuard implements CanActivate {
11
12   constructor(
13     private router: Router,
14     private authenticationService: AuthenticationService
15 ) { }
16   
17   canActivate(
18     next: ActivatedRouteSnapshot,
19     state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
20       const currentUser = this.authenticationService.currentUserValue;
21     if (currentUser) {
22         // authorised so return true
23         return true;
24     }
25
26     // not logged in so redirect to login page
27
28     this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
29     return false;
30   }
31   
32 }