From eba5475f03377b89b355ffd9211f6f08e1687187 Mon Sep 17 00:00:00 2001 From: salah3x Date: Wed, 17 Jul 2019 17:30:54 +0100 Subject: [PATCH] Add auth guard Closes #7 --- src/app/app.module.ts | 2 ++ src/app/auth/auth.page.html | 6 +++++- src/app/shared/auth.guard.ts | 27 +++++++++++++++++++++++++++ src/app/tabs/tabs-routing.module.ts | 4 +++- 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/app/shared/auth.guard.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 285ce1d..45caadc 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -3,6 +3,7 @@ import { BrowserModule } from '@angular/platform-browser'; import { RouteReuseStrategy } from '@angular/router'; import { IonicModule, IonicRouteStrategy } from '@ionic/angular'; import { AngularFireModule } from '@angular/fire'; +import { AngularFireAuthModule } from '@angular/fire/auth'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; @@ -15,6 +16,7 @@ import { environment } from '../environments/environment'; BrowserModule, AngularFireModule.initializeApp(environment.firebaseConfig), IonicModule.forRoot(), + AngularFireAuthModule, AppRoutingModule ], providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }], diff --git a/src/app/auth/auth.page.html b/src/app/auth/auth.page.html index 1a014d4..bd3b4d2 100644 --- a/src/app/auth/auth.page.html +++ b/src/app/auth/auth.page.html @@ -4,4 +4,8 @@ - + + + Close + + diff --git a/src/app/shared/auth.guard.ts b/src/app/shared/auth.guard.ts new file mode 100644 index 0000000..6c6fa1d --- /dev/null +++ b/src/app/shared/auth.guard.ts @@ -0,0 +1,27 @@ +import { Injectable } from '@angular/core'; +import { CanLoad, Route, UrlSegment, Router } from '@angular/router'; +import { AngularFireAuth } from '@angular/fire/auth'; +import { Observable } from 'rxjs'; +import { take, map, tap } from 'rxjs/operators'; + +@Injectable({ + providedIn: 'root' +}) +export class AuthGuard implements CanLoad { + constructor(private authService: AngularFireAuth, private router: Router) {} + + canLoad( + route: Route, + segments: UrlSegment[] + ): boolean | Observable | Promise { + return this.authService.authState.pipe( + take(1), + map(user => !!user), + tap(isAuhtenticated => { + if (!isAuhtenticated) { + this.router.navigate(['/auth']); + } + }) + ); + } +} diff --git a/src/app/tabs/tabs-routing.module.ts b/src/app/tabs/tabs-routing.module.ts index ece4b48..03d6fc9 100644 --- a/src/app/tabs/tabs-routing.module.ts +++ b/src/app/tabs/tabs-routing.module.ts @@ -2,6 +2,7 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { TabsPage } from './tabs.page'; +import { AuthGuard } from '../shared/auth.guard'; const routes: Routes = [ { @@ -31,7 +32,8 @@ const routes: Routes = [ children: [ { path: '', - loadChildren: '../settings/settings.module#SettingsPageModule' + loadChildren: '../settings/settings.module#SettingsPageModule', + canLoad: [AuthGuard] } ] },