keycloak is not initialized in hooks

See original GitHub issue

Describe 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 useKeycloak similar to how it’s shown in react-router examples. When being redirected back from keycloak authentication to my page I get Cannot read property 'login' of undefined pointing that adapter is 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:13

github_iconTop GitHub Comments

1reaction
punithbmcommented, Jul 9, 2020

@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

0reactions
aelfriccommented, Jul 9, 2020

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')

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found