Download the PHP package huseynvsal/jwt-auth-refresh without Composer
On this page you can find all versions of the php package huseynvsal/jwt-auth-refresh. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download huseynvsal/jwt-auth-refresh
More information about huseynvsal/jwt-auth-refresh
Files in huseynvsal/jwt-auth-refresh
Package jwt-auth-refresh
Short Description A Laravel package for JWT authentication with access and refresh tokens.
License MIT
Informations about the package jwt-auth-refresh
JWT Auth Refresh Package
This Laravel package provides JWT authentication with access and refresh token functionality. It includes all the necessary logic for token generation, token refresh, and user logout without the need to write any complex controller or middleware.
Features
- JWT Access and Refresh Tokens: Generate access and refresh tokens during login.
- Token Refresh: Refresh tokens if the refresh token is valid.
- Logout: Remove all refresh tokens during logout.
- Custom Guards: Automatically registers a custom JWT guard.
- Configuration: Easily configure token expiration times and secret keys.
Installation
Step 1: Install the Package
Install the package using Composer.
Step 2: Publish Resources
After installing the package, you need to publish the configuration and migration files.
Run the following command to publish the resources:
This will publish:
config/jwt-auth.php
– The configuration file for JWT tokens (secret keys and expiration times).- A migration file to create
refresh_tokens
tables.
Step 3: Run Migrations
Run the migration to create the necessary table for storing refresh tokens.
Step 4: Configure .env
File
In your .env file, add the following configuration for JWT tokens:
JWT_SECRET_KEY
: The secret key used to sign JWT tokens. Ensure you keep it secure.JWT_SECRET_KEY
: The secret key used to sign refresh tokens. Ensure you keep it secure.JWT_ACCESS_TOKEN_EXPIRATION
: The expiration time for access tokens (in seconds).JWT_REFRESH_TOKEN_EXPIRATION
: The expiration time for refresh tokens (in seconds).
Step 5: Set Guard in config/auth.php
Your package will automatically register the custom jwt guard for authentication. The next step is to set it in the config/auth.php file.
Ensure that your config/auth.php is configured to use the jwt guard for API authentication.
This sets the default guard for API requests to use the custom jwt guard provided by the package.
Step 6: Update User Model (Optional)
If you want to use the JWT-based authentication with your own User
model, ensure that your User model implements Illuminate\Contracts\Auth\Authenticatable
and have refreshTokens
relation defined:
Example:
This is required so that Laravel can authenticate the user using the JWT guard.
Usage
Step 1: Login and Generate Tokens
In your AuthController
, you can use the JwtAuthService
to generate both the access and refresh tokens when the user logs in.
Example login method:
Step 2: Refresh Tokens
You can create an API to allow users to refresh their tokens using a valid refresh token.
Example refresh method:
Step 3: Logout and Revoke Tokens
To handle logout, you can delete the user's access and refresh tokens:
Additional Information
- JWT Tokens: The refresh tokens will be stored in the refresh_tokens tables.
- Guard Usage: The jwt guard is automatically registered, and you can use the auth() helper to authenticate users using JWT tokens in your controllers.
- Expiration: Both access and refresh tokens have configurable expiration times defined in the config/jwt-auth.php file.
All versions of jwt-auth-refresh with dependencies
firebase/php-jwt Version ^6.0
illuminate/database Version ^9.0|^10.0|^11.0
illuminate/auth Version ^9.0|^10.0|^11.0
illuminate/support Version ^9.0|^10.0|^11.0
illuminate/contracts Version ^9.0|^10.0|^11.0
illuminate/http Version ^9.0|^10.0|^11.0