Project

General

Profile

1

    
2
import {Injectable} from '@angular/core';
3
import {ActivatedRouteSnapshot, CanActivate, CanLoad, Router, RouterStateSnapshot} from '@angular/router';
4
import {AuthenticationService} from './authentication.service';
5
import { getCookie } from '../domain/utils';
6
import {apiUrl} from "../domain/tempAPI";
7

    
8
@Injectable ()
9
export class AuthGuardService implements CanActivate, CanLoad {
10

    
11
//  private oidc_endpoint : string = process.env.OIDC_ENDPOINT;
12
  /*private loginUrl : string = `${apiUrl}/openid_connect_login`;*/
13
  private loginUrl = process.env.AAI_ENDPOINT;
14

    
15
  constructor (private authenticationService: AuthenticationService, private router: Router) {}
16

    
17
  canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
18

    
19
    if ( (getCookie('openAIREUser') !== null) && (getCookie('openAIREUser') !== '') && this.authenticationService.getIsUserLoggedIn() ) { return true; }
20

    
21
    if ( (getCookie('openAIREUser') !== null) && (getCookie('openAIREUser') !== '') ) { return true; }
22

    
23
    // Store the attempted URL for redirecting
24
    sessionStorage.setItem("state.location",state.url);
25

    
26
    // Navigate to the login page via the API
27
    /*window.location.href = this.loginUrl;*/
28
    this.router.navigate(['/landing']);
29

    
30
    return false;
31
  }
32

    
33
  canLoad () {
34

    
35
    if (this.authenticationService.getUserRole() &&
36
        this.authenticationService.getUserRole().includes('ROLE_ADMIN')) {
37

    
38
      return true;
39
    }
40

    
41
    this.router.navigate(['/landing']);
42

    
43
    return false;
44
  }
45
}
(1-1/10)