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.

1.1.0 2024-06-24 07:18 UTC

This package is auto-updated.

Last update: 2025-01-26 07:48:34 UTC


README

Packagist Dependency Version Total Downloads Latest Version on Packagist Packagist License

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.