Auth Guard initial implementation

Commented out until routing is simplified
This commit is contained in:
piratefinn 2017-06-08 14:28:54 +01:00
parent 1bd586b3ae
commit c7810de2b6
5 changed files with 30 additions and 11 deletions

View file

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

1
src/app/_guards/index.ts Normal file
View file

@ -0,0 +1 @@
export * from './auth.guard';

View file

@ -13,8 +13,9 @@ import { SIDEBAR_TOGGLE_DIRECTIVES } from './shared/sidebar.directive';
import { AsideToggleDirective } from './shared/aside.directive'; import { AsideToggleDirective } from './shared/aside.directive';
import { BreadcrumbsComponent } from './shared/breadcrumb.component'; import { BreadcrumbsComponent } from './shared/breadcrumb.component';
// Routing Module // Routing & Guard Module
import { AppRoutingModule } from './app.routing'; import { AppRoutingModule } from './app.routing';
import { AuthGuard } from './_guards/index';
// Layouts // Layouts
import { FullLayoutComponent } from './layouts/full-layout.component'; import { FullLayoutComponent } from './layouts/full-layout.component';
@ -38,7 +39,9 @@ import { SimpleLayoutComponent } from './layouts/simple-layout.component';
SIDEBAR_TOGGLE_DIRECTIVES, SIDEBAR_TOGGLE_DIRECTIVES,
AsideToggleDirective, AsideToggleDirective,
], ],
providers: [{ providers: [
AuthGuard,
{
provide: LocationStrategy, provide: LocationStrategy,
useClass: HashLocationStrategy useClass: HashLocationStrategy
}], }],

View file

@ -1,5 +1,6 @@
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router'; import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from './_guards/index';
// Layouts // Layouts
import { FullLayoutComponent } from './layouts/full-layout.component'; import { FullLayoutComponent } from './layouts/full-layout.component';
@ -14,6 +15,7 @@ export const routes: Routes = [
{ {
path: '', path: '',
component: FullLayoutComponent, component: FullLayoutComponent,
// canActivate: [AuthGuard],
data: { data: {
title: 'Home' title: 'Home'
}, },

View file

@ -11,7 +11,6 @@ import 'rxjs/add/operator/map';
}) })
export class LoginComponent { export class LoginComponent {
signin: FormGroup; signin: FormGroup;
ageRanges: Object[];
constructor( constructor(
private http: Http, private http: Http,
@ -19,13 +18,6 @@ export class LoginComponent {
private router: Router, private router: Router,
private api: ApiService private api: ApiService
) { ) {
this.api.getAgeRanges()
.subscribe(
result => {
console.log(result);
this.ageRanges = result.ages;
}
);
this.signin = this.formBuilder.group({ this.signin = this.formBuilder.group({
email: ['', [Validators.required]], email: ['', [Validators.required]],
password: ['', [Validators.required]], password: ['', [Validators.required]],