PHP code example of vinkla / shield

1. Go to this page and download the library: Download vinkla/shield library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vinkla / shield example snippets


'basic' => [
    'user' => env('BASIC_AUTH_USER'),
    'password' => env('BASIC_AUTH_PASSWORD'),
],



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);
    }
}

use App\Http\Middleware\BasicAuthMiddleware;
use App\Models\User;

Route::get('api/users', function () {
    return User::all();
})->middleware(BasicAuthMiddleware::class);