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 { 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
}],

View file

@ -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'
},

View file

@ -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]],