From 2f8806b8ce52b711729f234c43f51082c8ae03ed Mon Sep 17 00:00:00 2001 From: Spencer Ofwiti Date: Mon, 28 Dec 2020 12:05:33 +0300 Subject: [PATCH] Add authentication and role access guards. --- src/app/_guards/auth.guard.spec.ts | 16 ++++++++++++++++ src/app/_guards/auth.guard.ts | 23 +++++++++++++++++++++++ src/app/_guards/index.ts | 2 ++ src/app/_guards/role.guard.spec.ts | 16 ++++++++++++++++ src/app/_guards/role.guard.ts | 28 ++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 src/app/_guards/auth.guard.spec.ts create mode 100644 src/app/_guards/auth.guard.ts create mode 100644 src/app/_guards/index.ts create mode 100644 src/app/_guards/role.guard.spec.ts create mode 100644 src/app/_guards/role.guard.ts diff --git a/src/app/_guards/auth.guard.spec.ts b/src/app/_guards/auth.guard.spec.ts new file mode 100644 index 0000000..68889d2 --- /dev/null +++ b/src/app/_guards/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/_guards/auth.guard.ts b/src/app/_guards/auth.guard.ts new file mode 100644 index 0000000..3bc8723 --- /dev/null +++ b/src/app/_guards/auth.guard.ts @@ -0,0 +1,23 @@ +import { Injectable } from '@angular/core'; +import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router} from '@angular/router'; +import { Observable } from 'rxjs'; + +@Injectable({ + providedIn: 'root' +}) +export class AuthGuard implements CanActivate { + + constructor(private router: Router) {} + + canActivate( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { + if (localStorage.getItem(atob('CICADA_USER'))) { + return true; + } + + this.router.navigate(['/auth'], { queryParams: { returnUrl: state.url }}); + return false; + } + +} diff --git a/src/app/_guards/index.ts b/src/app/_guards/index.ts new file mode 100644 index 0000000..f033f4e --- /dev/null +++ b/src/app/_guards/index.ts @@ -0,0 +1,2 @@ +export * from './auth.guard'; +export * from './role.guard'; diff --git a/src/app/_guards/role.guard.spec.ts b/src/app/_guards/role.guard.spec.ts new file mode 100644 index 0000000..5b6db9f --- /dev/null +++ b/src/app/_guards/role.guard.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { RoleGuard } from './role.guard'; + +describe('RoleGuard', () => { + let guard: RoleGuard; + + beforeEach(() => { + TestBed.configureTestingModule({}); + guard = TestBed.inject(RoleGuard); + }); + + it('should be created', () => { + expect(guard).toBeTruthy(); + }); +}); diff --git a/src/app/_guards/role.guard.ts b/src/app/_guards/role.guard.ts new file mode 100644 index 0000000..ae40c75 --- /dev/null +++ b/src/app/_guards/role.guard.ts @@ -0,0 +1,28 @@ +import { Injectable } from '@angular/core'; +import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router} from '@angular/router'; +import { Observable } from 'rxjs'; + +@Injectable({ + providedIn: 'root' +}) +export class RoleGuard implements CanActivate { + + constructor(private router: Router) {} + + canActivate( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { + const currentUser = JSON.parse(localStorage.getItem(atob('CICADA_USER'))); + if (currentUser) { + if (route.data.roles && route.data.roles.indexOf(currentUser.role) === -1) { + this.router.navigate(['/']); + return false; + } + return true; + } + + this.router.navigate(['/auth'], { queryParams: { returnUrl: state.url }}); + return false; + } + +}