-
Notifications
You must be signed in to change notification settings - Fork 280
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
Server-side rendering (SSR) crash with 'Document not found' error in Keycloak-angular with Angular 17 #539
Comments
Hey, @sanjay-noknok |
@OZIOisgood Thank you for your response. keycloak.config.ts i have put this file in src/environments folder
app.config.ts file
assets/silent-check-sso.html
keycloak-initializer.ts
auth.service.ts
auth.guard.ts
|
@sanjay-noknok Maybe it is a problem, why you are facing some issues. Try to use function-based AuthGuard: 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']);
}; And Bearer Interceptor should be provided as: export const appConfig: ApplicationConfig = {
providers: [
KeycloakService,
{
provide: APP_INITIALIZER,
useFactory: initializeKeycloak,
multi: true,
deps: [KeycloakService]
},
{
provide: HTTP_INTERCEPTORS,
useClass: KeycloakBearerInterceptor,
multi: true
},
provideRouter(routes),
provideHttpClient(withInterceptorsFromDi())
]
};
|
@OZIOisgood Thank you for your response. I appreciate that. Thank you. |
having the same problem with SSR and this config: function initializeKeycloak(keycloak: KeycloakService) {
return () =>
keycloak.init({
config: {
url: 'http://localhost:8080',
realm: 'bizmatch',
clientId: 'bizmatch-angular-client'
},
initOptions: {
onLoad: 'check-sso',
silentCheckSsoRedirectUri: (<any>window).location.origin + '/assets/silent-check-sso.html'
},
bearerExcludedUrls: ['/assets'],
shouldUpdateToken(request) {
return request.headers.get('token-update') !== 'false';
}
});
} getting an error: "window is not defined" instead of "document is not defined" .. |
same issue here withAngular 17 SSR enabled. |
Just found a workaround (StackOverflow)
and this is the initial function
|
Hi again, actually I have an issue with this method. Need to call keycloakService.getToken() in server side which doesn't work as we don't load keycloak in server side. Is there any example of working keycloak in SSR mode? need to get token on server side. |
Please, follow up. ERROR ReferenceError: document is not defined
|
Hey there. The problem is overall that SSR doesn't know about the window/document property which is used by keycloak-js. Therefor keycloak-js itself and not only keycloak-angular would need to be Angular based and support the document injection. The only way to handle that is by using libs that mock the window object and every function that keycloak-js might use from that. |
Issue Description:
When enabling server-side rendering (SSR) in a Keycloak-angular project with Angular 17, the application crashes with a "Document not found" error. Disabling SSR resolves the issue.
Steps to Reproduce:
Expected Behavior:
The application should run successfully with SSR enabled, displaying the expected content.
Actual Behavior:
The application crashes with a "Document not found" error when SSR is enabled.
Additional Context:
The text was updated successfully, but these errors were encountered: