PHP code example of kristiansnts / filament-api-login

1. Go to this page and download the library: Download kristiansnts/filament-api-login library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kristiansnts / filament-api-login example snippets


'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'external' => [
        'driver' => 'external_session',
    ],
],



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
    }
}



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;
    }
}

$this->app->bind(
    \Kristiansnts\FilamentApiLogin\Services\ExternalAuthService::class,
    \App\Services\CustomExternalAuthService::class
);

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);
    }
])

'log_failures' => true,
bash
php artisan vendor:publish --tag="filament-api-login-config"