Project

General

Profile

1
import { Injectable } from "@angular/core";
2
import { ActivatedRoute, Router } from '@angular/router';
3
import { apiUrl, appBaseUrl } from '../domain/tempAPI';
4
import { deleteCookie, getCookie } from '../domain/utils';
5
import { Http } from '@angular/http';
6

    
7
@Injectable()
8
export class AuthenticationService {
9

    
10
  constructor(private route: ActivatedRoute,
11
              private router: Router,
12
              private http: Http) {}
13

    
14
  /*private apiUrl : string = apiUrl;*/
15
  /*private loginUrl : string = `${this.apiUrl}/openid_connect_login`;*/
16

    
17
  private apiUrl : string = process.env.API_ENDPOINT;
18
  private loginUrl: string = process.env.AAI_ENDPOINT;
19
  private baseUrl: string = process.env.BASE_URL;
20

    
21
  // store the URL so we can redirect after logging in
22
  public redirectUrl: string;
23

    
24
  private _storage: Storage = sessionStorage;
25

    
26

    
27
  isLoggedIn: boolean = false;
28

    
29
  public loginWithState() {
30
    console.log(`logging in with state. Current url is: ${this.router.url}`);
31
    sessionStorage.setItem("state.location", this.router.url);
32
    window.location.href = this.loginUrl;
33
  }
34

    
35
  public logout() {
36
    deleteCookie('openAIREUser');
37
    deleteCookie('AccessToken');
38
    sessionStorage.removeItem('name');
39
    sessionStorage.removeItem('email');
40
    sessionStorage.removeItem('role');
41
    this.isLoggedIn = false;
42

    
43
    console.log('logging out, calling:');
44
    console.log(`${this.apiUrl}/openid_logout`);
45

    
46
    /*window.location.href = `${this.apiUrl}/openid_logout`;*/
47
    window.location.href = `https://aai.openaire.eu/proxy/saml2/idp/SingleLogoutService.php?ReturnTo=${this.apiUrl}/openid_logout`;
48
  }
49

    
50
  public tryLogin() {
51
    if( getCookie('openAIREUser') && (getCookie('openAIREUser') !== '') ) {
52
      console.log(`I got the cookie!`);
53
      /* SETTING INTERVAL TO REFRESH SESSION TIMEOUT COUNTDOWN */
54
      setInterval(() => {
55
        this.http.get(this.apiUrl + '/user/login',{ withCredentials: true }).subscribe(
56
          userInfo => {
57
            console.log("User is still logged in");
58
            console.log(userInfo.json());
59
            this.isLoggedIn = true;
60
          },
61
          () => {
62
            sessionStorage.removeItem('name');
63
            sessionStorage.removeItem('email');
64
            sessionStorage.removeItem('role');
65
            deleteCookie('openAIREUser');
66
            deleteCookie('AccessToken');
67
            this.isLoggedIn = false;
68
            this.router.navigate(['/landing']);
69
          }
70
        );
71
      },1000 * 60 * 5);
72
      if(!sessionStorage.getItem('name')) {
73
        console.log(`session.name wasn't found --> logging in via repo-service!`);
74
        this.http.get(this.apiUrl + '/user/login',{ withCredentials: true }).subscribe(
75
          userInfo => {
76
            console.log(userInfo.json());
77
            sessionStorage.setItem('name',userInfo.json()['name']);
78
            sessionStorage.setItem('email',userInfo.json()['email']);
79
            sessionStorage.setItem('role',userInfo.json()['role']);
80
            this.isLoggedIn = true;
81
            console.log(`the current user is: ${sessionStorage.getItem('name')}, ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
82
          },
83
          () => {
84
            sessionStorage.removeItem('name');
85
            sessionStorage.removeItem('email');
86
            sessionStorage.removeItem('role');
87
            deleteCookie('openAIREUser');
88
            deleteCookie('AccessToken');
89
            this.isLoggedIn = false;
90
          }
91
        );
92
      } else {
93
        this.isLoggedIn = true;
94
        console.log(`the current user is: ${sessionStorage.getItem('name')}, ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
95
      }
96
      if ( sessionStorage.getItem("state.location") ) {
97
        let state = sessionStorage.getItem("state.location");
98
        sessionStorage.removeItem("state.location");
99
        console.log(`tried to login - returning to state: ${state}`);
100
        if (this.redirectUrl) {
101
          this.router.navigate([this.redirectUrl]);
102
        } else {
103
          this.router.navigate([state]);
104
        }
105
      }
106
    }
107
  }
108

    
109
  public getIsUserLoggedIn() {
110
    return this.isLoggedIn;
111
  }
112

    
113
  public getUserName() {
114
    if (this.isLoggedIn)
115
      return sessionStorage.getItem('name');
116
    else
117
      return '';
118
  }
119

    
120
  public getUserEmail() {
121
    if (this.isLoggedIn)
122
      return sessionStorage.getItem('email');
123
    else
124
      return '';
125
  }
126

    
127
  public getUserRole() {
128
    if (this.isLoggedIn)
129
      return sessionStorage.getItem('role');
130
    else
131
      return '';
132
  }
133

    
134
}
(3-3/10)