PHP code example of echo-fusion / authmanager

1. Go to this page and download the library: Download echo-fusion/authmanager 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/ */

    

echo-fusion / authmanager example snippets


return [
    'default' => 'web',

    'guards' => [
        'web' => ['driver' => 'session', 'provider' => 'users', 'key' => 'auth_user'],
        'api' => ['driver' => 'token',   'provider' => 'users',
                  'header' => 'Authorization', 'scheme' => 'Bearer',   // Authorization: Bearer <token>
                  'query' => 'api_token'],                              // optional ?api_token=<token>
        'internal' => ['driver' => 'token', 'provider' => 'users',
                       'header' => 'X-Api-Key', 'scheme' => null],      // bare token in a custom header
        'custom' => ['class' => MyGuard::class, 'provider' => 'users'], // new MyGuard($provider, $definition, $container)
    ],

    'providers' => [
        'users' => ['driver' => 'database', 'table' => 'users', 'id' => 'id', 'password' => 'password', 'token' => 'api_token'],
        'staff' => ['driver' => 'database', 'connection' => 'staff_db', 'table' => 'staff'],   // a named connection
        'fixed' => ['driver' => 'array', 'users' => [['id' => 1, 'email' => '[email protected]', 'password' => '<hash>']]],
        'ldap'  => ['class' => LdapProvider::class],                    // new LdapProvider($definition, $container)
    ],

    'hasher' => ['algorithm' => 'bcrypt', 'options' => ['cost' => 12]], // or 'argon2id'

    'middleware' => [
        'authenticate' => ['guard' => 'web', 'redirect_to' => '/login'],
        'guest'        => ['guard' => 'web', 'redirect_to' => '/'],
    ],
];

use EchoFusion\AuthManager\AuthManager;

$auth = new AuthManager($container, $config);   // $container: any PSR-11 container, see "Requirements of the container"

$web = $auth->statefulGuard('web');

if ($web->attempt(['email' => $email, 'password' => $password])) {
    // logged in: the session id was regenerated and the user id stored
}

$web->check();      // bool
$web->guest();      // bool
$web->id();         // the user id, or null
$web->user();       // UserInterface or null
$web->logout();

$web->login($user); // log a user in without checking a password (e.g. right after registration)

/** @var \EchoFusion\AuthManager\Users\DatabaseUserProvider $users */
$users = $auth->provider('users');
$user = $users->create(['name' => 'Ann', 'email' => '[email protected]', 'password' => $plainPassword]);   // the password is hashed
$web->login($user);

$api = $auth->guard('api');      // reads the token from the request, so give it one:
$api->setRequest($request);      // the middleware below does this for you
$api->user();

$token = bin2hex(random_bytes(32));
$connection->execute('UPDATE users SET api_token = ? WHERE id = ?', [hash('sha256', $token), $userId]);
// give $token to the client once; it cannot be recovered from the database

use EchoFusion\AuthManager\Middleware\AuthenticateMiddleware;
use EchoFusion\AuthManager\Middleware\GuestMiddleware;

new AuthenticateMiddleware($auth, $responseFactory, 'web', '/login');   // guard name, redirect target
new GuestMiddleware($auth, $responseFactory, 'web', '/');
bash
composer