Simple tool to authenticate a request.
composer require innmind/http-authenticationuse Innmind\HttpAuthentication\ViaBasicAuthorization;
use Innmind\Router\{
Router,
Component,
Collect,
Handle,
};
use Innmind\Http\{
ServerRequest,
Response,
};
use Innmind\Immutable\Attempt;
function login(string $user, string $password): Attempt
{
// find the user
}
$router = Router::of(
Component::of(ViaBasicAuthorization::of(
static fn(string $user, string $password) => login($user, $password),
))
->map(Collect::of('user'))
->pipe(Handle::of(
static fn(ServerRequest $request, mixed $user) => Attempt::result(
Response::of(
// build response
),
),
));
);
$response = $router(/* an instance of Innmind\Http\ServerRequest */)->unwrap();