I need to make a role base guard.
If the user has activeType = 0 he can access certain routes. If the user has activeType = 1 he can access other routes.
I already have one guard for routes but if you are logged in or not and I need to add this one for another guard but how?
userData: User;
authData: AuthData;
constructor(private router: Router,
private userService: UserService,
private location: Location) {
}
canActivate() {
const userData = JSON.parse(localStorage.getItem("userData"));
const authData = JSON.parse(localStorage.getItem("authData"));
const route = this.location.path();
if (!userData && !authData) {
return true;
} else {
if (route == '/login' || route === '/register') {
this.location.back();
return false;
} else {
return true;
}
}
}
canLoad() {
const userData = JSON.parse(localStorage.getItem("userData"));
const authData = JSON.parse(localStorage.getItem("authData"));
if (userData && authData) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.userService.logout();
this.router.navigate(["/login"]);
return false;
}
const routes: Routes = [
{
path: "route-for-user-activeType-1",
loadChildren: () =>
import("./one/one.module").then(
(m) => m.OneModule
),
canLoad: [AuthGuard]
},
{
path: "route-for-user-activeType-0",
loadChildren: () =>
import("./two/two.module").then((m) => m.twoModule),
canActivate: [AuthGuard]
},
{
path: "route-path-for-user-activeType-1",
loadChildren: () =>
import("./test/test.module").then((m) => m.testModule),
canActivate: [AuthGuard]
}
]