Project

General

Profile

1
import {Component, ElementRef, Input}        from '@angular/core';
2
import {ActivatedRoute, Router}       from '@angular/router';
3
import {Title, Meta}                  from '@angular/platform-browser';
4

    
5
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
@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
    public errorMessage: string = "";
20
    public password: string = "";
21
    private sub:any;
22
    private sublogin:any;
23
    public errorCode: string = "";
24
    public redirectUrl: string = "";
25
    public routerHelper:RouterHelper = new RouterHelper();
26
    public loginUrl;
27
    properties:EnvProperties;
28
    @Input() mainComponent = true;
29

    
30
    constructor( private router: Router,
31
                 private route: ActivatedRoute,
32
                 private _meta: Meta,
33
                 private _title: Title) {
34

    
35
                   var title = "OpenAIRE | Login";
36
                   this._title.setTitle(title);
37
    }
38

    
39
    ngOnInit() {
40
      this.route.data
41
        .subscribe((data: { envSpecific: EnvProperties }) => {
42
           this.properties = data.envSpecific;
43
          this.loginUrl = this.properties.loginUrl;
44

    
45
        });
46
      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

    
70
    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

    
96
    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
        Session.setReloadUrl(location.protocol +"//"+location.host, baseUrl, queryParams);
111
      }
112

    
113
      window.location.href = this.properties.loginUrl;
114
    }
115

    
116

    
117
}
(7-7/9)