Installation:
composer require raditzfarhan/simple-jwt-auth
Register Service Provider (Lumen):
Add to bootstrap/app.php:
$app->register(RaditzFarhan\SimpleJWTAuth\JWTAuthServiceProvider::class);
Configure Guards (in config/auth.php):
'guards' => [
'jwt' => [
'driver' => 'simple-jwt-auth',
'provider' => 'users',
],
],
Publish Config (if needed):
php artisan vendor:publish --provider="RaditzFarhan\SimpleJWTAuth\JWTAuthServiceProvider"
(Note: The package lacks a default config file; check config/jwt.php if published.)
First Use Case:
use RaditzFarhan\SimpleJWTAuth\Facades\JWTAuth;
Route::post('/login', function (Request $request) {
$credentials = $request->only(['email', 'password']);
$token = JWTAuth::attempt($credentials);
return response()->json(['token' => $token]);
});
Route::middleware('jwt')->get('/protected', function () {
return response()->json(['message' => 'Access granted']);
});
Authentication:
JWTAuth::attempt($credentials) for login.JWTAuth::once($credentials) for single-use tokens.JWTAuth::user() to retrieve the authenticated user from the request.Token Management:
$token = JWTAuth::fromUser($user); // Manually generate for a user.
Middleware Integration:
jwt middleware to routes:
Route::middleware('jwt')->group(function () {
// Protected routes
});
public function __construct() {
$this->middleware('jwt');
}
public function protectedMethod() {
$user = JWTAuth::user(); // Current user
}
Custom Guards:
$user = JWTAuth::authenticate($request); // Manually authenticate
if (!$user->hasRole('admin')) {
abort(403);
}
Route::group(['middleware' => 'jwt'], ...) for route protection.Authorization header:
Authorization: Bearer <token>
if (Auth::guard('jwt')->check()) {
// JWT logic
}
No Built-in Refresh Tokens:
Lumen-Only Focus:
Limited Configuration:
jwt.php config file is published. Customize via:
config(['jwt' => [
'secret' => env('JWT_SECRET'),
'ttl' => env('JWT_TTL', 60), // Token time-to-live in minutes
]]);
JWT_SECRET is set in .env.Token Expiry Handling:
TokenExpiredException. Catch it globally in App\Exceptions\Handler:
public function render($request, Throwable $exception) {
if ($exception instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException) {
return response()->json(['error' => 'Token expired'], 401);
}
return parent::render($request, $exception);
}
Middleware Conflicts:
auth middleware alongside jwt, ensure the jwt guard is properly configured in auth.php:
'defaults' => [
'guard' => 'jwt',
],
Token Validation:
use Firebase\JWT\JWT;
$decoded = JWT::decode($token, env('JWT_SECRET'), ['HS256']);
JWT_SECRET matches the one used in the package.Logging:
config/jwt.php:
'logging' => true,
Common Errors:
TokenInvalidException: Token is malformed or tampered with.TokenBlacklistedException: Token is revoked (if using a blacklist).JWTException: General JWT library errors (e.g., invalid secret).Custom Claims:
$token = JWTAuth::fromUser($user, [
'custom_claim' => 'value',
]);
$claims = JWTAuth::parseToken()->authenticate()->getPayload();
Token Blacklisting:
// Middleware example
public function handle($request, Closure $next) {
if (JWTAuth::parseToken()->inBlacklist()) {
abort(401, 'Token revoked');
}
return $next($request);
}
Custom User Resolver:
// In JWTAuthServiceProvider
public function register() {
$this->app->bind('auth.provider', function () {
return new CustomUserProvider();
});
}
How can I help you explore Laravel packages today?