Installation
composer require cetria/laravel-auth
Publish the package configuration:
php artisan vendor:publish --provider="Cetria\LaravelAuth\AuthServiceProvider" --tag="config"
Configuration
config/auth.php (published by the package) to define:
api_guard (default: sanctum)token_lifetime (default: 1440 minutes)login, refresh, logout routes).First Use Case
Register a route in routes/api.php:
Route::post('/auth/login', [\Cetria\LaravelAuth\Http\Controllers\AuthController::class, 'login']);
Test with a POST request to /auth/login with credentials:
{
"email": "user@example.com",
"password": "password123"
}
The response will include a token and token_type (e.g., Bearer).
Token-Based Authentication
AuthController to handle:
login(): Issues a Sanctum/Bearer token.refresh(): Extends token validity (if configured).logout(): Revokes the token.Route::middleware(['auth:sanctum'])->group(function () {
Route::get('/profile', [UserController::class, 'show']);
});
Customization
AuthController:
namespace App\Http\Controllers;
use Cetria\LaravelAuth\Http\Controllers\AuthController as BaseAuthController;
class AuthController extends BaseAuthController
{
public function login()
{
$response = parent::login();
return $response->withAdditionalData(['custom_field' => 'value']);
}
}
config/auth.php:
'token_payload' => [
'user_id',
'username',
'custom_metadata' => 'value',
],
Integration with Sanctum
HasApiTokens trait) alongside the package.use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
}
Rate Limiting
app/Http/Kernel.php:
'auth' => ['throttle:60,1'],
Token Revocation
$user->currentAccessToken()->delete();
revoke() helper:
Sanctum::revoke($token);
Configuration Overrides
config/auth.php is merged correctly. Use php artisan config:clear if changes aren’t reflected.sanctum) must match the guard in config/auth.php and app/Http/Kernel.php.CORS Issues
config/cors.php:
'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
Token Validation Errors
HasApiTokens trait for token validation logic.Sanctum::debugging();
\Log::info('Token payload:', $user->createToken('name')->plainTextToken);
Custom Payloads
token_payload in config is ignored, verify the AuthController is not overriding it. Extend the controller to debug:
public function login()
{
\Log::info('Payload before:', $this->getTokenPayload());
$response = parent::login();
return $response;
}
Custom Auth Logic
AuthController methods (e.g., validateCredentials) for custom validation:
protected function validateCredentials(array $credentials)
{
return User::where('email', $credentials['email'])
->where('password', $credentials['password'])
->where('account_status', 'active')
->exists();
}
Multi-Guard Support
passport):
// In AuthServiceProvider
$this->app['auth']->extend('passport', function ($app) {
return new PassportGuard($app['auth']->createUserProvider(), $app['request']);
});
Event Listeners
auth.login, auth.logout) in EventServiceProvider:
protected $listen = [
'auth.login' => [
\App\Listeners\LogAuthAttempt::class,
],
];
How can I help you explore Laravel packages today?