From 79b54ca975d933597dfed48e740ae5edcda0766b Mon Sep 17 00:00:00 2001 From: "konstantina.galouni" Date: Wed, 14 Dec 2016 14:53:50 +0000 Subject: [PATCH] Login Functionality added (accepted credentials: username=sba password=12345678) git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-portal/trunk@44972 d315682c-612b-4755-9ff5-7f18f6832af3 --- .../src/app/login/login-routing.module.ts | 14 --- portal-2/src/app/login/login.component.ts | 100 ------------------ portal-2/src/app/login/login.module.ts | 33 ------ portal-2/src/app/services/login.service.ts | 18 +++- .../app/sharedComponents/login.component.ts | 79 ++++++++++++++ .../navigationBar.component.ts | 2 +- .../sharedComponents.module.ts | 8 +- portal-2/src/app/utils/entities/user.ts | 7 ++ .../utils/properties/openaireProperties.ts | 2 +- 9 files changed, 107 insertions(+), 156 deletions(-) delete mode 100644 portal-2/src/app/login/login-routing.module.ts delete mode 100644 portal-2/src/app/login/login.component.ts delete mode 100644 portal-2/src/app/login/login.module.ts create mode 100644 portal-2/src/app/sharedComponents/login.component.ts create mode 100644 portal-2/src/app/utils/entities/user.ts diff --git a/portal-2/src/app/login/login-routing.module.ts b/portal-2/src/app/login/login-routing.module.ts deleted file mode 100644 index c5a41217..00000000 --- a/portal-2/src/app/login/login-routing.module.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { NgModule } from '@angular/core'; -import { RouterModule } from '@angular/router'; - -import { LoginComponent } from './login.component'; - -@NgModule({ - imports: [ - RouterModule.forChild([ - { path: 'login', component: LoginComponent } - - ]) - ] -}) -export class LoginRoutingModule { } diff --git a/portal-2/src/app/login/login.component.ts b/portal-2/src/app/login/login.component.ts deleted file mode 100644 index 6ad632cc..00000000 --- a/portal-2/src/app/login/login.component.ts +++ /dev/null @@ -1,100 +0,0 @@ -// import { Component } from '@angular/core'; -// import { Router } from '@angular/router'; -// import { Http } from '@angular/http'; -// // import { contentHeaders } from '../../common/headers'; -// -// // const styles = require('./login.css'); -// // const template = require('./login.html'); -// -// @Component({ -// selector: 'login', -// template: template, -// styles: [ styles ] -// }) -// export class Login { -// constructor(public router: Router, public http: Http) { -// } -// -// login(event, username, password) { -// event.preventDefault(); -// let body = JSON.stringify({ username, password }); -// localStorage.setItem('id_token',"test"); -// // this.http.post('http://localhost:3001/sessions/create', body, { headers: contentHeaders }) -// // .subscribe( -// // response => { -// // localStorage.setItem('id_token', response.json().id_token); -// // this.router.navigate(['home']); -// // }, -// // error => { -// // alert(error.text()); -// // console.log(error.text()); -// // } -// // ); -// } -// -// signup(event) { -// event.preventDefault(); -// this.router.navigate(['signup']); -// } -// } - - -import {Component, ElementRef, Input} from '@angular/core'; -import {Observable} from 'rxjs/Observable'; -import {LoginService} from '../services/login.service'; - -@Component({ - selector: 'login', - template: ` -
-
- Welcome in Login -
-
- - - -
-
- ` -}) - -export class LoginComponent { - - @Input() email: string = "sba"; - @Input() password: string = "12345678"; - - constructor( private _loginService: LoginService ) {} - - login() { - this._loginService.authenticate(this.email, this.password).subscribe( - data => { - console.info(data); - }, - err => { - console.log(err); - } - ); - - } -} diff --git a/portal-2/src/app/login/login.module.ts b/portal-2/src/app/login/login.module.ts deleted file mode 100644 index 9935e8c3..00000000 --- a/portal-2/src/app/login/login.module.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { NgModule} from '@angular/core'; -//import { CommonModule } from '@angular/common'; -//import { FormsModule } from '@angular/forms'; - -//import {UtilsModule} from '../utils/utils.module'; -//import {ClaimModule} from '../claimPages/claim.module'; -import {ServicesModule} from '../services/services.module'; - -//import {SearchModule} from '../searchPages/search.module'; - -import {LoginComponent} from './login.component'; - - -import {LoginRoutingModule} from './login-routing.module'; - -@NgModule({ - imports: [ - //CommonModule, FormsModule, - //UtilsModule, - //ClaimModule, - ServicesModule, - LoginRoutingModule - ], - declarations: [ - LoginComponent - ], - providers:[ - ], - exports: [ - LoginComponent - ] -}) -export class LoginModule { } diff --git a/portal-2/src/app/services/login.service.ts b/portal-2/src/app/services/login.service.ts index d55c6d77..a07092bc 100644 --- a/portal-2/src/app/services/login.service.ts +++ b/portal-2/src/app/services/login.service.ts @@ -7,24 +7,36 @@ import 'rxjs/add/operator/share'; import { CacheService } from '../shared/cache.service'; import {PersonInfo} from '../utils/entities/personInfo'; -import {OpenaireProperties} from '../utils/properties/openaireProperties' +import {OpenaireProperties} from '../utils/properties/openaireProperties'; +import {User} from '../utils/entities/user'; @Injectable() export class LoginService { constructor(private http: Http, public _cache: CacheService) {} - authenticate (email: string, password: string):any { + authenticate (user: User):any { - let url = OpenaireProperties.getLoginAPIURL()+email+"&password="+password; + let url = OpenaireProperties.getLoginAPIURL()+user.username+"&password="+user.password; let key = url; if (this._cache.has(key)) { return Observable.of(this._cache.get(key)); } return this.http.get(url) .map(res => res.json()) + .map(res => this.parseUserInfo(res['data'], user)) .do(res => { this._cache.set(key, res); }); } + + private parseUserInfo(data: any, user: User): User { + user.email = data.email; + user.id = data.id; + user.fullname = data.fullname; + + localStorage.setItem("user", JSON.stringify(user)); + + return user; + } } diff --git a/portal-2/src/app/sharedComponents/login.component.ts b/portal-2/src/app/sharedComponents/login.component.ts new file mode 100644 index 00000000..1e466d35 --- /dev/null +++ b/portal-2/src/app/sharedComponents/login.component.ts @@ -0,0 +1,79 @@ +import {Component, ElementRef} from '@angular/core'; +import {Observable} from 'rxjs/Observable'; +import {LoginService} from '../services/login.service'; +import {User} from '../utils/entities/user'; + +@Component({ + selector: 'login', + template: ` +
+
+ + +
+
+ + +
+
+ +
+
+ + +
+
+ + +
+
+ Hello {{user.fullname}}! +
+
+ ` +}) + +export class LoginComponent { + public user: User; + private loggedIn: boolean; + private errorMessage: string; + + constructor( private _loginService: LoginService ) {} + + ngOnInit() { + if( typeof localStorage !== 'undefined') { + if(localStorage.getItem("user")) { + this.user = JSON.parse(localStorage.getItem("user")); + this.loggedIn = true; + } + } + + if(this.loggedIn == undefined) { + this.user = new User(); + this.loggedIn = false; + } + this.errorMessage = ""; + } + + login() { + this._loginService.authenticate(this.user).subscribe( + data => { + this.loggedIn = true; + }, + err => { + console.log(err); + if(err.status == "404") { + this.errorMessage = "Wrong username"; + } else if(err.status == "401") { + this.errorMessage = "Wrong password"; + } + + } + ); + } +} diff --git a/portal-2/src/app/sharedComponents/navigationBar.component.ts b/portal-2/src/app/sharedComponents/navigationBar.component.ts index f112bd01..11cc8b5a 100644 --- a/portal-2/src/app/sharedComponents/navigationBar.component.ts +++ b/portal-2/src/app/sharedComponents/navigationBar.component.ts @@ -25,7 +25,7 @@ import 'rxjs/Rx';