From b4e9f7c7fd929facf5d6abe96c4c1cb28eb2549c Mon Sep 17 00:00:00 2001 From: Steven Zou Date: Thu, 16 Feb 2017 18:13:34 +0800 Subject: [PATCH] Implement session service --- harbor-app/src/app/account/account.module.ts | 11 +-- .../app/account/sign-in/sign-in.component.ts | 32 +++++--- .../base-settings.component.html | 12 +++ .../base-settings/base-settings.component.ts | 16 ++++ harbor-app/src/app/base/base.module.ts | 2 + .../global-search/global-search.component.ts | 4 +- .../base/navigator/navigator.component.html | 77 ++++++++++--------- harbor-app/src/app/shared/session.service.ts | 43 +++++++++++ harbor-app/src/app/shared/shared.module.ts | 5 +- 9 files changed, 145 insertions(+), 57 deletions(-) create mode 100644 harbor-app/src/app/base/base-settings/base-settings.component.html create mode 100644 harbor-app/src/app/base/base-settings/base-settings.component.ts create mode 100644 harbor-app/src/app/shared/session.service.ts diff --git a/harbor-app/src/app/account/account.module.ts b/harbor-app/src/app/account/account.module.ts index 9aa924a84..7ace60143 100644 --- a/harbor-app/src/app/account/account.module.ts +++ b/harbor-app/src/app/account/account.module.ts @@ -1,14 +1,15 @@ import { NgModule } from '@angular/core'; -import { SignInComponent } from './sign-in/sign-in.component'; import { SharedModule } from '../shared/shared.module'; -import { RouterModule } from '@angular/router'; +import { RouterModule } from '@angular/router'; + +import { SignInComponent } from './sign-in/sign-in.component'; @NgModule({ - imports: [ + imports: [ SharedModule, RouterModule ], - declarations: [ SignInComponent ], + declarations: [SignInComponent], exports: [SignInComponent] }) -export class AccountModule {} \ No newline at end of file +export class AccountModule { } \ No newline at end of file diff --git a/harbor-app/src/app/account/sign-in/sign-in.component.ts b/harbor-app/src/app/account/sign-in/sign-in.component.ts index 1b8a8617d..8c33029d4 100644 --- a/harbor-app/src/app/account/sign-in/sign-in.component.ts +++ b/harbor-app/src/app/account/sign-in/sign-in.component.ts @@ -5,6 +5,7 @@ import { NgForm } from '@angular/forms'; import { SignInService } from './sign-in.service'; import { SignInCredential } from './sign-in-credential' +import { SessionService } from '../../shared/session.service'; //Define status flags for signing in states export const signInStatusNormal = 0; @@ -35,7 +36,8 @@ export class SignInComponent implements AfterViewChecked { constructor( private signInService: SignInService, - private router: Router + private router: Router, + private session: SessionService ) { } //For template accessing @@ -53,6 +55,15 @@ export class SignInComponent implements AfterViewChecked { //return this.signInForm.valid; } + //General error handler + private handleError(error) { + //Set error status + this.signInStatus = signInStatusError; + + let message = error.status ? error.status + ":" + error.statusText : error; + console.error("An error occurred when signing in:", message); + } + //Hande form values changes private formChanged() { if (this.currentForm === this.signInForm) { @@ -100,17 +111,16 @@ export class SignInComponent implements AfterViewChecked { //Set status this.signInStatus = signInStatusNormal; - //Routing to the right location - let nextRoute = ["/harbor", "dashboard"]; - this.router.navigate(nextRoute); + //Validate the sign-in session + this.session.retrieveUser() + .then(() => { + //Routing to the right location + let nextRoute = ["/harbor", "dashboard"]; + this.router.navigate(nextRoute); + }) + .catch(this.handleError); }) - .catch(error => { - //Set error status - this.signInStatus = signInStatusError; - - let message = error.status ? error.status + ":" + error.statusText : error; - console.error("An error occurred when signing in:", message); - }); + .catch(this.handleError); } //Help user navigate to the sign up diff --git a/harbor-app/src/app/base/base-settings/base-settings.component.html b/harbor-app/src/app/base/base-settings/base-settings.component.html new file mode 100644 index 000000000..6a87002b9 --- /dev/null +++ b/harbor-app/src/app/base/base-settings/base-settings.component.html @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/harbor-app/src/app/base/base-settings/base-settings.component.ts b/harbor-app/src/app/base/base-settings/base-settings.component.ts new file mode 100644 index 000000000..99a8abdf2 --- /dev/null +++ b/harbor-app/src/app/base/base-settings/base-settings.component.ts @@ -0,0 +1,16 @@ +import { Component, OnInit } from '@angular/core'; + +@Component({ + selector: "base-settings", + templateUrl: "base-settings.component.html" +}) + +/** + * Component to handle the account settings + */ +export class BaseSettingsComponent implements OnInit { + + ngOnInit(): void { + } + +} diff --git a/harbor-app/src/app/base/base.module.ts b/harbor-app/src/app/base/base.module.ts index 6b7e74e2b..2b4242193 100644 --- a/harbor-app/src/app/base/base.module.ts +++ b/harbor-app/src/app/base/base.module.ts @@ -9,6 +9,7 @@ import { NavigatorComponent } from './navigator/navigator.component'; import { GlobalSearchComponent } from './global-search/global-search.component'; import { FooterComponent } from './footer/footer.component'; import { HarborShellComponent } from './harbor-shell/harbor-shell.component'; +import { BaseSettingsComponent } from './base-settings/base-settings.component'; import { BaseRoutingModule } from './base-routing.module'; @@ -23,6 +24,7 @@ import { BaseRoutingModule } from './base-routing.module'; declarations: [ NavigatorComponent, GlobalSearchComponent, + BaseSettingsComponent, FooterComponent, HarborShellComponent ], diff --git a/harbor-app/src/app/base/global-search/global-search.component.ts b/harbor-app/src/app/base/global-search/global-search.component.ts index 135b65fca..3441a8116 100644 --- a/harbor-app/src/app/base/global-search/global-search.component.ts +++ b/harbor-app/src/app/base/global-search/global-search.component.ts @@ -1,10 +1,10 @@ -import { Component } from '@angular/core'; +import { Component} from '@angular/core'; import { Router } from '@angular/router'; @Component({ selector: 'global-search', templateUrl: "global-search.component.html" }) -export class GlobalSearchComponent { +export class GlobalSearchComponent{ // constructor(private router: Router){} } \ No newline at end of file diff --git a/harbor-app/src/app/base/navigator/navigator.component.html b/harbor-app/src/app/base/navigator/navigator.component.html index ecc514510..c13091948 100644 --- a/harbor-app/src/app/base/navigator/navigator.component.html +++ b/harbor-app/src/app/base/navigator/navigator.component.html @@ -1,40 +1,41 @@ - -
- -
- - - - - - - - - -
+ +
+ +
+ + + + + + + + + + +
\ No newline at end of file diff --git a/harbor-app/src/app/shared/session.service.ts b/harbor-app/src/app/shared/session.service.ts new file mode 100644 index 000000000..3f4c322fe --- /dev/null +++ b/harbor-app/src/app/shared/session.service.ts @@ -0,0 +1,43 @@ +import { Injectable } from '@angular/core'; +import { Headers, Http } from '@angular/http'; +import 'rxjs/add/operator/toPromise'; + +const currentUserEndpint = "/api/users/current"; +/** + * Define related methods to handle account and session corresponding things + * + * @export + * @class SessionService + */ +@Injectable() +export class SessionService { + currentUser: any = null; + + private headers = new Headers({ + "Content-Type": 'application/json' + }); + + constructor(private http: Http) {} + + /** + * Get the related information of current signed in user from backend + * + * @returns {Promise} + * + * @memberOf SessionService + */ + retrieveUser(): Promise { + return this.http.get(currentUserEndpint, { headers: this.headers }).toPromise() + .then(response => this.currentUser = response.json()) + .catch(error => { + console.log("An error occurred when getting current user ", error);//TODO: Will replaced with general error handler + }) + } + + /** + * For getting info + */ + getCurrentUser(): any { + return this.currentUser; + } +} \ No newline at end of file diff --git a/harbor-app/src/app/shared/shared.module.ts b/harbor-app/src/app/shared/shared.module.ts index 35df2530f..7a370cae2 100644 --- a/harbor-app/src/app/shared/shared.module.ts +++ b/harbor-app/src/app/shared/shared.module.ts @@ -1,13 +1,16 @@ import { NgModule } from '@angular/core'; import { CoreModule } from '../core/core.module'; +import { SessionService } from '../shared/session.service'; + @NgModule({ imports: [ CoreModule ], exports: [ CoreModule - ] + ], + providers: [SessionService] }) export class SharedModule {