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; + } + +}