Installation:
composer require hans-thomas/sphinx
php artisan vendor:publish --tag=sphinx-config
Publish the config and publish migrations (if using database-backed tokens):
php artisan vendor:publish --tag=sphinx-migrations
php artisan migrate
Model Integration:
Apply the SphinxTrait to your User model (or equivalent):
use Hans\Sphinx\Traits\SphinxTrait;
class User extends Authenticatable
{
use SphinxTrait;
// Implement required abstract methods
public function getJwtIdentifier(): string { return $this->id; }
public function getJwtClaims(): array { return ['sub' => $this->id]; }
public function getJwtCustomClaims(): array { return []; }
}
First Authentication Flow:
Sphinx facade:
use Hans\Sphinx\Facades\Sphinx;
$tokens = Sphinx::generateTokens($user);
// Returns ['access_token' => '...', 'refresh_token' => '...']
$user = Sphinx::authenticate(); // Throws exception if invalid
Token Generation & Issuance:
$tokens = Sphinx::generateTokens($user, [
'expires_in' => 3600, // Custom TTL (default: config)
'refresh_ttl' => 86400,
]);
$newAccessToken = Sphinx::refreshToken($refreshToken);
Middleware Integration:
Use the provided middleware (Sphinx::middleware()) in app/Http/Kernel.php:
protected $routeMiddleware = [
'auth.sphinx' => \Hans\Sphinx\Http\Middleware\Authenticate::class,
];
Apply to routes:
Route::middleware('auth.sphinx')->group(function () {
// Protected routes
});
Custom Claims & Metadata: Extend claims dynamically:
public function getJwtCustomClaims(): array {
return [
'roles' => $this->roles->pluck('name'),
'permissions' => $this->permissions,
];
}
Horus Integration (if used):
// Sync with Horus (e.g., for multi-account management)
Sphinx::syncWithHorus($userId, $accountId);
Token Revocation:
Sphinx::revokeToken($token);
$user->markTokenAsRevoked($token); // Custom logic
Multi-Tenancy: Add tenant context to claims:
public function getJwtCustomClaims(): array {
return ['tenant_id' => auth()->tenant()->id];
}
Rate Limiting:
Combine with Laravel’s throttle middleware:
Route::middleware(['throttle:60,1', 'auth.sphinx'])->group(...);
Testing:
Use the SphinxTestCase helper:
use Hans\Sphinx\Testing\SphinxTestCase;
class UserTest extends SphinxTestCase {
public function testAuthentication() {
$tokens = $this->generateTokens($this->user);
$this->assertAuthenticatedAs($this->user);
}
}
Double Hooks:
self::sphinxHooks() in booted() causes token generation to fail.protected static function booted() {
static::sphinxHooks();
}
Clock Skew:
JWT_LEEWAY in .env (e.g., JWT_LEEWAY=30).Refresh Token Exhaustion:
Custom Claims Serialization:
getJwtCustomClaims() throws errors.DateTime to strings).Token Decoding:
Use the Sphinx facade to inspect tokens:
$decoded = Sphinx::decodeToken($token);
dd($decoded);
Logging:
Enable debug logging in config/sphinx.php:
'debug' => env('SPHINX_DEBUG', false),
Token Expiry: Check expiry claims manually:
$expiry = Sphinx::getTokenExpiry($token);
Custom Encryption:
Override the default encryption layers in config/sphinx.php:
'encryption' => [
'key' => env('SPHINX_ENCRYPTION_KEY'),
'algorithm' => 'AES-256-CBC', // Custom algorithm
],
Event Listeners:
Listen to token events (e.g., TokenGenerated, TokenRevoked):
Sphinx::listen('TokenGenerated', function ($event) {
// Log or sync with external systems
});
Guard Integration: Extend Laravel’s auth guard for Sphinx:
// In AuthServiceProvider
$this->app['auth']->extend('sphinx', function ($app) {
return new SphinxGuard($app['request']);
});
Database Backend: Customize the token storage table by publishing and modifying migrations:
php artisan vendor:publish --tag=sphinx-migrations
How can I help you explore Laravel packages today?