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
|
@Component({
|
9
|
selector: 'user',
|
10
|
template: `
|
11
|
|
12
|
<div *ngIf="!server" class="uk-margin-top">
|
13
|
<div *ngIf="errorCode == '1'" class="uk-alert uk-alert-warning">
|
14
|
The requested page requires authentication. Please login.
|
15
|
</div>
|
16
|
<div *ngIf="errorCode == '2'" class="uk-alert uk-alert-warning">
|
17
|
You are not authorized to use the requested page
|
18
|
</div>
|
19
|
<div *ngIf="errorCode == '3'" class="uk-alert uk-alert-warning">
|
20
|
The session has expired. Please log in again or continue <a (click)="redirect();">browsing as a guest</a>.
|
21
|
</div>
|
22
|
<form *ngIf="!loggedIn" class=" uk-flex ">
|
23
|
<fieldset>
|
24
|
<legend>Login Form</legend>
|
25
|
<div class="-row">
|
26
|
<input placeholder="Username" type="text" name="username" [(ngModel)]="username">
|
27
|
</div>
|
28
|
<div class="-row">
|
29
|
<input placeholder="Password" type="password" name="password" [(ngModel)]="password">
|
30
|
</div>
|
31
|
<div class="-row">
|
32
|
<button (click)="login()" class=" uk-button uk-button-default">Login</button>
|
33
|
</div>
|
34
|
</fieldset>
|
35
|
</form>
|
36
|
|
37
|
<div *ngIf="!loggedIn && errorMessage.length > 0" class="uk-alert uk-alert-danger">{{errorMessage}}</div>
|
38
|
|
39
|
<div *ngIf="loggedIn">
|
40
|
<div class="uk-alert uk-alert-success">
|
41
|
Hello {{user.fullname}}!
|
42
|
</div>
|
43
|
<button (click)="logout()" class=" uk-button uk-button-default">Log out</button>
|
44
|
</div>
|
45
|
</div>
|
46
|
`
|
47
|
})
|
48
|
|
49
|
export class UserComponent {
|
50
|
public user: User;
|
51
|
public loggedIn: boolean = false;
|
52
|
public server: boolean = true;
|
53
|
public errorMessage: string;
|
54
|
public username: string = "";
|
55
|
public password: string = "";
|
56
|
private sub:any;private sublogin:any;
|
57
|
public errorCode: string = "";
|
58
|
public redirectUrl: string = "";
|
59
|
public routerHelper:RouterHelper = new RouterHelper();
|
60
|
|
61
|
constructor( private router: Router, private _loginService: LoginService, private route: ActivatedRoute ) {}
|
62
|
|
63
|
ngOnInit() {
|
64
|
if( typeof document !== 'undefined') {
|
65
|
this.server = false;
|
66
|
}
|
67
|
this.loggedIn = Session.isLoggedIn();
|
68
|
this.user = Session.getUser();
|
69
|
this.errorMessage = "";
|
70
|
this.sub = this.route.queryParams.subscribe(params => {
|
71
|
this.errorCode = params["errorCode"];
|
72
|
this.redirectUrl = params["redirectUrl"];
|
73
|
this.loggedIn = Session.isLoggedIn();
|
74
|
this.user = Session.getUser();
|
75
|
this.errorMessage = "";
|
76
|
});
|
77
|
}
|
78
|
ngOnDestroy(){
|
79
|
this.sub.unsubscribe();
|
80
|
if(this.sublogin){
|
81
|
this.sublogin.unsubscribe();
|
82
|
}
|
83
|
}
|
84
|
logout(){
|
85
|
if(Session.isLoggedIn()){
|
86
|
Session.removeUser();
|
87
|
}
|
88
|
this.loggedIn = false;
|
89
|
this.user = new User();
|
90
|
this.username = "";
|
91
|
this.password = "";
|
92
|
this.redirect();
|
93
|
|
94
|
}
|
95
|
redirect(){
|
96
|
if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
|
97
|
this.redirectUrl = decodeURIComponent(this.redirectUrl);
|
98
|
var baseUrl = this.redirectUrl;
|
99
|
var queryParams = "";
|
100
|
var paramsArray =[];
|
101
|
var valuesArray =[];
|
102
|
if(this.redirectUrl.indexOf('?') != -1){
|
103
|
baseUrl = this.redirectUrl.split('?')[0];
|
104
|
queryParams = this.redirectUrl.split('?')[1];
|
105
|
}
|
106
|
if(queryParams != ""){
|
107
|
var queryParamsArray = queryParams.split('&');
|
108
|
for(var i = 0; i < queryParamsArray.length; i++){
|
109
|
paramsArray.push(queryParamsArray[i].split("=")[0]);
|
110
|
valuesArray.push(queryParamsArray[i].split("=")[1]);
|
111
|
}
|
112
|
this.router.navigate([baseUrl], { queryParams: this.routerHelper.createQueryParams(paramsArray,valuesArray)});
|
113
|
}else{
|
114
|
this.router.navigate([baseUrl]);
|
115
|
}
|
116
|
}else{
|
117
|
this.router.navigate(['/search/find']);
|
118
|
}
|
119
|
}
|
120
|
login() {
|
121
|
this.sublogin =this._loginService.authenticate(/*this.user*/this.username, this.password).subscribe(
|
122
|
data => {
|
123
|
this.user = data;
|
124
|
this.loggedIn = true;
|
125
|
this.username = "";
|
126
|
this.password = "";
|
127
|
this.errorCode = "";
|
128
|
this.redirect();
|
129
|
|
130
|
},
|
131
|
err => {
|
132
|
console.log(err);
|
133
|
if(err.status == "404") {
|
134
|
this.errorMessage = "Wrong username";
|
135
|
} else if(err.status == "401") {
|
136
|
this.errorMessage = "Wrong password";
|
137
|
}
|
138
|
this.username = "";
|
139
|
this.password = "";
|
140
|
this.errorCode = "";
|
141
|
|
142
|
}
|
143
|
);
|
144
|
}
|
145
|
}
|