Install the package:
composer require mydaniel/laravel-paseto
Publish the config:
php artisan vendor:publish --provider="MyDaniel\Paseto\PasetoServiceProvider" --tag="config"
Generate a secret key (critical for security):
php artisan paseto:generate-key
Store the output securely (e.g., in .env as PASETO_SECRET_KEY).
Configure config/auth.php:
Add the Paseto guard to your guards array:
'guards' => [
'web' => ['driver' => 'session'],
'api' => ['driver' => 'paseto', 'provider' => 'users'],
],
Generate a token for a user (e.g., in a controller or service):
use MyDaniel\Paseto\Facades\Paseto;
$token = Paseto::generate([
'user_id' => auth()->id(),
'exp' => now()->addHours(24),
]);
Use the token in API requests (e.g., Authorization: Bearer <token>).
Authentication Flow:
AuthenticatesUsers trait.Paseto::attempt() for manual validation:
if (Paseto::attempt($token, ['user_id'])) {
$user = User::find(Paseto::get('user_id'));
auth()->login($user);
}
Token Blacklisting:
PasetoBlacklist model:
Paseto::blacklist($token); // Manually blacklist
Illuminate\Auth\Events\Logout.Custom Claims:
$token = Paseto::generate([
'user_id' => 1,
'role' => 'admin',
'exp' => now()->addMinutes(30),
]);
auth:api middleware for Paseto-protected routes.Paseto facade in tests:
Paseto::shouldReceive('attempt')->andReturn(true);
Secret Key Management:
.env or a secrets manager.paseto:rotate-key if added in future updates).Token Storage:
HttpOnly cookies for web apps to prevent XSS theft.Blacklist Performance:
Clock Skew:
exp claims). Use PASETO_LEEWAY in config for minor time drift.config/paseto.php for strict mode (enforces exp/nbf).PASETO_SECRET_KEY matches the key used to generate the token.debug mode in config to log token generation/validation:
'debug' => env('PASETO_DEBUG', false),
MyDaniel\Paseto\Contracts\Paseto to implement asymmetric Paseto (v3) if needed.PasetoBlacklist to support Redis for scalability.Paseto::validate() hooks:
Paseto::extend(function ($token) {
return $token->get('role') === 'admin';
});
How can I help you explore Laravel packages today?