Ajax Logout from the frontend application without javascript adapter

Hello everyone,
I’m trying to get more control on the logout flow on the frontend. I am not using the javascript adapter.
Currently I’m redirecting my users to the utility logout-redirect endpoint, however I want to control the user experience here and avoid doing redirects.
I’ve tried the backchannel logout endpoint using a public client but I get a CORS error.
The public client web origins is set to *
This is how the request looks:

HEADERS: 
  - Authorization: Bearer ACCESS_TOKEN
  - Content-Type: application/x-www-form-urlencoded

POST <BASE_URL>/auth/realms/REALM/protocol/openid-connect/logout?client_id=public&refresh_token=REFRESH_TOKEN

I’ve been struggling with the documentation to be honest and this is what I put together from multiple stackoverflow posts and old messages from the keycloak mail list.

Any hints on how this should be done?

edit: clarified I’m not using js adapter

1 Like

Hi, I’ve read that one before but I’m not using the js adapters, I’ve updated my question to clarify that.
I couldn’t find there how to do it other than redirecting to the logout endpoint.
Thanks!

You could look at the source code of the javascript adapter.

So I’ve been through the adapter source code and it is not using a backchannel logout, it simply replaces the window location with the logout url using the provided options, but thanks for the suggestion.

Hi @fmayoral this work for me

const refresToken = localStorage.getItem(‘refresh_token’);
const headers = new HttpHeaders({ ‘Content-Type’: ‘application/x-www-form-urlencoded’ });
const body = new URLSearchParams();
body.set(‘client_id’, this.clientId);
body.set(‘refresh_token’, this.refresToken );

return this.httpClient.post(this.apiUrlLogout, body.toString(), { headers}
  ).subscribe(response => {
    console.log(response);
});
1 Like