Unified authentication package for Laravel supporting Sanctum, JWT, and Passport
obrainwave/auth-fusion is a Laravel package for unified authentication package for laravel supporting sanctum, jwt, and passport.
It currently has 3 GitHub stars and 1 downloads on Packagist (latest version 1.0.0).
Install it with composer require obrainwave/auth-fusion.
Discover more Laravel packages by obrainwave
or browse all Laravel packages to compare alternatives.
Last updated
A unified authentication package for Laravel that seamlessly integrates Sanctum, JWT, and Passport into a single, flexible API.
composer require obrainwave/auth-fusion
For Laravel 11+, the service provider and aliases will be auto-discovered. For Laravel 10, manually register in config/app.php if needed.
Publish the configuration file:
php artisan vendor:publish --tag=auth-fusion-config
Use our artisan command to automatically install and configure drivers:
# Install Sanctum driver
php artisan auth-fusion:install sanctum
# Install JWT driver
php artisan auth-fusion:install jwt
# Install Passport driver
php artisan auth-fusion:install passport
# Install all drivers at once
php artisan auth-fusion:install all
# Install with custom model name
php artisan auth-fusion:install sanctum --model=Admin
The command will:
.env automaticallyConfigure your default driver in .env:
AUTH_FUSION_DRIVER=sanctum
use AuthFusion;
// Or use the full namespace:
// use Obrainwave\AuthFusion\Facades\AuthFusion;
// Login with default driver
$result = AuthFusion::driver()->login([
'email' => '[email protected]',
'password' => 'password'
]);
return response()->json([
'token' => $result['token'],
'user' => $result['user']
]);
// Validate token
if (AuthFusion::driver()->validate($token)) {
$user = AuthFusion::driver()->getUser($token);
}
// Logout
AuthFusion::driver()->logout($token);
// Use Sanctum
AuthFusion::driver('sanctum')->login($credentials);
// Use JWT
AuthFusion::driver('jwt')->login($credentials);
// Use Passport
AuthFusion::driver('passport')->login($credentials);
$result = AuthFusion::driver('sanctum')->login(
$credentials,
[
'device_name' => 'iPhone 15',
'abilities' => ['read', 'write'],
'expires_at' => now()->addWeek()
]
);
try {
$newToken = AuthFusion::driver('jwt')->refresh($oldToken);
return response()->json(['token' => $newToken['token']]);
} catch (\Illuminate\Auth\AuthenticationException $e) {
return response()->json(['error' => 'Invalid token'], 401);
}
// Change default driver at runtime
AuthFusion::setDefaultDriver('jwt');
// Now all calls use JWT by default
AuthFusion::driver()->login($credentials);
All drivers implement the AuthDriverInterface which provides:
| Method | Description |
|--------|-------------|
| login(array $credentials, array $options = []) | Authenticate user and return token |
| logout($token) | Logout user and invalidate token |
| refresh($token) | Refresh an access token |
| validate($token) | Check if token is valid |
| getUser($token) | Get authenticated user from token |
| getName() | Get the driver name |
You can create custom drivers by extending the AuthDriverInterface:
<?php
namespace App\Drivers;
use Obrainwave\AuthFusion\Contracts\AuthDriverInterface;
class CustomDriver implements AuthDriverInterface
{
// Implement all required methods
public function login(array $credentials, array $options = []): array { }
public function logout($token): bool { }
public function refresh($token): array { }
public function validate($token): bool { }
public function getUser($token): ?Authenticatable { }
public function getName(): string { }
}
Register your custom driver:
use App\Drivers\CustomDriver;
use AuthFusion;
AuthFusion::extend('custom', function () {
return new CustomDriver();
});
// Use it
AuthFusion::driver('custom')->login($credentials);
All drivers throw \Illuminate\Auth\AuthenticationException on authentication failures:
try {
$result = AuthFusion::driver()->login($credentials);
} catch (\Illuminate\Auth\AuthenticationException $e) {
return response()->json([
'message' => 'Invalid credentials'
], 401);
}
You can create unified middleware for all drivers:
<?php
namespace App\Http\Middleware;
use AuthFusion;
use Closure;
class AuthFusionMiddleware
{
public function handle($request, Closure $next)
{
$token = $request->bearerToken();
if (!$token || !AuthFusion::driver()->validate($token)) {
return response()->json(['message' => 'Unauthenticated'], 401);
}
$user = AuthFusion::driver()->getUser($token);
$request->setUserResolver(fn() => $user);
return $next($request);
}
}
Edit config/auth-fusion.php:
return [
'driver' => env('AUTH_FUSION_DRIVER', 'sanctum'),
'drivers' => [
'sanctum' => [
'guard' => 'web',
],
'jwt' => [
'guard' => 'api',
],
'passport' => [
'guard' => 'web',
],
],
];
Run tests with:
php artisan test
Or with PHPUnit:
./vendor/bin/phpunit
If you get this error even after installing Sanctum:
Run composer dump-autoload:
composer dump-autoload
Ensure your User model uses HasApiTokens:
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
}
Clear Laravel caches:
php artisan config:clear
php artisan cache:clear
Verify Sanctum is installed:
composer show laravel/sanctum
Follow the same steps, but ensure you've:
Contributions are welcome! Please feel free to submit a Pull Request.
The MIT License (MIT). Please see License File for more information.
For issues and questions:
Please see CHANGELOG for more information on what has changed recently.