Permalink
Browse files

authenticaton guard

  • Loading branch information...
adash333 committed Sep 30, 2018
1 parent 0ddb774 commit 4b275852a4821e3cf729ddad1bf723f2225bbdf6
Showing with 69 additions and 5 deletions.
  1. +26 −5 src/app/app-routing.module.ts
  2. +15 −0 src/app/services/user/auth.guard.spec.ts
  3. +28 −0 src/app/services/user/auth.guard.ts
@@ -1,14 +1,35 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from './services/user/auth.guard';
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', loadChildren: './home/home.module#HomePageModule' },
{ path: 'event-create', loadChildren: './pages/event-create/event-create.module#EventCreatePageModule' },
{ path: 'event-detail', loadChildren: './pages/event-detail/event-detail.module#EventDetailPageModule' },
{ path: 'event-list', loadChildren: './pages/event-list/event-list.module#EventListPageModule' },
{
path: 'home',
loadChildren: './home/home.module#HomePageModule',
canActivate: [AuthGuard],
},
{
path: 'event-create',
loadChildren: './pages/event-create/event-create.module#EventCreatePageModule',
canActivate: [AuthGuard],
},
{
path: 'event-detail/:id',
loadChildren: './pages/event-detail/event-detail.module#EventDetailPageModule',
canActivate: [AuthGuard],
},
{
path: 'event-list',
loadChildren: './pages/event-list/event-list.module#EventListPageModule',
canActivate: [AuthGuard],
},
{ path: 'login', loadChildren: './pages/login/login.module#LoginPageModule' },
{ path: 'profile', loadChildren: './pages/profile/profile.module#ProfilePageModule' },
{
path: 'profile',
loadChildren: './pages/profile/profile.module#ProfilePageModule',
canActivate: [AuthGuard],
},
{ path: 'reset-password', loadChildren: './pages/reset-password/reset-password.module#ResetPasswordPageModule' },
{ path: 'signup', loadChildren: './pages/signup/signup.module#SignupPageModule' },
];
@@ -0,0 +1,15 @@
import { TestBed, async, inject } from '@angular/core/testing';
import { AuthGuard } from './auth.guard';
describe('AuthGuard', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [AuthGuard]
});
});
it('should ...', inject([AuthGuard], (guard: AuthGuard) => {
expect(guard).toBeTruthy();
}));
});
@@ -0,0 +1,28 @@
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';
import * as firebase from 'firebase/app';
import 'firebase/auth';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private router: Router) {}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
return new Promise((resolve, reject) => {
firebase.auth().onAuthStateChanged((user: firebase.User) => {
if(user) {
resolve(true);
} else {
console.log('User is not logged in');
this.router.navigate(['/login']);
resolve(false);
}
});
});
}
}

0 comments on commit 4b27585

Please sign in to comment.