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.WORKFLOW_API_ENDPOINT;
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('currentUser');
37
    sessionStorage.removeItem('name');
38
    sessionStorage.removeItem('email');
39
    sessionStorage.removeItem('role');
40
    this.isLoggedIn = false;
41

    
42
    console.log('logging out, going to:');
43
    console.log(`https://aai.openaire.eu/proxy/saml2/idp/SingleLogoutService.php?ReturnTo=${this.baseUrl}/landing`);
44
    window.location.href = `https://aai.openaire.eu/proxy/saml2/idp/SingleLogoutService.php?ReturnTo=${this.baseUrl}/landing`;
45
  }
46

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

    
102
  public getIsUserLoggedIn() {
103
    return this.isLoggedIn;
104
  }
105

    
106
  public getUserName() {
107
    if (this.isLoggedIn)
108
      return sessionStorage.getItem('name');
109
    else
110
      return '';
111
  }
112

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

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

    
127
}
(2-2/8)