Installation
composer require nghufron/aolauth
Ensure naailulghufron/aolauth is added to providers in config/app.php:
Naailulghufron\Aolauth\AolauthServiceProvider::class,
Publish Config
php artisan vendor:publish --provider="Naailulghufron\Aolauth\AolauthServiceProvider"
Configure .env with required keys (e.g., AOL_AUTH_KEY, AOL_AUTH_SECRET).
First Use Case Generate a token in a controller:
use Naailulghufron\Aolauth\Facades\Aolauth;
public function generateToken()
{
$token = Aolauth::generateToken();
return response()->json(['token' => $token]);
}
Token Generation
$token = Aolauth::generateToken(['user_id' => 123, 'expires' => 3600]);
user_id, expires, custom_data).Validation
use Naailulghufron\Aolauth\Middleware\VerifyToken;
protected $middleware = [
VerifyToken::class,
];
Integration with Laravel Auth
$payload = Aolauth::decodeToken($token);
$user = User::find($payload['user_id']);
Custom Claims
Extend the Naailulghufron\Aolauth\TokenBuilder class to add custom logic:
class CustomTokenBuilder extends TokenBuilder {
public function addCustomClaim($claim) {
$this->claims['custom'] = $claim;
}
}
Rate Limiting
Combine with Laravel’s throttle middleware to limit token generation:
Route::middleware(['throttle:5,1'])->group(function () {
Route::post('/generate-token', [TokenController::class, 'generateToken']);
});
Missing Config
.env keys will throw RuntimeException.php artisan vendor:publish and verify .env values.Token Expiry
null (no expiry). Always explicitly set expires in claims to avoid infinite tokens:
$token = Aolauth::generateToken(['expires' => time() + 3600]);
Secret Key Management
env() or a secrets manager.Invalid Tokens Check the payload structure with:
try {
$payload = Aolauth::decodeToken($token);
} catch (\Exception $e) {
Log::error("Token decode failed: " . $e->getMessage());
}
Logging Enable debug mode in config:
'debug' => env('AOL_AUTH_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Custom Algorithms
Override the default HMAC-SHA256 by extending Naailulghufron\Aolauth\TokenGenerator:
class CustomTokenGenerator extends TokenGenerator {
protected function sign($payload, $secret) {
return hash('sha512', $payload . $secret);
}
}
Token Storage Store tokens in a database for revocation:
// After generation
Token::create([
'token' => $token,
'user_id' => $payload['user_id'],
'expires_at' => $payload['expires'],
]);
Testing Mock the facade in tests:
$this->mock(Naailulghufron\Aolauth\Facades\Aolauth::class)
->shouldReceive('generateToken')
->andReturn('mocked_token_123');
How can I help you explore Laravel packages today?