-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Pure Function-Based Guards in Angular #526
Labels
enhancement
This issue/PR is an enhancement or new feature.
evaluate-in-next-version
Evaluate if the feature or change might be included in the next library release.
Comments
Hi @nephi5, |
mauriciovigolo
added
enhancement
This issue/PR is an enhancement or new feature.
evaluate-in-next-version
Evaluate if the feature or change might be included in the next library release.
labels
Nov 20, 2023
I'm using the function-based guard for a half year now, you could do it like follows: import { CanMatchFn, Router, UrlTree } from '@angular/router';
import { inject } from '@angular/core';
// Services
import { KeycloakService } from 'keycloak-angular';
export const authGuard: CanMatchFn = async (route, segments): Promise<boolean | UrlTree> => {
const router = inject(Router);
const keycloakService = inject(KeycloakService);
const authenticated: boolean = await keycloakService.isLoggedIn();
if (!authenticated) {
await keycloakService.login({
redirectUri: window.location.origin,
});
}
// Get the user Keycloak roles and the required from the route
const roles: string[] = keycloakService.getUserRoles(true);
const requiredRoles = route.data?.['roles'];
// Allow the user to proceed if no additional roles are required to access the route
if (!Array.isArray(requiredRoles) || requiredRoles.length === 0) {
return true;
}
// Allow the user to proceed if ALL of the required roles are present
const authorized = requiredRoles.every((role) => roles.includes(role));
// Allow the user to proceed if ONE of the required roles is present
//const authorized = requiredRoles.some((role) => roles.includes(role));
if (authorized) {
return true;
}
// Display my custom HTTP 403 access denied page
return router.createUrlTree(['/access']);
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
enhancement
This issue/PR is an enhancement or new feature.
evaluate-in-next-version
Evaluate if the feature or change might be included in the next library release.
Bug Report or Feature Request (mark with an
x
)Currently, 'keycloak-angular' is functioning as expected with Angular versions up to v.17. However, the reliance on Class-based Guards is a concern due to Angular's evolving standards
Versions.
Repro steps.
ng update
automatically wraps the Class based Guards intomapToCanActivate
function provided under@angular/router
.Desired functionality.
The text was updated successfully, but these errors were encountered: