Add authentication and role access guards.
This commit is contained in:
parent
02bed9d124
commit
2f8806b8ce
16
src/app/_guards/auth.guard.spec.ts
Normal file
16
src/app/_guards/auth.guard.spec.ts
Normal file
@ -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();
|
||||
});
|
||||
});
|
23
src/app/_guards/auth.guard.ts
Normal file
23
src/app/_guards/auth.guard.ts
Normal file
@ -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<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
||||
if (localStorage.getItem(atob('CICADA_USER'))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
this.router.navigate(['/auth'], { queryParams: { returnUrl: state.url }});
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
2
src/app/_guards/index.ts
Normal file
2
src/app/_guards/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './auth.guard';
|
||||
export * from './role.guard';
|
16
src/app/_guards/role.guard.spec.ts
Normal file
16
src/app/_guards/role.guard.spec.ts
Normal file
@ -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();
|
||||
});
|
||||
});
|
28
src/app/_guards/role.guard.ts
Normal file
28
src/app/_guards/role.guard.ts
Normal file
@ -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<boolean | UrlTree> | Promise<boolean | UrlTree> | 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user