Project

General

Profile

1 46737 argiro.kok
import {Component, ElementRef} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
3
import {ActivatedRoute, Router} from '@angular/router';
4
5 48552 argiro.kok
// import {LoginService} from './login.service';
6 46792 argiro.kok
import {User,Session} from './utils/helper.class';
7
import {RouterHelper} from '../utils/routerHelper.class';
8 47451 konstantin
import { Meta} from '../../angular2-meta';
9 48552 argiro.kok
import {OpenaireProperties} from '../utils/properties/openaireProperties';
10 47451 konstantin
11 46737 argiro.kok
@Component({
12
    selector: 'user',
13
    template: `
14 48566 argiro.kok
    <div id="tm-main" class=" uk-section  uk-margin-small-top tm-middle"   >
15
      <div uk-grid uk-grid>
16
       <div class="tm-main uk-width-1-1@s uk-width-1-1@m  uk-width-1-1@l uk-row-first ">
17 46737 argiro.kok
18 47266 argiro.kok
<div *ngIf="!server" class="uk-margin-top uk-container uk-container-small uk-position-relative">
19
20 47343 argiro.kok
21 48552 argiro.kok
    <!--form *ngIf="!loggedIn" class=" ">
22 47343 argiro.kok
23 48596 argiro.kok
            <h3>Welcome to OpenAIRE's Discover Portal</h3>
24 47343 argiro.kok
25
               <p>This service uses the same ldap sign-in  as the OpenAIRE services so you can use the same
26
                   credentials.</p>
27
28
               <p>Login in. To see it in action.</p>
29 47266 argiro.kok
            <div class="uk-margin">
30
             <input class="uk-input uk-form-width-medium" placeholder="Username" type="text" name="username" [(ngModel)]="username">
31 46737 argiro.kok
          </div>
32 47266 argiro.kok
33
          <div class="uk-margin">
34
             <input placeholder="Password" class="uk-input uk-form-width-medium" type="password" name="password" [(ngModel)]="password">
35 46737 argiro.kok
          </div>
36 47266 argiro.kok
          <div class="uk-margin">
37
             <button (click)="login()" class=" uk-button uk-button-primary">Login</button>
38 46737 argiro.kok
          </div>
39 48552 argiro.kok
     </form-->
40 47343 argiro.kok
     <div *ngIf="errorCode == '1'" class="uk-alert uk-alert-warning">
41 48552 argiro.kok
      The requested page requires authentication. Please sign in.
42
      <span  *ngIf="!loggedIn">
43
          <a  class="loginLink"  [href]="loginUrl"  >Sign in</a>
44
       </span>
45 47343 argiro.kok
    </div>
46
    <div *ngIf="errorCode == '2'" class="uk-alert uk-alert-warning">
47
      You are not authorized to use the requested page
48
    </div>
49
    <div *ngIf="errorCode == '3'" class="uk-alert uk-alert-warning">
50 48552 argiro.kok
      The session has expired. Please sign in again or continue <a (click)="redirect();">browsing as a guest</a>.
51
      <span  *ngIf="!loggedIn">
52
          <a  class="loginLink"   [href]="loginUrl"   >Sign in</a>
53
       </span>
54 47343 argiro.kok
    </div>
55 46737 argiro.kok
    <div *ngIf="!loggedIn && errorMessage.length > 0" class="uk-alert uk-alert-danger">{{errorMessage}}</div>
56
57
    <div *ngIf="loggedIn">
58
        <div  class="uk-alert uk-alert-success">
59
        Hello {{user.fullname}}!
60
        </div>
61 47131 argiro.kok
        <button (click)="logout()" class=" uk-button uk-button-default">Log out</button>
62 46737 argiro.kok
    </div>
63
</div>
64 48566 argiro.kok
</div>
65
</div>
66
</div>
67 46737 argiro.kok
    `
68
})
69
70
export class UserComponent {
71
    public user: User;
72
    public loggedIn: boolean = false;
73 46792 argiro.kok
    public server: boolean = true;
74 46737 argiro.kok
    public errorMessage: string;
75
    public username: string = "";
76
    public password: string = "";
77
    private sub:any;private sublogin:any;
78
    public errorCode: string = "";
79
    public redirectUrl: string = "";
80 46792 argiro.kok
    public routerHelper:RouterHelper = new RouterHelper();
81 48552 argiro.kok
    public loginUrl= OpenaireProperties.getLoginURL();
82 46737 argiro.kok
83 48552 argiro.kok
    constructor( private router: Router,
84 47451 konstantin
                 private  route: ActivatedRoute, private _meta: Meta ) {
85 48658 argiro.kok
      this._meta.setTitle("OpenAIRE | Login");
86 47451 konstantin
    }
87 46737 argiro.kok
88
    ngOnInit() {
89 46792 argiro.kok
      if( typeof document !== 'undefined') {
90
        this.server = false;
91 46737 argiro.kok
      }
92 46792 argiro.kok
      this.loggedIn = Session.isLoggedIn();
93
      this.user = Session.getUser();
94 46737 argiro.kok
      this.errorMessage = "";
95
      this.sub =  this.route.queryParams.subscribe(params => {
96
        this.errorCode = params["errorCode"];
97
        this.redirectUrl = params["redirectUrl"];
98 46792 argiro.kok
        this.loggedIn = Session.isLoggedIn();
99
        this.user = Session.getUser();
100 46737 argiro.kok
        this.errorMessage = "";
101
      });
102
    }
103
    ngOnDestroy(){
104
      this.sub.unsubscribe();
105
      if(this.sublogin){
106
        this.sublogin.unsubscribe();
107
      }
108
    }
109
    logout(){
110 46792 argiro.kok
        if(Session.isLoggedIn()){
111
          Session.removeUser();
112
        }
113 46737 argiro.kok
        this.loggedIn = false;
114
        this.user = new User();
115
        this.username = "";
116
        this.password = "";
117 46792 argiro.kok
        this.redirect();
118 46737 argiro.kok
119
    }
120
    redirect(){
121
      if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
122 46792 argiro.kok
        this.redirectUrl = decodeURIComponent(this.redirectUrl);
123
        var baseUrl = this.redirectUrl;
124
        var queryParams = "";
125
        var paramsArray =[];
126
        var valuesArray =[];
127
        if(this.redirectUrl.indexOf('?') != -1){
128
          baseUrl = this.redirectUrl.split('?')[0];
129
          queryParams =  this.redirectUrl.split('?')[1];
130
        }
131
        if(queryParams != ""){
132
          var queryParamsArray = queryParams.split('&');
133
          for(var i = 0; i < queryParamsArray.length; i++){
134
            paramsArray.push(queryParamsArray[i].split("=")[0]);
135
            valuesArray.push(queryParamsArray[i].split("=")[1]);
136
          }
137
          this.router.navigate([baseUrl], { queryParams: this.routerHelper.createQueryParams(paramsArray,valuesArray)});
138
        }else{
139
          this.router.navigate([baseUrl]);
140
        }
141 46737 argiro.kok
      }else{
142 46792 argiro.kok
        this.router.navigate(['/search/find']);
143 46737 argiro.kok
      }
144
    }
145 48552 argiro.kok
    // login() {
146
    //     this.sublogin =this._loginService.authenticate(/*this.user*/this.username, this.password).subscribe(
147
    //         data => {
148
    //             this.user = data;
149
    //             this.loggedIn = true;
150
    //             this.username = "";
151
    //             this.password = "";
152
    //             this.errorCode = "";
153
    //             this.redirect();
154
    //
155
    //         },
156
    //         err => {
157
    //           console.log(err);
158
    //           if(err.status == "404") {
159
    //               this.errorMessage = "Wrong username";
160
    //           } else if(err.status == "401") {
161
    //               this.errorMessage = "Wrong password";
162
    //           }
163
    //           this.username = "";
164
    //           this.password = "";
165
    //           this.errorCode = "";
166
    //
167
    //         }
168
    //     );
169
    // }
170 46737 argiro.kok
}