Auth0 integration in PHP - getUser() always returning NULL

Within login.php in the Auth0 starter app for PHP, if you run the following code, which comes straight from the documentation, you won't be able to get the user information. I.e., $auth0->getUser(); will return NULL.

<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/dotenv-loader.php';

$auth0 = new Auth0\SDK\Auth0([
    'domain' => $_ENV['AUTH0_DOMAIN'],
    'client_id' => $_ENV['AUTH0_CLIENT_ID'],
    'redirect_uri' => $_ENV['AUTH0_CALLBACK_URL'],
    'audience' => $_ENV['AUTH0_AUDIENCE'],
    'scope' => 'openid profile email',
]);

$auth0->login();

Answer

Solution:

It took me a while to figure this out, but you'll need to add the client secret within the new instance of auth0. I.e. add the following snippet: 'client_secret' => $_ENV['AUTH0_CLIENT_SECRET'],

The final code should look like:

<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/dotenv-loader.php';

$auth0 = new Auth0\SDK\Auth0([
    'domain' => $_ENV['AUTH0_DOMAIN'],
    'client_id' => $_ENV['AUTH0_CLIENT_ID'],
    'redirect_uri' => $_ENV['AUTH0_CALLBACK_URL'],
    'audience' => $_ENV['AUTH0_AUDIENCE'],
    'scope' => 'openid profile email',
]);

$auth0->login();

At this point, when you call $auth0->getUser(); it will return the full user object.

Source