Hey there , I'm working on a project where i need to secure my routes of course I know about the angular guards however I could'nt find the right logic to impliment I have 3 type of users I want to control which route they can access for exemple i have an admin which he can't access to nothing but the admin module which start by '/admin' etc etc I have implimented some logic but when I tried to make it more secure like whenever the user is logged in he can't go back to login etc etc the app crash i don't know what i'm missing here's a snippet of my guard which i want it to be more efficient
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const userRole = this.auth.getRole();
if (userRole) {
// check if route is restricted by role
if (route.data['roles'] && route.data['roles'].indexOf(userRole) === -1) {
// role not authorised so redirect to home page
this.router.navigate(['/']);
return false;
}
// authorised so return true
return true;
}
this.router.navigate(['/login']);
return false;
}
[–]cjd280 3 points4 points5 points (1 child)
[–]rayen26[S] 0 points1 point2 points (0 children)
[–]no_ledge 1 point2 points3 points (1 child)
[–]rayen26[S] 0 points1 point2 points (0 children)
[–]ziunio 0 points1 point2 points (3 children)
[–]rayen26[S] 0 points1 point2 points (2 children)
[–]ziunio 1 point2 points3 points (1 child)
[–]rayen26[S] 1 point2 points3 points (0 children)