shahzadbarkati/role-based-jwt-auth
Installation Run:
composer require shahzadbarkati/role-based-jwt-auth
php artisan vendor:publish --provider="ShahzadBarkati\RoleBasedJwtAuth\Providers\JwtAuthServiceProvider" --tag="config"
php artisan vendor:publish --provider="ShahzadBarkati\RoleBasedJwtAuth\Providers\JwtAuthServiceProvider" --tag="migrations"
Run migrations:
php artisan migrate
Configure Auth Guard
Update config/auth.php to include the JWT guard:
'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
First Use Case: Login Endpoint
Send a POST request to /api/auth/login with:
{
"email": "user@example.com",
"password": "password123"
}
Response includes JWT token and user data.
Authentication Flow
Auth::guard('api')->attempt() for manual validation.@auth:api or role:admin for route protection.Role-Based Middleware Apply role checks in routes or controllers:
Route::middleware(['auth:api', 'role:admin'])->group(function () {
// Admin-only routes
});
Or in controllers:
public function __construct() {
$this->middleware('role:user|admin')->only(['index']);
}
Token Management
/api/auth/refresh with the current token./api/auth/logout or manually:
Auth::guard('api')->user()->tokens()->delete();
Password Resets
/api/auth/forgot-password (returns 6-digit code)./api/auth/reset-password (code + new password).Role model or use pivot tables for many-to-many roles.config/jwt-auth.php to revoke tokens on logout.resources/views/vendor/jwt-auth/emails for custom reset emails.Auth::guard('api')->login($user) in tests to simulate auth.Token Invalidation
'invalidate_previous_tokens' => false,
jwt_personal_access_tokens table for orphaned tokens.Role Assignment
user->roles()->attach($roleId).$user->roles()->sync([1, 2]); // Correct
$user->roles()->attach(1); // May not update if using many-to-many
Password Reset Codes
config/jwt-auth.php:
'password_reset' => [
'code_expiry_minutes' => 30,
],
password_reset_tokens table for expired codes.Middleware Conflicts
auth:api middleware, ensure the guard is properly configured in config/auth.php.auth:api explicitly to routes or controllers.Custom Guards Extend the guard for additional logic:
Auth::guard('api')->extend('custom', function ($app) {
return new CustomJwtGuard($app['auth'], $app['request']);
});
Rate Limiting Apply rate limits to auth endpoints:
Route::middleware(['throttle:6,1'])->group(function () {
// Login/refresh routes
});
Logging
Enable debug logging in config/jwt-auth.php:
'debug' => env('APP_DEBUG', false),
Check logs for token generation/validation issues.
Testing Tokens Generate test tokens manually:
$token = Auth::guard('api')->login($user);
Or use the jwt-auth facade:
use ShahzadBarkati\RoleBasedJwtAuth\Facades\JwtAuth;
$token = JwtAuth::attempt(['email' => 'test@example.com', 'password' => 'password']);
Performance
$user->getRoles()->cache();
select queries for token lookups:
$token = Auth::guard('api')->user()->tokens()->select('id')->first();
How can I help you explore Laravel packages today?