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

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

    
10
//  private oidc_endpoint : string = process.env.OIDC_ENDPOINT;
11
  private loginUrl = process.env.AAI_ENDPOINT;
12

    
13
  constructor (private authenticationService: AuthenticationService, private router: Router) {}
14

    
15
  canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
16

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

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

    
21
    /* If no cookie was found, clear the app's session.
22
       The user may have logged out using another OpenAIRE portal */
23
    sessionStorage.clear();
24

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

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

    
32
    return false;
33
  }
34

    
35
  canLoad () {
36

    
37
    if (this.authenticationService.getUserRole() &&
38
        this.authenticationService.getUserRole().includes('ROLE_ADMIN')) {
39

    
40
      return true;
41
    }
42

    
43
    this.router.navigate(['/landing']);
44

    
45
    return false;
46
  }
47
}
(1-1/10)