From 512bb78b65bceabee93ee3a7e0b9ac3dfa894966 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 1 Apr 2022 19:51:11 +0530 Subject: [PATCH 1/4] [issue7]: CanLoad guard implemented --- src/app/auth/auth.guard.spec.ts | 16 ++++++++++++++++ src/app/auth/auth.guard.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/app/auth/auth.guard.spec.ts create mode 100644 src/app/auth/auth.guard.ts diff --git a/src/app/auth/auth.guard.spec.ts b/src/app/auth/auth.guard.spec.ts new file mode 100644 index 0000000..68889d2 --- /dev/null +++ b/src/app/auth/auth.guard.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthGuard } from './auth.guard'; + +describe('AuthGuard', () => { + let guard: AuthGuard; + + beforeEach(() => { + TestBed.configureTestingModule({}); + guard = TestBed.inject(AuthGuard); + }); + + it('should be created', () => { + expect(guard).toBeTruthy(); + }); +}); diff --git a/src/app/auth/auth.guard.ts b/src/app/auth/auth.guard.ts new file mode 100644 index 0000000..9909308 --- /dev/null +++ b/src/app/auth/auth.guard.ts @@ -0,0 +1,27 @@ +import { Injectable } from '@angular/core'; +import { CanLoad, Router, UrlTree } from '@angular/router'; +import { Observable } from 'rxjs'; +import { map, tap } from 'rxjs/operators'; +import { AuthService } from './auth.service'; + +@Injectable({ + providedIn: 'root', +}) +export class AuthGuard implements CanLoad { + constructor( + private readonly authService: AuthService, + private readonly router: Router + ) {} + canLoad(): + | Observable + | Promise + | boolean + | UrlTree { + return this.authService.isLoggedIn$.pipe( + tap(console.log), + tap((isLoggedIn) => + isLoggedIn ? true : this.router.navigate(['non-auth']) + ) + ); + } +} From 92fafebfe5be203155d695b3355d4c43787f80f2 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 1 Apr 2022 20:03:45 +0530 Subject: [PATCH 2/4] Implement CanLoad #7 --- src/app/app-routing.module.ts | 7 ++++++ src/app/app.component.html | 2 +- src/app/home/home.component.ts | 1 + .../users-container.component.spec.ts | 25 +++++++++++++++++++ .../users-container.component.ts | 8 ++++++ src/app/users/users-routing.module.ts | 12 +++++++++ src/app/users/users.module.ts | 11 ++++++++ 7 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/app/users/users-container/users-container.component.spec.ts create mode 100644 src/app/users/users-container/users-container.component.ts create mode 100644 src/app/users/users-routing.module.ts create mode 100644 src/app/users/users.module.ts diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 1e0d9ec..9850b10 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -1,11 +1,18 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; +import { AuthGuard } from './auth/auth.guard'; import { HomeComponent } from './home/home.component'; import { NonAuthenticatedComponent } from './non-authenticated/non-authenticated.component'; const routes: Routes = [ { path: '', pathMatch: 'full', redirectTo: 'home' }, { path: 'home', component: HomeComponent }, + { + path: 'users', + canLoad: [AuthGuard], + loadChildren: () => + import('./users/users.module').then((m) => m.UsersModule), + }, { path: 'non-auth', component: NonAuthenticatedComponent }, ]; diff --git a/src/app/app.component.html b/src/app/app.component.html index bf4278e..74577e1 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -2,7 +2,7 @@ {{ title }} - +