src/app/core/helpers/auth.guard.ts
constructor(router: Router, authenticationService: ApiAuthService)
|
canActivate |
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
|
Returns:
void
|
import { Injectable } from '@angular/core';
import { Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { ApiAuthService } from '../services/auth/api.auth.service';
@Injectable({ providedIn: 'root' })
export class AuthGuard {
constructor(
private router: Router,
private authenticationService: ApiAuthService
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const user = this.authenticationService.userValue;
if (user) {
// check if route is restricted by role
if (route.data['roles'] && route.data['roles'].indexOf(user.role) === -1) {
// role not authorised so redirect to home page
this.router.navigate(['/']);
return false;
}
// authorised so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
return false;
}
}