This is an example Laravel application showing how to add user authentication using Authgear via OpenID Connect (OIDC) and OAuth 2.0.
It targets Laravel 12 and PHP 8.2+.
- Redirecting users to Authgear to sign in (
/login). - Handling the OAuth 2.0 authorization-code callback with state (CSRF) validation.
- Exchanging the code for tokens and reading the user's profile from the OIDC UserInfo endpoint.
- Creating a local
Userkeyed by the stable Authgear subject (sub) and safely linking to an existing account only when the email is verified. - Signing the user out of both the app and Authgear (
/logout).
The Authgear integration lives in:
config/authgear.php— configuration (readsAUTHGEAR_*env vars).app/Providers/AppServiceProvider.php— binds the OAuth provider.app/Http/Controllers/OAuthController.php— the OAuth flow.
- PHP 8.2+
- Composer 2
- Node.js 18+ and npm
-
Sign up for a free Authgear account and create an application of type OIDC Client Application (Regular Web App).
-
In the Authgear portal, add this redirect URI to your application:
http://localhost:8000/oauth/callback -
Clone and install dependencies:
composer install npm install
-
Create your environment file and generate an app key:
cp .env.example .env php artisan key:generate
-
Fill in the Authgear values in
.env:AUTHGEAR_PROJECT_URL=https://your-project.authgear.cloud AUTHGEAR_APP_CLIENT_ID=your-client-id AUTHGEAR_APP_CLIENT_SECRET=your-client-secret AUTHGEAR_APP_REDIRECT_URI=http://localhost:8000/oauth/callbackThe app uses SQLite by default, so no database server is required. Create the database file:
touch database/database.sqlite
-
Run migrations and build front-end assets:
php artisan migrate npm run build
-
Start the app:
php artisan serve
Open http://localhost:8000 and click Login.
php artisan testFor a step-by-step guide, see the Authgear docs: https://docs.authgear.com/get-started/regular-web-app/laravel