PHPackages                             bhry98/keycloak-laravel-auth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. bhry98/keycloak-laravel-auth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

bhry98/keycloak-laravel-auth
============================

Laravel Keycloak authentication via Socialite

v1.0.0(3mo ago)0298↓33.3%MITPHP

Since Feb 12Pushed 1mo agoCompare

[ Source](https://github.com/bhry98/keycloak-laravel-auth)[ Packagist](https://packagist.org/packages/bhry98/keycloak-laravel-auth)[ RSS](/packages/bhry98-keycloak-laravel-auth/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

🛡️ Bhry98 Keycloak Auth for Laravel
===================================

[](#️-bhry98-keycloak-auth-for-laravel)

A modern **Laravel authentication package** that integrates **Keycloak** using **Laravel Socialite** for both **API** and **Filament panel** authentication.

> Built for enterprise-grade Laravel apps needing Keycloak SSO integration.

---

🚀 Features
----------

[](#-features)

- ✅ Keycloak authentication using **Socialite**
- ✅ Works for **APIs** (JWT-based) and **Filament panels**
- ✅ Auto-refresh Keycloak tokens
- ✅ Role-based access via `HasKeycloakRoles` trait
- ✅ Middleware protection for routes
- ✅ Extendable service structure (OIDC, JWT, and Socialite)
- ✅ Plug-and-play with any Laravel app

---

📦 Installation
--------------

[](#-installation)

### Step 1: Install the package

[](#step-1-install-the-package)

```
composer require bhry98/keycloak-laravel-auth
```

---

### Step 2: Publish config file

[](#step-2-publish-config-file)

```
php artisan vendor:publish --provider="Bhry98\KeycloakAuth\Providers\KeycloakAuthServiceProvider" --tag="config"
```

This will create a config file:

```
config/bhry98-keycloak.php

```

---

### Step 3: Add Keycloak credentials to `.env`

[](#step-3-add-keycloak-credentials-to-env)

```
KEYCLOAK_BASE_URL=https://keycloak-domain
KEYCLOAK_REALM=your-realm-id
KEYCLOAK_CLIENT_ID=your-client-id
KEYCLOAK_CLIENT_SECRET=your-client-secret
KEYCLOAK_REDIRECT_URI=${APP_URL}/auth/callback
```

---

### Step 4: Register in Filament (optional)

[](#step-4-register-in-filament-optional)

```
->authMiddleware([
    \Bhry98\KeycloakAuth\Http\Middleware\KeycloakMiddleware::class,
])
```

---

🔐 Middleware Usage
------------------

[](#-middleware-usage)

You can protect routes for both **API** and **Web** like this:

```
// web
Route::middleware(['keycloak.web'])->group(function () {
    Route::get('/user', function (Request $request) {
        return $request->user();
    });
});

// api
Route::middleware(['keycloak.api'])->group(function () {
    Route::get('/user', function (Request $request) {
        return $request->user();
    });
});
```

For **API frontends**, use token-based auth:

```
Authorization: Bearer
```

---

### 🧱 Middleware

[](#-middleware)

- `KeycloakMiddleware => keycloak.web` — checks for valid Keycloak access tokens basen on web session
- `KeycloakApiMiddleware => keycloak.api` — checks for valid Keycloak access tokens basen on api

---

🧠 Example Login Flow
--------------------

[](#-example-login-flow)

### Web (Filament)

[](#web-filament)

1. User clicks **Login with Keycloak**
2. Redirects to Keycloak
3. Keycloak returns `code` → package exchanges it for tokens
4. Laravel authenticates the user

### API (Frontend)

[](#api-frontend)

1. Frontend gets tokens via Keycloak
2. Sends `Authorization: Bearer ` with requests
3. Middleware validates and identifies the user

---

🧩 Folder Structure
------------------

[](#-folder-structure)

```
src/
├── config/
│   └── bhry98-keycloak.php
├── Http/
│   ├── Controllers/
│   │   └── KeycloakAuthController.php
│   └── Middleware/
│       ├── KeycloakApiMiddleware.php
│       └── KeycloakMiddleware.php
├── Providers/
│   └── KeycloakAuthServiceProvider.php
├── routes/
│   └── web.php
└── Services/
    ├── KeycloakJWTService.php
    └── KeycloakSocialiteProvider.php

```

---

💡 Example `.env` Setup for API + Filament
-----------------------------------------

[](#-example-env-setup-for-api--filament)

```
APP_URL=https://your-laravel-application-domain

KEYCLOAK_BASE_URL=https://keycloak-domain
KEYCLOAK_REALM=your-realm-id
KEYCLOAK_CLIENT_ID=your-client-id
KEYCLOAK_CLIENT_SECRET=your-client-secret
KEYCLOAK_REDIRECT_URI=${APP_URL}/auth/callback
```

---

🧑‍💻 Author
----------

[](#‍-author)

**BHR Abdelrahman**
💼 GitHub: [@bhry98](https://github.com/bhry98)

---

📄 License
---------

[](#-license)

This package is open-sourced software licensed under the **MIT license**.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance86

Actively maintained with recent releases

Popularity13

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

90d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/953ea673ae5e75b34c73d362b902d47932dc15e033b2a36a080bd363dd21f227?d=identicon)[bhry98](/maintainers/bhry98)

---

Top Contributors

[![bhry98](https://avatars.githubusercontent.com/u/85765830?v=4)](https://github.com/bhry98 "bhry98 (14 commits)")

### Embed Badge

![Health badge](/badges/bhry98-keycloak-laravel-auth/health.svg)

```
[![Health](https://phpackages.com/badges/bhry98-keycloak-laravel-auth/health.svg)](https://phpackages.com/packages/bhry98-keycloak-laravel-auth)
```

###  Alternatives

[lab404/laravel-impersonate

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

2.3k16.4M48](/packages/lab404-laravel-impersonate)[overtrue/laravel-follow

User follow unfollow system for Laravel.

1.2k404.7k5](/packages/overtrue-laravel-follow)[metrogistics/laravel-azure-ad-oauth

Provides single-sign-on ability to Microsoft Azure Active Directory enabled apps.

8679.1k1](/packages/metrogistics-laravel-azure-ad-oauth)[phhung1901/google_one_tap

Login with google one tap/google popup login

2716.1k](/packages/phhung1901-google-one-tap)[truckersmp/steam-socialite

Laravel Socialite provider for Steam OpenID.

1516.7k](/packages/truckersmp-steam-socialite)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
