1
|
import {Component, ElementRef} from '@angular/core';
|
2
|
import {Observable} from 'rxjs/Observable';
|
3
|
import {ActivatedRoute, Router} from '@angular/router';
|
4
|
|
5
|
// import {LoginService} from './login.service';
|
6
|
import {User,Session} from './utils/helper.class';
|
7
|
import {RouterHelper} from '../utils/routerHelper.class';
|
8
|
import { Meta} from '../../angular2-meta';
|
9
|
import {OpenaireProperties} from '../utils/properties/openaireProperties';
|
10
|
|
11
|
@Component({
|
12
|
selector: 'user',
|
13
|
templateUrl: 'user.component.html'
|
14
|
})
|
15
|
|
16
|
export class UserComponent {
|
17
|
public user: User;
|
18
|
public loggedIn: boolean = false;
|
19
|
public server: boolean = true;
|
20
|
public errorMessage: string;
|
21
|
public username: string = "";
|
22
|
public password: string = "";
|
23
|
private sub:any;private sublogin:any;
|
24
|
public errorCode: string = "";
|
25
|
public redirectUrl: string = "";
|
26
|
public routerHelper:RouterHelper = new RouterHelper();
|
27
|
public loginUrl= OpenaireProperties.getLoginURL();
|
28
|
|
29
|
constructor( private router: Router,
|
30
|
private route: ActivatedRoute, private _meta: Meta ) {
|
31
|
this._meta.setTitle("OpenAIRE | Login");
|
32
|
}
|
33
|
|
34
|
ngOnInit() {
|
35
|
if( typeof document !== 'undefined') {
|
36
|
this.server = false;
|
37
|
}
|
38
|
this.loggedIn = Session.isLoggedIn();
|
39
|
this.user = Session.getUser();
|
40
|
this.errorMessage = "";
|
41
|
this.sub = this.route.queryParams.subscribe(params => {
|
42
|
this.errorCode = params["errorCode"];
|
43
|
this.redirectUrl = params["redirectUrl"];
|
44
|
this.loggedIn = Session.isLoggedIn();
|
45
|
this.user = Session.getUser();
|
46
|
this.errorMessage = "";
|
47
|
if(this.loggedIn){
|
48
|
this.redirect();
|
49
|
}
|
50
|
});
|
51
|
}
|
52
|
ngOnDestroy(){
|
53
|
this.sub.unsubscribe();
|
54
|
if(this.sublogin){
|
55
|
this.sublogin.unsubscribe();
|
56
|
}
|
57
|
}
|
58
|
logout(){
|
59
|
if(Session.isLoggedIn()){
|
60
|
Session.removeUser();
|
61
|
}
|
62
|
this.loggedIn = false;
|
63
|
this.user = new User();
|
64
|
this.username = "";
|
65
|
this.password = "";
|
66
|
this.redirect();
|
67
|
|
68
|
}
|
69
|
redirect(){
|
70
|
if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
|
71
|
this.redirectUrl = decodeURIComponent(this.redirectUrl);
|
72
|
var baseUrl = this.redirectUrl;
|
73
|
var queryParams = "";
|
74
|
var paramsArray =[];
|
75
|
var valuesArray =[];
|
76
|
if(this.redirectUrl.indexOf('?') != -1){
|
77
|
baseUrl = this.redirectUrl.split('?')[0];
|
78
|
queryParams = this.redirectUrl.split('?')[1];
|
79
|
}
|
80
|
if(queryParams != ""){
|
81
|
var queryParamsArray = queryParams.split('&');
|
82
|
for(var i = 0; i < queryParamsArray.length; i++){
|
83
|
paramsArray.push(queryParamsArray[i].split("=")[0]);
|
84
|
valuesArray.push(queryParamsArray[i].split("=")[1]);
|
85
|
}
|
86
|
this.router.navigate([baseUrl], { queryParams: this.routerHelper.createQueryParams(paramsArray,valuesArray)});
|
87
|
}else{
|
88
|
this.router.navigate([baseUrl]);
|
89
|
}
|
90
|
}else{
|
91
|
this.router.navigate(['/']);
|
92
|
}
|
93
|
}
|
94
|
// login() {
|
95
|
// this.sublogin =this._loginService.authenticate(/*this.user*/this.username, this.password).subscribe(
|
96
|
// data => {
|
97
|
// this.user = data;
|
98
|
// this.loggedIn = true;
|
99
|
// this.username = "";
|
100
|
// this.password = "";
|
101
|
// this.errorCode = "";
|
102
|
// this.redirect();
|
103
|
//
|
104
|
// },
|
105
|
// err => {
|
106
|
// console.log(err);
|
107
|
// if(err.status == "404") {
|
108
|
// this.errorMessage = "Wrong username";
|
109
|
// } else if(err.status == "401") {
|
110
|
// this.errorMessage = "Wrong password";
|
111
|
// }
|
112
|
// this.username = "";
|
113
|
// this.password = "";
|
114
|
// this.errorCode = "";
|
115
|
//
|
116
|
// }
|
117
|
// );
|
118
|
// }
|
119
|
}
|