Keycloak-Js manually set token

Hello,

We are using Keycloak 15 for our application, and we are using keycloak-js (Version 15 also : https://www.npmjs.com/package/keycloak-js)

Our application would have two ways of authenticating for 2 users.
One way is the standard way with the redirection on our keycloak instance login page.

The other way tho, require us to manually call keycloak back-end for an access token due to the flows we would like to have right now.

The thing is, while we can obtain the token without any problems, we cannot feed our keycloak instance in the front-end correctly with this token.
We tried init but it did not work correctly with various options we tried. (note that the keycloak instance should be empty when we want to feed it with the new token)

What we do is something like

      keycloak.sessionId = request.data.session_state;
      keycloak.token = request.data.access_token;
      keycloak.refreshToken = request.data.access_token;
      keycloak.authenticated = true;

But it seems it does not trigger some internal logic of the Keycloak Instance and barely work.
We spent a lot of time looking for answer, and most of time we found unanswered questions, or nothing better than the solution above.

Any insight or help would be awesome!

1 Like

Bump, any insight on this? :frowning:

1 Like

We need the same here. Up!

I got the same problem. Have you solved the issue?

Well, not really.

We managed to solve our case by trial and error but we still use more or less the same method, we just adjusted bugs as we encountered them…

We since passed to keycloak 18 but same problems apply

Hi @Kenny.

I have the same issue. For now I use your method. But, as you said yourself, it barely works : I found a bug with a specific case.

It happens if :

  • in a first browser tab, I open my app passing the token as a param in the URL => it works. It authenticates to my app with your method.
  • in a second browser tab, I open my app again, but without passing the token as a param => it fails. I am redirected to the classic keycloak login page. Which is quite normal: if my app is launched that way, it has no information on the authentication (because your method is a trick that never really informs keycloak of the authentication).

I don’t find how to solve it. The proper way would be to inform keycloak of the authentication. But I guess if it would be easily possible, you wouldn’t have look for that tricky method…

Do you have any new information/code to help me?

Thanks

Hey,

I am not sure for your version if it works, but I am actually doing this in 18 using init options of JavaScript Adapter

keycloak.init({
config: {
url: XXX,
realm: XXX,
clientId: XXX
},
initOptions: {
onLoad: ‘login-required’,
token: XXX,
refreshToken: XXX,
checkLoginIframe: false
},
enableBearerInterceptor: true,
}

Hope it helps !

I’m facing the same problem today.

The solution mentioned by @pjmalandrino works fine, thanks!
But the session is not kept after I refresh the page, which can be a problem. Did any of you find a solution for this?

This is probably because of missing values in cookies/local storage. But all these are defined by when login through the Keycloak UI (which I don’t want here for this case).

Thank you