Keycloak JS client and confidential clients

front application are insecure by design, that’s why the ‘public’ mode exists. You cannot safely store a secret in a front application. The OIDC security there is only based on the allowed domains and origins. A frotn application should never use a non-public client.

Your public js application should communicate with a backend application, this backend application can use ‘confidential’ or ‘bearer-only’ mode. The bearer-only mode is especially made for such backends, as they receive tokens from ajax requests sent by front application, they do not need to redirect the user to the login page when the token is wrong (as a confidential application would do), but send errors instead. I’m pretty sure a bearerOnly application could use the introspection endpoint.

You can also add ‘audience’ (‘aud’) management with the ‘scope’ definition, and ensure that the access token received by the front application contains audiences. these access_tokens are used for communications between the front app and the backends, and the audience field is a list of valid client_id for this token (so it can list the valid backends).

On the other side a backend application should never use a ‘public’ client. That’s only for front applications, with validated redirects and logins.

I hope this explains why public clients have some limitations.