<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class BasicAdminAuth
{
    public function handle(Request $request, Closure $next): Response
    {
        $user = (string) env('ADMIN_USER', 'admin');
        $pass = (string) env('ADMIN_PASS', 'admin');

        $givenUser = (string) $request->getUser();
        $givenPass = (string) $request->getPassword();

        if ($givenUser !== $user || $givenPass !== $pass) {
            return response('Unauthorized', 401, ['WWW-Authenticate' => 'Basic']);
        }

        return $next($request);
    }
}
