PHP code example of tihloh / prefab-auth

1. Go to this page and download the library: Download tihloh/prefab-auth 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/ */

    

tihloh / prefab-auth example snippets


use Tihloh\Prefab\Auth\Services\AuthManager;

$auth = new AuthManager();

$result = $auth->attempt(
    $_POST['identifier'] ?? '',
    $_POST['password'] ?? '',
);

if ($result->success) {
    // Authentication succeeded.
}

$auth = new AuthManager($userProvider);

$auth->check();
$auth->id();
$auth->user();
$auth->logout();

if ($auth->check()) {
    $user = $auth->user();
    echo 'Welcome ' . $user->name;
}

PrefabConfig::set([
    'session' => [
        'namespace' => 'company_sso',
    ],
]);

PrefabConfig::set([
    'session' => [
        'namespace' => 'company_sso',
        'name' => 'COMPANY_SESSION',
        'path' => '/',
        'domain' => '.example.com',
        'secure' => true,
        'httponly' => true,
        'samesite' => 'Lax',
        'lifetime' => 0,
    ],
]);

use Tihloh\Prefab\PrefabConfig;

PrefabConfig::set([
    'modules' => [
        'users' => [
            'identifiers' => ['email', 'username'],
        ],
    ],
]);

$map = new UserMap(
    table: 'users',
    email: 'email',
    attributes: [
        'username' => 'user',
    ],
);

$result = $auth->attempt(
    $_POST['identifier'] ?? '',
    $_POST['password'] ?? '',
);

$auth = new AuthManager();
$result = $auth->attempt($email, $password);

$result = $auth->attempt($identifier, $password);

if (!$result->success) {
    // The host application decides how to respond.
}

$result = $auth->attempt($identifier, $password, [
    'ip_address' => $_SERVER['REMOTE_ADDR'] ?? null,
    'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null,
]);

$registry->register($googleProvider);
$registry->register($githubProvider);

$url = $social->authorizationUrl('google');
header('Location: ' . $url);
exit;

$result = $social->callback('google', $_GET, [
    'ip_address' => $_SERVER['REMOTE_ADDR'] ?? null,
    'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null,
]);

try {
    $result = $auth->attempt($email, $password);
} catch (RuntimeException $e) {
    // Your application chooses HTML, JSON, redirect or logging.
}

print_r($auth->explain());