I was working with integrating Keycloak with an Ionic application, https://github.com/damienbod/angular-auth-oidc-client
All was working fine, but after being authenticated with keycloak i have to register that user back to the backend, so what i did i added a page callback and call checkAuth
there to register user to backend and also there was a previous call to the same funcion in main app component, and whenever i was trying to test the flow i was getting 400 Bad Request, code is not valid
, from keycloak.
Reaching to Google is the only way i could think of, but there was no solution which was solving the problem, i am putting some of the solution i found on Google
- Time of server is not matching with Keycloak server (which was not in my case, since both are running on same machine)
- cache isn't being updated (but i was not using any cache in between)
- Client roles aren't assigned (Yes it was not assigned but after assigning them still no luck)
- scope which i was trying to access isn't allowed for the endpoint i was trying to access (but i didn't mentioned any scope explicitly, so it might be getting correct scope from keycloak entity)
When i was looking at keycloak server log i could only find is the code is being used by so and so client already. But i was thinking i am running only one client where this is coming from it didn't struck to my mind about that app_component is also calling same function.
https://stackoverflow.com/a/67446927 but when i was reading this comment, then it strike to my mind removing that from callback and only keeping in app component and use isAuthenticated
function in call back will do the stuff for me.
And Voila!😌 it works
Learning
- Knowing your tool is necessary, to correctly operate it.
- Its not always third party component which is having issue, its your coding pattern also
checkAuth
should be used only once in whole application https://github.com/damienbod/angular-auth-oidc-client with respect to this package if you need token in some other module or guard you can callthis.oidcSecurityService.getAccessToken()
Thats a nice progress and update. Thanks for sharing