From c7810de2b635b09d5bf2b317ee48d2fb9e1f372b Mon Sep 17 00:00:00 2001 From: piratefinn Date: Thu, 8 Jun 2017 14:28:54 +0100 Subject: [PATCH] Auth Guard initial implementation Commented out until routing is simplified --- src/app/_guards/auth.guard.ts | 21 +++++++++++++++++++++ src/app/_guards/index.ts | 1 + src/app/app.module.ts | 7 +++++-- src/app/app.routing.ts | 2 ++ src/app/pages/login.component.ts | 10 +--------- 5 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 src/app/_guards/auth.guard.ts create mode 100644 src/app/_guards/index.ts diff --git a/src/app/_guards/auth.guard.ts b/src/app/_guards/auth.guard.ts new file mode 100644 index 0000000..01d891f --- /dev/null +++ b/src/app/_guards/auth.guard.ts @@ -0,0 +1,21 @@ +import { Injectable } from '@angular/core'; +import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; + +@Injectable() +export class AuthGuard implements CanActivate { + + constructor(private router: Router) { } + + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { + if (localStorage.getItem('sessionKey')) { + console.log('session key found') + // logged in so return true + return true; + } + + // not logged in so redirect to login page with the return url + console.log('no session key found') + this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }}); + return false; + } +} \ No newline at end of file diff --git a/src/app/_guards/index.ts b/src/app/_guards/index.ts new file mode 100644 index 0000000..3e48800 --- /dev/null +++ b/src/app/_guards/index.ts @@ -0,0 +1 @@ +export * from './auth.guard'; \ No newline at end of file diff --git a/src/app/app.module.ts b/src/app/app.module.ts index c1cca0c..0918901 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -13,8 +13,9 @@ import { SIDEBAR_TOGGLE_DIRECTIVES } from './shared/sidebar.directive'; import { AsideToggleDirective } from './shared/aside.directive'; import { BreadcrumbsComponent } from './shared/breadcrumb.component'; -// Routing Module +// Routing & Guard Module import { AppRoutingModule } from './app.routing'; +import { AuthGuard } from './_guards/index'; // Layouts import { FullLayoutComponent } from './layouts/full-layout.component'; @@ -38,7 +39,9 @@ import { SimpleLayoutComponent } from './layouts/simple-layout.component'; SIDEBAR_TOGGLE_DIRECTIVES, AsideToggleDirective, ], - providers: [{ + providers: [ + AuthGuard, + { provide: LocationStrategy, useClass: HashLocationStrategy }], diff --git a/src/app/app.routing.ts b/src/app/app.routing.ts index 2c898a0..3a74dd8 100644 --- a/src/app/app.routing.ts +++ b/src/app/app.routing.ts @@ -1,5 +1,6 @@ import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; +import { AuthGuard } from './_guards/index'; // Layouts import { FullLayoutComponent } from './layouts/full-layout.component'; @@ -14,6 +15,7 @@ export const routes: Routes = [ { path: '', component: FullLayoutComponent, + // canActivate: [AuthGuard], data: { title: 'Home' }, diff --git a/src/app/pages/login.component.ts b/src/app/pages/login.component.ts index 054c06c..2fde798 100644 --- a/src/app/pages/login.component.ts +++ b/src/app/pages/login.component.ts @@ -11,21 +11,13 @@ import 'rxjs/add/operator/map'; }) export class LoginComponent { signin: FormGroup; - ageRanges: Object[]; constructor( private http: Http, private formBuilder: FormBuilder, private router: Router, private api: ApiService - ) { - this.api.getAgeRanges() - .subscribe( - result => { - console.log(result); - this.ageRanges = result.ages; - } - ); + ) { this.signin = this.formBuilder.group({ email: ['', [Validators.required]], password: ['', [Validators.required]],