apps/docs/content/sdk-examples/laravel.mdx
Laravel is a web application framework with expressive, elegant syntax. It provides a robust set of features for web applications, making it one of the most popular choices for building server-side applications. This example demonstrates how to integrate Zitadel using the OAuth 2.0 PKCE flow to authenticate users securely and maintain sessions across your application.
This example uses Laravel Socialite, the standard authentication library for Laravel applications. Laravel Socialite implements the OpenID Connect (OIDC) flow through a custom Zitadel provider, manages PKCE, performs secure token exchange, and provides session management through Laravel's authentication system.
This example shows a complete authentication implementation using Laravel with Zitadel. Users start on a public landing page, click a login button to authenticate with Zitadel using the secure PKCE flow, and are redirected to a protected profile page displaying their user information after successful authentication.
The application implements server-side session management with Laravel's built-in session handling, storing authentication state securely in encrypted cookies. Protected routes use Laravel middleware to automatically redirect unauthenticated users to the sign-in flow, ensuring only authenticated users can access sensitive areas. The profile page displays comprehensive user information including OIDC claims and session metadata.
The application demonstrates proper federated logout by terminating sessions both locally and with Zitadel's end-session endpoint, complete with CSRF protection using state parameters. Additionally, it includes automatic token refresh using refresh tokens to maintain long-lived sessions without requiring users to re-authenticate. The example uses Zitadel-specific scopes like urn:zitadel:iam:user:metadata and urn:zitadel:iam:org:projects:roles to access extended user attributes and role information for implementing role-based access control (RBAC).
Before running this example, you need to create and configure a PKCE application in the ZITADEL Management Console. Follow the PKCE application setup guide to:
http://localhost:3000/auth/callback for development)http://localhost:3000/auth/logout/callback)Note: Make sure to enable Dev Mode in the ZITADEL Management Console if you're using HTTP URLs during local development. For production, always use HTTPS URLs and disable Dev Mode.
Once you have your Zitadel application configured:
.env file (copy from .env.example) and configure it with the values from your Zitadel application. Use these exact environment variable names:
APP_KEY=your-app-key
APP_ENV=local
APP_DEBUG=true
SERVER_URL=http://localhost:3000
SERVER_PORT=3000
DB_CONNECTION=sqlite
ZITADEL_DOMAIN=https://your-zitadel-domain
ZITADEL_CLIENT_ID=your-zitadel-application-client-id
ZITADEL_CLIENT_SECRET=your-randomly-generated-client-secret
ZITADEL_POST_LOGOUT_URL=http://localhost:3000/auth/logout/callback
ZITADEL_DOMAIN (the issuer)ZITADEL_CLIENT_IDZITADEL_POST_LOGOUT_URLAPP_KEY (generate using: php artisan key:generate)ZITADEL_CLIENT_SECRET (generate using: php -r "echo bin2hex(random_bytes(32));")composer install and start the development server with composer run dev to verify the authentication flow end-to-end.