usmonaliyev / env-auth
The package provides middleware for Laravel applications to handle Basic Authentication and Secret Key Authentication using credentials stored in the .env file.
Requires
- php: ^7.4|^8.0
- illuminate/support: ^9.0|^10.0|^11.0
Requires (Dev)
- laravel/pint: ^1.16
- phpunit/phpunit: ^9.0
README
The package provides middleware for Laravel applications to handle Basic Authentication and Secret Key Authentication using credentials stored in the .env
file.
Installation
You can install the package via composer:
composer require usmonaliyev/env-auth
Publish
Publish the package configuration:
php artisan vendor:publish --provider="Usmonaliyev\EnvAuth\EnvAuthServiceProvider"
Configuration
Add the necessary credentials to your .env
file.
If you want to modify keys of variables, you need to change config/env-auth.php
after publish config file.
For BasicEnv
middleware:
BASIC_USERNAME=your-username BASIC_PASSWORD=your-password
For SecretEnv
middleware:
AUTH_SECRET_KEY=your-secret-key
Usage
Register middleware
If you are using Laravel 11.x check out this link: https://laravel.com/docs/11.x/middleware#middleware-aliases
In your Laravel application's app/Http/Kernel.php
file, register the new middleware classes:
protected $routeMiddleware = [ // Other middleware 'basic.env' => \Usmonaliyev\EnvAuth\Middleware\BasicEnv::class, 'secret.env' => \Usmonaliyev\EnvAuth\Middleware\SecretEnv::class, ];
Protect routes
Apply the middleware to your routes in routes/web.php
or routes/api.php
.
Route::middleware('basic.env')->group(function () { ... });
Testing
Use the following cURL command to test Basic Authentication:
curl -u your-username:your-password http://your-app-url/basic-protected-route
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.