Implement session service

This commit is contained in:
Steven Zou 2017-02-16 18:13:34 +08:00
parent cbb3838e61
commit b4e9f7c7fd
9 changed files with 145 additions and 57 deletions

View File

@ -1,14 +1,15 @@
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { SignInComponent } from './sign-in/sign-in.component';
import { SharedModule } from '../shared/shared.module'; 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({ @NgModule({
imports: [ imports: [
SharedModule, SharedModule,
RouterModule RouterModule
], ],
declarations: [ SignInComponent ], declarations: [SignInComponent],
exports: [SignInComponent] exports: [SignInComponent]
}) })
export class AccountModule {} export class AccountModule { }

View File

@ -5,6 +5,7 @@ import { NgForm } from '@angular/forms';
import { SignInService } from './sign-in.service'; import { SignInService } from './sign-in.service';
import { SignInCredential } from './sign-in-credential' import { SignInCredential } from './sign-in-credential'
import { SessionService } from '../../shared/session.service';
//Define status flags for signing in states //Define status flags for signing in states
export const signInStatusNormal = 0; export const signInStatusNormal = 0;
@ -35,7 +36,8 @@ export class SignInComponent implements AfterViewChecked {
constructor( constructor(
private signInService: SignInService, private signInService: SignInService,
private router: Router private router: Router,
private session: SessionService
) { } ) { }
//For template accessing //For template accessing
@ -53,6 +55,15 @@ export class SignInComponent implements AfterViewChecked {
//return this.signInForm.valid; //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 //Hande form values changes
private formChanged() { private formChanged() {
if (this.currentForm === this.signInForm) { if (this.currentForm === this.signInForm) {
@ -100,17 +111,16 @@ export class SignInComponent implements AfterViewChecked {
//Set status //Set status
this.signInStatus = signInStatusNormal; this.signInStatus = signInStatusNormal;
//Routing to the right location //Validate the sign-in session
let nextRoute = ["/harbor", "dashboard"]; this.session.retrieveUser()
this.router.navigate(nextRoute); .then(() => {
//Routing to the right location
let nextRoute = ["/harbor", "dashboard"];
this.router.navigate(nextRoute);
})
.catch(this.handleError);
}) })
.catch(error => { .catch(this.handleError);
//Set error status
this.signInStatus = signInStatusError;
let message = error.status ? error.status + ":" + error.statusText : error;
console.error("An error occurred when signing in:", message);
});
} }
//Help user navigate to the sign up //Help user navigate to the sign up

View File

@ -0,0 +1,12 @@
<clr-dropdown [clrMenuPosition]="'bottom-right'">
<button class="nav-text" clrDropdownToggle>
<!--<clr-icon shape="user" class="is-inverse" size="24"></clr-icon>-->
<span>Administrator</span>
<clr-icon shape="caret down"></clr-icon>
</button>
<div class="dropdown-menu">
<a href="javascript:void(0)" clrDropdownItem>Add User</a>
<a href="javascript:void(0)" clrDropdownItem>Account Setting</a>
<a href="javascript:void(0)" clrDropdownItem>About</a>
</div>
</clr-dropdown>

View File

@ -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 {
}
}

View File

@ -9,6 +9,7 @@ import { NavigatorComponent } from './navigator/navigator.component';
import { GlobalSearchComponent } from './global-search/global-search.component'; import { GlobalSearchComponent } from './global-search/global-search.component';
import { FooterComponent } from './footer/footer.component'; import { FooterComponent } from './footer/footer.component';
import { HarborShellComponent } from './harbor-shell/harbor-shell.component'; import { HarborShellComponent } from './harbor-shell/harbor-shell.component';
import { BaseSettingsComponent } from './base-settings/base-settings.component';
import { BaseRoutingModule } from './base-routing.module'; import { BaseRoutingModule } from './base-routing.module';
@ -23,6 +24,7 @@ import { BaseRoutingModule } from './base-routing.module';
declarations: [ declarations: [
NavigatorComponent, NavigatorComponent,
GlobalSearchComponent, GlobalSearchComponent,
BaseSettingsComponent,
FooterComponent, FooterComponent,
HarborShellComponent HarborShellComponent
], ],

View File

@ -1,10 +1,10 @@
import { Component } from '@angular/core'; import { Component} from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
@Component({ @Component({
selector: 'global-search', selector: 'global-search',
templateUrl: "global-search.component.html" templateUrl: "global-search.component.html"
}) })
export class GlobalSearchComponent { export class GlobalSearchComponent{
// constructor(private router: Router){} // constructor(private router: Router){}
} }

View File

@ -1,40 +1,41 @@
<clr-header class="header-4 header"> <clr-header class="header-4 header">
<div class="branding"> <div class="branding">
<a href="#" class="nav-link"> <a href="#" class="nav-link">
<clr-icon shape="vm-bug"></clr-icon> <clr-icon shape="vm-bug"></clr-icon>
<span class="title">Harbor</span> <span class="title">Harbor</span>
</a> </a>
</div> </div>
<div class="divider"></div> <div class="divider"></div>
<div class="header-nav"> <div class="header-nav">
<a class="nav-link" id="dashboard-link" [routerLink]="['/harbor', 'dashboard']" routerLinkActive="active"> <a class="nav-link" id="dashboard-link" [routerLink]="['/harbor', 'dashboard']" routerLinkActive="active">
<span class="nav-text">Dashboard</span> <span class="nav-text">Dashboard</span>
</a> </a>
<a class="nav-link" id="dashboard-link" [routerLink]="['/harbor', 'projects']" routerLinkActive="active"> <a class="nav-link" id="dashboard-link" [routerLink]="['/harbor', 'projects']" routerLinkActive="active">
<span class="nav-text">Project</span> <span class="nav-text">Project</span>
</a> </a>
</div> </div>
<div class="header-actions"> <div class="header-actions">
<clr-dropdown class="dropdown bottom-right"> <clr-dropdown [clrMenuPosition]="'bottom-right'">
<button class="nav-text" clrDropdownToggle> <!--<clr-icon shape="user" class="is-inverse" size="24"></clr-icon>-->
Administrator <button class="nav-text" clrDropdownToggle>
<clr-icon shape="caret down"></clr-icon> <span>Administrator</span>
</button> <clr-icon shape="caret down"></clr-icon>
<div class="dropdown-menu"> </button>
<a href="javascript:void(0)" clrDropdownItem>Add User</a> <div class="dropdown-menu">
<a href="javascript:void(0)" clrDropdownItem (click)="account_settings_opened = true">Account Setting</a> <a href="javascript:void(0)" clrDropdownItem>Add User</a>
<a href="javascript:void(0)" clrDropdownItem>About</a> <a href="javascript:void(0)" clrDropdownItem>Account Setting</a>
</div> <a href="javascript:void(0)" clrDropdownItem>About</a>
</clr-dropdown> </div>
<global-search></global-search> </clr-dropdown>
<clr-dropdown class="dropdown bottom-right"> <global-search></global-search>
<button class="nav-text" clrDropdownToggle> <clr-dropdown class="dropdown bottom-right">
<clr-icon shape="cog"></clr-icon> <button class="nav-text" clrDropdownToggle>
</button> <clr-icon shape="cog"></clr-icon>
<div class="dropdown-menu"> </button>
<a href="javascript:void(0)" clrDropdownItem>Preferences</a> <div class="dropdown-menu">
<a href="javascript:void(0)" clrDropdownItem>Log out</a> <a href="javascript:void(0)" clrDropdownItem>Preferences</a>
</div> <a href="javascript:void(0)" clrDropdownItem>Log out</a>
</clr-dropdown> </div>
</div> </clr-dropdown>
</div>
</clr-header> </clr-header>

View File

@ -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<any>}
*
* @memberOf SessionService
*/
retrieveUser(): Promise<any> {
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;
}
}

View File

@ -1,13 +1,16 @@
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { CoreModule } from '../core/core.module'; import { CoreModule } from '../core/core.module';
import { SessionService } from '../shared/session.service';
@NgModule({ @NgModule({
imports: [ imports: [
CoreModule CoreModule
], ],
exports: [ exports: [
CoreModule CoreModule
] ],
providers: [SessionService]
}) })
export class SharedModule { export class SharedModule {