auth0/auth0-php
The official Auth0 PHP SDK for Authentication and Management APIs. Build PHP 8.2+ apps with PSR-18/17/7 HTTP support and tailored Laravel, Symfony, and WordPress SDKs. Includes quickstarts for session-based web apps and token-protected backend APIs.
composer require auth0/auth0-php
.env (Laravel):
AUTH0_DOMAIN=your-tenant.auth0.com
AUTH0_CLIENT_ID=...
AUTH0_CLIENT_SECRET=...
AUTH0_COOKIE_SECRET=$(openssl rand -hex 32)
AUTH0_CALLBACK_URL=https://your-app.test/auth0/callback
// AppServiceProvider::register()
$this->app->singleton(Auth0::class, function () {
return new Auth0(new SdkConfiguration(
domain: config('auth0.domain'),
clientId: config('auth0.client_id'),
clientSecret: config('auth0.client_secret'),
cookieSecret: config('auth0.cookie_secret'),
redirectUri: config('auth0.callback_url'),
));
});
Route::get('/dashboard', function () {
$auth0 = app(Auth0::class);
$creds = $auth0->getCredentials();
if (! $creds || $creds->accessTokenExpired) {
return redirect($auth0->login());
}
return view('dashboard', ['user' => $creds->user]);
})->middleware('web');
$auth0->login().Route::get('/auth0/callback', function (Auth0 $auth0) {
if ($auth0->getExchangeParameters()) {
$auth0->exchange();
return redirect('/dashboard');
}
return redirect('/login');
});
$credentials = $auth0->getCredentials(); $user = $credentials?->user;The SDK now exposes Application Profiles, CAPTCHA policies, and client search alongside existing endpoints.
use Auth0\SDK\Management\Auth0ManagementClient;
$mgmt = new Auth0ManagementClient(
domain: config('auth0.domain'),
clientId: config('auth0.client_id'),
clientSecret: config('auth0.client_secret'),
scope: ['read:users','update:users','read:clients','create:clients','read:captcha_policies']
);
$clients = $mgmt->clients->search([
'q' => 'name:"My App"',
'per_page' => 10,
]);
$profile = $mgmt->applicationProfiles->create([
'name' => 'My Profile',
'description' => '...',
]);
$policy = $mgmt->captchaPolicies->create([
'name' => 'Bot Protection',
'enabled' => true,
]);
Use the SDK’s token helper (e.g., Auth0\SDK\Token) or a JWT library. Example with firebase/php-jwt:
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
$decoded = JWT::decode($request->bearerToken(), new Key($publicKey, 'RS256'));
// Validate issuer, audience, expiry
Custom claims are added via Auth0 Actions/Rules, not the SDK. After login, the $credentials->user object includes custom claims if configured in the ID token. Example Auth0 Action:
exports.onExecutePostLogin = async (event, api) => {
api.idToken.setCustomClaim('https://myapp.com/roles', event.user.app_metadata.roles);
};
Then access in Laravel: $user->{'https://myapp.com/roles'}.
renew(). Ensure your Auth0 tenant’s signing keys are correctly configured and the aud/iss match.public function handle($request, Closure $next) {
$auth0 = app(Auth0::class);
if ($auth0->getCredentials()?->accessTokenExpired) {
$auth0->renew(); // now validates ID token
}
return $next($request);
}
renew() now validates ID token – previously it returned tokens without verification. This may cause failures if your ID token is invalid (e.g., clock skew, wrong audience). Test renew() in staging.read:clients, create:clients, read:captcha_policies). Update your Auth0 application’s granted scopes.http vs https).accessTokenExpired before using credentials; leverage renew() but be aware of the new validation.$config = new SdkConfiguration(..., debug: true);
Logs go to storage/logs/laravel.log.composer require firebase/php-jwt
$decoded = JWT::decode($token, new Key($key, 'RS256'));
try {
$mgmt->users->get('user_id');
} catch (\Auth0\SDK\Exception\SdkException $e) {
report($e);
}
$http = new \GuzzleHttp\Client(['timeout' => 10]);
$config = new SdkConfiguration(..., httpClient: $http);
User with an auth0_id column and sync on callback.Cache::remember.$mgmt->users->get($id, ['fields' => ['email','name']]).deleteUsers) where available..env / Laravel’s encrypted config.cookieSettings: ['secure' => true, 'httpOnly' => true, 'sameSite' => 'Lax']
RateLimiter.How can I help you explore Laravel packages today?