vinkla / shield
A HTTP basic auth middleware for Laravel
Installs: 318 253
Dependents: 1
Suggesters: 0
Security: 0
Stars: 227
Watchers: 4
Forks: 25
Open Issues: 0
Requires
- php: ^8.1
- illuminate/support: ^10.0
- symfony/http-kernel: ^6.2
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
README
This package used to be an HTTP basic auth middleware for Laravel. However, it is now deprecated as I no longer use it personally. Laravel already has built-in basic auth support for their web guard.
If you need simpler basic auth for your API, instead of depending on a third-party library, you can add it yourself. Please follow the guide below.
To begin, update your .env
file with the following details:
BASIC_AUTH_USER=your-username BASIC_AUTH_PASSWORD=your-password
Modify your config/auth.php
file as follows:
'basic' => [ 'user' => env('BASIC_AUTH_USER'), 'password' => env('BASIC_AUTH_PASSWORD'), ],
Finally, create a middleware that resembles the code below:
<?php declare(strict_types=1); namespace App\Http\Middleware; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; class BasicAuthMiddleware { public function handle(Request $request, Closure $next): Response { $user = config('auth.basic.user'); $password = config('auth.basic.password'); if ( $request->getUser() !== $user || $request->getPassword() !== $password ) { throw new UnauthorizedHttpException('Basic'); } return $next($request); } }
That's it! You can now use the middleware in your routes.
use App\Http\Middleware\BasicAuthMiddleware; use App\Models\User; Route::get('api/users', function () { return User::all(); })->middleware(BasicAuthMiddleware::class);