PHPackages                             shahzadbarkati/role-based-jwt-auth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. shahzadbarkati/role-based-jwt-auth

ActiveLaravel-package

shahzadbarkati/role-based-jwt-auth
==================================

Role based JWT authentication for Laravel 12 with forgot and reset password

v1.1.7(7mo ago)09MITPHPPHP ^8.2

Since Oct 10Pushed 7mo agoCompare

[ Source](https://github.com/ShahzadBarkati/jwt-auth-package)[ Packagist](https://packagist.org/packages/shahzadbarkati/role-based-jwt-auth)[ RSS](/packages/shahzadbarkati-role-based-jwt-auth/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

JWT Auth Package for Laravel
============================

[](#jwt-auth-package-for-laravel)

A lightweight, role-based JWT authentication package for Laravel 12. Built with SOLID principles, it provides secure API endpoints for login, logout, token refresh, profile management, password reset (with email codes), and password updates. Tokens are invalidated on new logins/refreshes for enhanced security.

Features
========

[](#features)

- JWT Authentication: Powered by `tymon/jwt-auth` with configurable TTL and blacklisting.
- Role-Based Access: Middleware for roles like admin, user. Supports many-to-many relationships.
- Token Management: Auto-invalidate previous tokens (configurable max per user).
- Password Handling: Forgot/reset with 6-digit codes (10-min expiration, customizable) via email.
- Endpoints: Login, logout, refresh, profile, forgot-password, reset-password, update-password.
- Easy Integration: Uses existing users table; optional migrations for roles/tokens.
- Configurable: Full config for TTLs, guards, emails, routes, etc.

Requirements
============

[](#requirements)

- PHP 8.2+
- Laravel 12.0+
- Database (MySQL/PostgreSQL/SQLite) for tokens/resets/roles.
- Mail configuration for password resets (e.g., SMTP).

Installation
============

[](#installation)

#### 1. Require the Package

[](#1-require-the-package)

```
composer require shahzadbarkati/role-based-jwt-auth
```

This auto-registers the service provider via Laravel's package discovery.

#### 2. Publish Assets

[](#2-publish-assets)

Publish the config, migrations, views, and trait:

```
php artisan vendor:publish --provider="ShahzadBarkati\RoleBasedJwtAuth\Providers\JwtAuthServiceProvider" --tag="jwt-auth"
```

This creates:

- `config/jwt-auth.php`: Core configuration.
- `database/migrations/`: Migrations for roles, pivot, and JWT tokens.
- `resources/views/vendor/jwt-auth/emails/password-reset.blade.php`: Email template.
- `app/Traits/HasRoles.php`: Trait for your User model.

#### 3. Generate JWT Secret

[](#3-generate-jwt-secret)

Run the command to auto-generate and add `JWT_SECRET` (and suggested TTLs) to your `.env`:

```
php artisan jwt:secret
```

Example output:

```
textJWT_SECRET=base64:
Also set JWT_TTL=60
JWT_REFRESH_TTL=20160
PASSWORD_RESET_TTL=10
JWT_BLACKLIST_ENABLED=true

```

#### 4. Run Migrations

[](#4-run-migrations)

The package prompts for migrations on first boot (in console). If skipped, run manually: Check if Migrations Are Needed:

If you have an existing users table: ✅ (package uses it).
For roles/tokens: New tables (roles, role\_user, jwt\_tokens).

#### Interactive Prompt (Recommended):

[](#interactive-prompt-recommended)

```
php artisan jwt-auth:migrate-prompt
```

- This asks: "Run migrations for roles and tokens? (Y/N)"
- Y: Automatically runs php artisan migrate (adds tables without overwriting existing data).
- N: Skip; proceed to manual run below.

#### Manual Run (Alternative):

[](#manual-run-alternative)

```
php artisan migrate
```

#### Optional: Add Indexes (for performance):

[](#optional-add-indexes-for-performance)

Run these after migration:

```
-- In your DB tool
ALTER TABLE jwt_tokens ADD INDEX idx_user_id (user_id);
ALTER TABLE jwt_tokens ADD INDEX idx_jti (jti);
ALTER TABLE jwt_tokens ADD INDEX idx_expires_at (expires_at);
ALTER TABLE password_resets ADD INDEX idx_email (email);
```

#### 5. Configure Your User Model

[](#5-configure-your-user-model)

Add the published `HasRoles` trait and JWT settings to `app/Models/User.php`:

```
