PHP code example of innmind / http-authentication

1. Go to this page and download the library: Download innmind/http-authentication 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/ */

    

innmind / http-authentication example snippets


use Innmind\HttpAuthentication\{
    Identity,
    Any,
    ViaBasicAuthorization,
    ViaBasicAuthorization\Resolver as BasicResolver,
    ViaForm,
    ViaForm\Resolver as FormResolver,
};

$auth = bootstrap();
$viaBasicAuthorization = new ViaBasicAuthorization(
    new class implements BasicResolver {
        public function __invoke(string $user, string $password): Identity
        {
            // this info comes from the Authorization header

            // your logic here to authenticate the user
        }
    }
);
$viaForm = new ViaForm(
    new class implements FormResolver {
        public function __invoke(Form $form): Identity
        {
            // your logic here to authenticate the user by inspecting
            // the form, you have access to the whole form data so the
            // library doesn't force you to have specific fields
        }
    }
);
$authenticate = new Any(
    $viaBasicAuthorization,
    $viaForm
);

$identity = $authenticate(/* an instance of Innmind\Http\Message\ServerRequest */)->match(
    static fn($identity) => $identity,
    static fn() => throw new \RuntimeException('Unknown identity'),
);