Skip to content

authgear/authgear-example-laravel

Repository files navigation

Add User Authentication to a Laravel App with Authgear and OAuth 2.0

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+.

What it demonstrates

  • 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 User keyed 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 (reads AUTHGEAR_* env vars).
  • app/Providers/AppServiceProvider.php — binds the OAuth provider.
  • app/Http/Controllers/OAuthController.php — the OAuth flow.

Prerequisites

  • PHP 8.2+
  • Composer 2
  • Node.js 18+ and npm

Setup

  1. Sign up for a free Authgear account and create an application of type OIDC Client Application (Regular Web App).

  2. In the Authgear portal, add this redirect URI to your application:

    http://localhost:8000/oauth/callback
    
  3. Clone and install dependencies:

    composer install
    npm install
  4. Create your environment file and generate an app key:

    cp .env.example .env
    php artisan key:generate
  5. 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/callback
    

    The app uses SQLite by default, so no database server is required. Create the database file:

    touch database/database.sqlite
  6. Run migrations and build front-end assets:

    php artisan migrate
    npm run build
  7. Start the app:

    php artisan serve

    Open http://localhost:8000 and click Login.

Running tests

php artisan test

Learn more

For a step-by-step guide, see the Authgear docs: https://docs.authgear.com/get-started/regular-web-app/laravel

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages