Project

General

Profile

1 55365 argiro.kok
import {Component, ElementRef, Input}        from '@angular/core';
2 51835 sofia.balt
import {ActivatedRoute, Router}       from '@angular/router';
3
import {Title, Meta}                  from '@angular/platform-browser';
4 50169 argiro.kok
5 51835 sofia.balt
import {User,Session}                 from './utils/helper.class';
6
import {RouterHelper}                 from '../utils/routerHelper.class';
7
8
import {EnvProperties}                from '../utils/properties/env-properties';
9
10 50169 argiro.kok
@Component({
11
    selector: 'user',
12
    templateUrl: 'user.component.html'
13
  })
14
15
export class UserComponent {
16
    public user: User;
17
    public loggedIn: boolean = false;
18
    public server: boolean = true;
19 52501 argiro.kok
    public errorMessage: string = "";
20 50169 argiro.kok
    public password: string = "";
21 54903 k.triantaf
    private sub:any;
22
    private sublogin:any;
23 50169 argiro.kok
    public errorCode: string = "";
24
    public redirectUrl: string = "";
25
    public routerHelper:RouterHelper = new RouterHelper();
26 50586 argiro.kok
    public loginUrl;
27
    properties:EnvProperties;
28 55659 k.triantaf
    @Input() mainComponent = true;
29 50169 argiro.kok
30
    constructor( private router: Router,
31 51835 sofia.balt
                 private route: ActivatedRoute,
32
                 private _meta: Meta,
33
                 private _title: Title) {
34
35
                   var title = "OpenAIRE | Login";
36
                   this._title.setTitle(title);
37 50169 argiro.kok
    }
38
39
    ngOnInit() {
40 50586 argiro.kok
      this.route.data
41
        .subscribe((data: { envSpecific: EnvProperties }) => {
42
           this.properties = data.envSpecific;
43
          this.loginUrl = this.properties.loginUrl;
44
45
        });
46 50169 argiro.kok
      if( typeof document !== 'undefined') {
47
        this.server = false;
48
      }
49
      this.loggedIn = Session.isLoggedIn();
50
      this.user = Session.getUser();
51
      this.errorMessage = "";
52
      this.sub =  this.route.queryParams.subscribe(params => {
53
        this.errorCode = params["errorCode"];
54
        this.redirectUrl = params["redirectUrl"];
55
        this.loggedIn = Session.isLoggedIn();
56
        this.user = Session.getUser();
57
        this.errorMessage = "";
58
        if(this.loggedIn){
59
          this.redirect();
60
        }
61
      });
62
    }
63
    ngOnDestroy(){
64
      this.sub.unsubscribe();
65
      if(this.sublogin){
66
        this.sublogin.unsubscribe();
67
      }
68
    }
69 54903 k.triantaf
70 50169 argiro.kok
    redirect(){
71
      if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
72
        this.redirectUrl = decodeURIComponent(this.redirectUrl);
73
        var baseUrl = this.redirectUrl;
74
        var queryParams = "";
75
        var paramsArray =[];
76
        var valuesArray =[];
77
        if(this.redirectUrl.indexOf('?') != -1){
78
          baseUrl = this.redirectUrl.split('?')[0];
79
          queryParams =  this.redirectUrl.split('?')[1];
80
        }
81
        if(queryParams != ""){
82
          var queryParamsArray = queryParams.split('&');
83
          for(var i = 0; i < queryParamsArray.length; i++){
84
            paramsArray.push(queryParamsArray[i].split("=")[0]);
85
            valuesArray.push(queryParamsArray[i].split("=")[1]);
86
          }
87
          this.router.navigate([baseUrl], { queryParams: this.routerHelper.createQueryParams(paramsArray,valuesArray)});
88
        }else{
89
          this.router.navigate([baseUrl]);
90
        }
91
      }else{
92
        this.router.navigate(['/']);
93
      }
94
    }
95 54903 k.triantaf
96 50169 argiro.kok
    logIn(){
97
      if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
98
        this.redirectUrl = decodeURIComponent(this.redirectUrl);
99
        var baseUrl = this.redirectUrl;
100
        var queryParams = "";
101
        if(this.redirectUrl.indexOf('?') != -1){
102
          var splits =this.redirectUrl.split('?');
103
          if(splits.length>0){
104
            baseUrl = splits[0];
105
          }
106
          if(splits.length >1){
107
            queryParams =  splits[1];
108
          }
109
        }
110 52177 argiro.kok
        Session.setReloadUrl(location.protocol +"//"+location.host, baseUrl, queryParams);
111 50169 argiro.kok
      }
112
113 50586 argiro.kok
      window.location.href = this.properties.loginUrl;
114 50169 argiro.kok
    }
115
116 52309 argiro.kok
117 50169 argiro.kok
}