Angular Capacitor JSON exception

Problem statement

I am seeing an issue with opening the browser in my Ionic/Angular/Capacitor app, specifically on the Android platform. The Capacitor browser flashes open and closes immediately but fails the login.

Attempts to call the /token endpoint fail with ‘JSON Exception’ when this.auth0.handleRedirectCallback(url) is called.

Symptoms

Attempts to call the /token endpoint fail with ‘JSON Exception’ when this.auth0.handleRedirectCallback(url) is called.

It looks like the Auth0 SDK is trying to send the /token request with the body of a string when it is expecting a JSON:

org.json.JSONException: JSValue could not be coerced to JSObject.

Cause

The capacitor/android package had a bug regarding how it sent requests with url-encoded type header requests. It was this exact issue:

Solution

Upgrading to the latest Capacitor 4 versions resolved the issue