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

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

    
23
  private _storage: Storage = sessionStorage;
24

    
25

    
26
  isLoggedIn: boolean = false;
27

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

    
34
  public logout(){
35
    deleteCookie('currentUser');
36
    sessionStorage.removeItem('name');
37
    sessionStorage.removeItem('email');
38
    sessionStorage.removeItem('role');
39
    this.isLoggedIn = false;
40
    const baseUrl = appBaseUrl;
41
    console.log('logging out, going to:');
42
    console.log(`https://aai.openaire.eu/proxy/saml2/idp/SingleLogoutService.php?ReturnTo=${baseUrl}/landing`);
43
    window.location.href = `https://aai.openaire.eu/proxy/saml2/idp/SingleLogoutService.php?ReturnTo=${baseUrl}/landing`;
44
  }
45

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

    
99
  public getIsUserLoggedIn() {
100
    return this.isLoggedIn;
101
  }
102

    
103
  public getUserName() {
104
    if (this.isLoggedIn)
105
      return sessionStorage.getItem('name');
106
  }
107

    
108
  public getUserEmail() {
109
    if (this.isLoggedIn)
110
      return sessionStorage.getItem('email');
111
  }
112

    
113
  public getUserRole() {
114
    if (this.isLoggedIn)
115
      return sessionStorage.getItem('role');
116
  }
117

    
118
}
(2-2/8)