Auth0 PHP SDK Missing code $auth0->exchange()

SDK: auth0-php
SDK Version: ^8.0
Platform Version: PHP 8.0

I was going along the quick start for setting up the SDK in order to apply a Discord authenticator to my PHP site. For now, though, I was simply setting up an empty file in accordance with the guide.
I do get the authentication through, but once I return with the callback I do get the following error message:

Fatal error: Uncaught Auth0\SDK\Exception\StateException: Missing code in .../vendor/auth0/auth0-php/src/Exception/StateException.php:30
Stack trace: #0 ../vendor/auth0/auth0-php/src/Auth0.php(317): Auth0\SDK\Exception\StateException::missingCode() #1 ../user2.php(69): Auth0\SDK\Auth0->exchange('http://127.0.0....')

Still, I followed the guide from start to finish. I even copy-pasted the code to make sure I didn’t make any typos.

  • Code Snippets/Error Messages/Supporting Details/Screenshots:

As far as I could see, the issue comes in my ROUTE_URL_CALLBACK that I defined earlier.
I checked the data and naming but all seem to be correct.

Hi @JoeyJaySWE,

A ‘missing code’ error occurs when the ?code parameter was not returned or is not accessible to your application’s callback from the authentication flow, for whatever reason. From your callback page, can you check what parameters your user is being returned to your application in the URL with?

  • You should have ?code=… and ?state=… query parameters in your callback URL after a successful authentication flow.
  • If there is an ?error parameter present instead, that may give you a hint as to what’s going wrong.
  • From your Auth0 Dashboard, check your logs and ensure the authentication flow is actually going through successfully.
1 Like

Turns out it didn’t go through properly.
I get a failed login attempt with “invalid_request”.
I still managed to get the Discord connection showing and I get to the stage where it finds my account and I can press Authorize, but once I do it fails.
I can’t seem to find any mentioning of what has failed, I only got a parameter saying invalid_request and a state in hexadecimal.

Anything unusual in your Auth0 dashboard logs? Can find them under Monitoring > Logs on the sidebar

1 Like