Token-based authentication for FilamentPHP that authenticates against external APIs without requiring local database users
kristiansnts/filament-api-login is a Laravel package for token-based authentication for filamentphp that authenticates against external apis without requiring local database users.
It currently has 4 GitHub stars and 289 downloads on Packagist (latest version 1.0.0).
Install it with composer require kristiansnts/filament-api-login.
Discover more Laravel packages by kristiansnts
or browse all Laravel packages to compare alternatives.
Last updated
Token-based authentication for FilamentPHP that authenticates against external APIs without requiring local database users.
You can install the package via Composer:
composer require kristiansnts/filament-api-login
Publish the configuration file:
php artisan vendor:publish --tag="filament-api-login-config"
Add these variables to your .env file:
FILAMENT_API_LOGIN_URL=https://your-api.com/api/auth
FILAMENT_API_LOGIN_TIMEOUT=30
FILAMENT_API_LOGIN_LOG_FAILURES=true
Add the external guard to your config/auth.php:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'external' => [
'driver' => 'external_session',
],
],
Update your Filament Panel Provider to use the external authentication:
<?php
namespace App\Providers\Filament;
use Kristiansnts\FilamentApiLogin\Pages\Auth\Login;
use Filament\Panel;
use Filament\PanelProvider;
class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin')
->login(Login::class) // Use the package's login page
->authGuard('external') // Use the external guard
->colors([
'primary' => Color::Amber,
])
// ... rest of your configuration
}
}
Your external API should return a response in this format:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"data": {
"id": "123",
"email": "[email protected]",
"username": "john_doe",
"role": "admin"
}
}
You can customize the API request by extending the ExternalAuthService:
<?php
namespace App\Services;
use Kristiansnts\FilamentApiLogin\Services\ExternalAuthService as BaseService;
class CustomExternalAuthService extends BaseService
{
public function authenticate(string $email, string $password): ?array
{
// Add custom headers, modify request format, etc.
$response = Http::timeout($this->timeout)
->withHeaders([
'Accept' => 'application/json',
'X-API-Key' => config('app.api_key'),
])
->post($this->apiUrl, [
'email' => $email, // or 'username' => $email
'password' => $password,
'client_id' => config('app.client_id'),
]);
// Custom response handling
if ($response->successful()) {
$userData = $response->json();
if (isset($userData['token']) && isset($userData['data'])) {
return $userData;
}
}
return null;
}
}
Then bind your custom service in a service provider:
$this->app->bind(
\Kristiansnts\FilamentApiLogin\Services\ExternalAuthService::class,
\App\Services\CustomExternalAuthService::class
);
Override the canAccessPanel method in your panel configuration:
use Kristiansnts\FilamentApiLogin\Auth\SessionUser;
// In your Panel Provider
->authGuard('external')
->middleware([
// ... other middleware
function ($request, $next) {
$user = auth('external')->user();
if ($user && !in_array($user->role, ['admin', 'moderator'])) {
abort(403, 'Access denied');
}
return $next($request);
}
])
The package configuration file includes these options:
api_url - Your external authentication API endpoint (env: FILAMENT_API_LOGIN_URL)timeout - API request timeout in seconds (env: FILAMENT_API_LOGIN_TIMEOUT)log_failures - Enable/disable logging of authentication failures (env: FILAMENT_API_LOGIN_LOG_FAILURES)FILAMENT_API_LOGIN_URL and network connectivityEnable logging in the configuration to debug authentication issues:
'log_failures' => true,
Or via environment variable:
FILAMENT_API_LOGIN_LOG_FAILURES=true
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.