Project

General

Profile

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

    
5
import {Observable}                   from 'rxjs/Observable';
6

    
7
// import {LoginService}                 from './login.service';
8
import {User,Session}                 from './utils/helper.class';
9
import {RouterHelper}                 from '../utils/routerHelper.class';
10

    
11
import {EnvProperties}                from '../utils/properties/env-properties';
12

    
13
@Component({
14
    selector: 'user',
15
    templateUrl: 'user.component.html'
16
  })
17

    
18
export class UserComponent {
19
    public user: User;
20
    public loggedIn: boolean = false;
21
    public server: boolean = true;
22
    public errorMessage: string;
23
    public username: string = "";
24
    public password: string = "";
25
    private sub:any;private sublogin:any;
26
    public errorCode: string = "";
27
    public redirectUrl: string = "";
28
    public routerHelper:RouterHelper = new RouterHelper();
29
    public loginUrl;
30
    properties:EnvProperties;
31

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

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

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

    
47
        });
48
      if( typeof document !== 'undefined') {
49
        this.server = false;
50
      }
51
      this.loggedIn = Session.isLoggedIn();
52
      this.user = Session.getUser();
53
      this.errorMessage = "";
54
      this.sub =  this.route.queryParams.subscribe(params => {
55
        this.errorCode = params["errorCode"];
56
        this.redirectUrl = params["redirectUrl"];
57
        this.loggedIn = Session.isLoggedIn();
58
        this.user = Session.getUser();
59
        this.errorMessage = "";
60
        if(this.loggedIn){
61
          this.redirect();
62
        }
63
      });
64
    }
65
    ngOnDestroy(){
66
      this.sub.unsubscribe();
67
      if(this.sublogin){
68
        this.sublogin.unsubscribe();
69
      }
70
    }
71
    // logout(){
72
    //     if(Session.isLoggedIn()){
73
    //       Session.removeUser();
74
    //     }
75
    //     this.loggedIn = false;
76
    //     this.user = new User();
77
    //     this.username = "";
78
    //     this.password = "";
79
    //     this.redirect();
80
    //
81
    // }
82
    redirect(){
83
      if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
84
        this.redirectUrl = decodeURIComponent(this.redirectUrl);
85
        var baseUrl = this.redirectUrl;
86
        var queryParams = "";
87
        var paramsArray =[];
88
        var valuesArray =[];
89
        if(this.redirectUrl.indexOf('?') != -1){
90
          baseUrl = this.redirectUrl.split('?')[0];
91
          queryParams =  this.redirectUrl.split('?')[1];
92
        }
93
        if(queryParams != ""){
94
          var queryParamsArray = queryParams.split('&');
95
          for(var i = 0; i < queryParamsArray.length; i++){
96
            paramsArray.push(queryParamsArray[i].split("=")[0]);
97
            valuesArray.push(queryParamsArray[i].split("=")[1]);
98
          }
99
          this.router.navigate([baseUrl], { queryParams: this.routerHelper.createQueryParams(paramsArray,valuesArray)});
100
        }else{
101
          this.router.navigate([baseUrl]);
102
        }
103
      }else{
104
        this.router.navigate(['/']);
105
      }
106
    }
107
    logIn(){
108
      if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
109
        this.redirectUrl = decodeURIComponent(this.redirectUrl);
110
        var baseUrl = this.redirectUrl;
111
        var queryParams = "";
112
        var paramsArray =[];
113
        var valuesArray =[];
114
        if(this.redirectUrl.indexOf('?') != -1){
115
          var splits =this.redirectUrl.split('?');
116
          if(splits.length>0){
117
            baseUrl = splits[0];
118
          }
119
          if(splits.length >1){
120
            queryParams =  splits[1];
121
          }
122
        }
123
        Session.setReloadUrl(location.protocol +"//"+location.host, baseUrl, queryParams);
124
      }
125

    
126
      window.location.href = this.properties.loginUrl;
127
    }
128

    
129
 
130
}
(7-7/9)