2017-12-19 13:53:46 +01:00
|
|
|
import {Component, ElementRef} from '@angular/core';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
|
|
|
|
// import {LoginService} from './login.service';
|
|
|
|
import {User,Session} from './utils/helper.class';
|
|
|
|
import {RouterHelper} from '../utils/routerHelper.class';
|
2018-02-12 14:38:30 +01:00
|
|
|
import { Meta} from '../sharedComponents/metaService';
|
2018-02-05 14:14:59 +01:00
|
|
|
import{EnvProperties} from '../utils/properties/env-properties';
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'user',
|
|
|
|
templateUrl: 'user.component.html'
|
|
|
|
})
|
|
|
|
|
|
|
|
export class UserComponent {
|
|
|
|
public user: User;
|
|
|
|
public loggedIn: boolean = false;
|
|
|
|
public server: boolean = true;
|
|
|
|
public errorMessage: string;
|
|
|
|
public username: string = "";
|
|
|
|
public password: string = "";
|
|
|
|
private sub:any;private sublogin:any;
|
|
|
|
public errorCode: string = "";
|
|
|
|
public redirectUrl: string = "";
|
|
|
|
public routerHelper:RouterHelper = new RouterHelper();
|
2018-02-05 14:14:59 +01:00
|
|
|
public loginUrl;
|
|
|
|
properties:EnvProperties;
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
constructor( private router: Router,
|
|
|
|
private route: ActivatedRoute, private _meta: Meta) {
|
|
|
|
this._meta.setTitle("OpenAIRE | Login");
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2018-02-05 14:14:59 +01:00
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
this.loginUrl = this.properties.loginUrl;
|
|
|
|
|
|
|
|
});
|
2017-12-19 13:53:46 +01:00
|
|
|
if( typeof document !== 'undefined') {
|
|
|
|
this.server = false;
|
|
|
|
}
|
|
|
|
this.loggedIn = Session.isLoggedIn();
|
|
|
|
this.user = Session.getUser();
|
|
|
|
this.errorMessage = "";
|
|
|
|
this.sub = this.route.queryParams.subscribe(params => {
|
|
|
|
this.errorCode = params["errorCode"];
|
|
|
|
this.redirectUrl = params["redirectUrl"];
|
|
|
|
this.loggedIn = Session.isLoggedIn();
|
|
|
|
this.user = Session.getUser();
|
|
|
|
this.errorMessage = "";
|
|
|
|
if(this.loggedIn){
|
|
|
|
this.redirect();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
ngOnDestroy(){
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
if(this.sublogin){
|
|
|
|
this.sublogin.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// logout(){
|
|
|
|
// if(Session.isLoggedIn()){
|
|
|
|
// Session.removeUser();
|
|
|
|
// }
|
|
|
|
// this.loggedIn = false;
|
|
|
|
// this.user = new User();
|
|
|
|
// this.username = "";
|
|
|
|
// this.password = "";
|
|
|
|
// this.redirect();
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
redirect(){
|
|
|
|
if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
|
|
|
|
this.redirectUrl = decodeURIComponent(this.redirectUrl);
|
|
|
|
var baseUrl = this.redirectUrl;
|
|
|
|
var queryParams = "";
|
|
|
|
var paramsArray =[];
|
|
|
|
var valuesArray =[];
|
|
|
|
if(this.redirectUrl.indexOf('?') != -1){
|
|
|
|
baseUrl = this.redirectUrl.split('?')[0];
|
|
|
|
queryParams = this.redirectUrl.split('?')[1];
|
|
|
|
}
|
|
|
|
if(queryParams != ""){
|
|
|
|
var queryParamsArray = queryParams.split('&');
|
|
|
|
for(var i = 0; i < queryParamsArray.length; i++){
|
|
|
|
paramsArray.push(queryParamsArray[i].split("=")[0]);
|
|
|
|
valuesArray.push(queryParamsArray[i].split("=")[1]);
|
|
|
|
}
|
|
|
|
this.router.navigate([baseUrl], { queryParams: this.routerHelper.createQueryParams(paramsArray,valuesArray)});
|
|
|
|
}else{
|
|
|
|
this.router.navigate([baseUrl]);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
this.router.navigate(['/']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logIn(){
|
|
|
|
if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
|
|
|
|
this.redirectUrl = decodeURIComponent(this.redirectUrl);
|
|
|
|
var baseUrl = this.redirectUrl;
|
|
|
|
var queryParams = "";
|
|
|
|
var paramsArray =[];
|
|
|
|
var valuesArray =[];
|
|
|
|
if(this.redirectUrl.indexOf('?') != -1){
|
|
|
|
var splits =this.redirectUrl.split('?');
|
|
|
|
if(splits.length>0){
|
|
|
|
baseUrl = splits[0];
|
|
|
|
}
|
|
|
|
if(splits.length >1){
|
|
|
|
queryParams = splits[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log("saving url & params...");
|
|
|
|
console.log(queryParams);
|
|
|
|
Session.setCurrentUrl(baseUrl);
|
|
|
|
Session.setCurrentParameters(queryParams);
|
|
|
|
}
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
window.location.href = this.properties.loginUrl;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// login() {
|
|
|
|
// this.sublogin =this._loginService.authenticate(/*this.user*/this.username, this.password).subscribe(
|
|
|
|
// data => {
|
|
|
|
// this.user = data;
|
|
|
|
// this.loggedIn = true;
|
|
|
|
// this.username = "";
|
|
|
|
// this.password = "";
|
|
|
|
// this.errorCode = "";
|
|
|
|
// this.redirect();
|
|
|
|
//
|
|
|
|
// },
|
|
|
|
// err => {
|
|
|
|
// console.log(err);
|
|
|
|
// if(err.status == "404") {
|
|
|
|
// this.errorMessage = "Wrong username";
|
|
|
|
// } else if(err.status == "401") {
|
|
|
|
// this.errorMessage = "Wrong password";
|
|
|
|
// }
|
|
|
|
// this.username = "";
|
|
|
|
// this.password = "";
|
|
|
|
// this.errorCode = "";
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
}
|