keycloak is not initialized in hooks
See original GitHub issueDescribe the bug
In createReactKeycloakContext initialized is set to false and looks like it never becomes true.
When calling keycloak.login() I am getting that adapter property inside keycloak.js is undefined, that seems to be possible if keycloak wasn’t initialized.
I am not sure if it’s related to hooks initialized property at all though.
Use-case scenario:
- I am writing a PrivateRoute for Gatsby.js and am using
useKeycloaksimilar to how it’s shown in react-router examples. When being redirected back from keycloak authentication to my page I getCannot read property 'login' of undefinedpointing thatadapteris undefined and keycloak isn’t properly inited
KeycloakProvider is present in the hierarchy and some kind of keycloak is provided by useKeycloak with my realm settings. It’s just doesn’t seem to be properly initialized
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:13
Top Results From Across the Web
useKeycloak hooks always returns keycloakStubInstance in ...
createLoginUrl always returns an empty string. I suspect that the problem is that the variable called initialized is always false, probably ...
Read more >Tryning to secure a front End react page with keycloak server ...
I've modified my component so it would mount when page is routed. but at the end i get this error: keycloak.init(...).then is not...
Read more >How to implement Keycloak authentication in React
Jumpstart your Keycloak authentication, from login/logout to protected routes, with this detailed implementation guide for React apps.
Read more >Server Administration Guide - Keycloak
Keycloak is a single sign on solution for web apps and RESTful web services. The goal of Keycloak is to make security simple...
Read more >Implementing Secure Authentication with Keycloak - Section.io
Using the useState hook: export default function Resources(){ const [keycloak, setKeycloak] = useState(null) const [authenticated, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@panz3r Hello, can you please check on this. We’re also facing a similar issue and not able to identify a solution. We’re getting keycloak is not initialized in the hooks on refreshing the page
I didn’t realize when I first commented on this issue that you were using Gatsby. I expect the issue is that your code is running during the server-side rendering phase and not in the browser.
I have not used it myself, but I think for Gatsby you’d need to follow or borrow from the NextJS example provided in this repository. The essential thing is to wrap Keycloak initialization with a check for whether it’s running in the browser. The recommended way to do that seems to be
if(typeof window !== 'undefined')