1. Go to this page and download the library: Download betterauth/multimodal-php 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/ */
betterauth / multimodal-php example snippets
use BetterAuth\Core\AuthManager;
use BetterAuth\Core\SessionAuthManager;
use BetterAuth\Core\SessionService;
use BetterAuth\Core\PasswordHasher;
use BetterAuth\Core\Config\AuthConfig;
use BetterAuth\Storage\Pdo\PdoUserRepository;
use BetterAuth\Storage\Pdo\PdoSessionRepository;
// The bundled PDO storage (createTable / upserts) targets MySQL/MariaDB.
// For SQLite or PostgreSQL, provide your own storage implementation.
$pdo = new PDO('mysql:host=127.0.0.1;dbname=auth', 'user', 'pass');
$config = AuthConfig::forMonolith('your-secret-key-min-32-chars');
$users = new PdoUserRepository($pdo);
$sessions = new PdoSessionRepository($pdo);
$hasher = new PasswordHasher();
$service = new SessionService($sessions, $config);
$sessionAuth = new SessionAuthManager($users, $service, $hasher);
$auth = new AuthManager($config, sessionAuth: $sessionAuth);
// Sign up
$result = $auth->signUp('[email protected]', 'SecureP@ss2024!', 'Alice');
// Sign in
$result = $auth->signIn('[email protected]', 'SecureP@ss2024!');
// $result = ['user' => UserDto, 'session' => Session]
// Validate session
$user = $auth->validateSession($result['session']->getToken());
use BetterAuth\Core\AuthManager;
use BetterAuth\Core\TokenAuthManager;
use BetterAuth\Core\TokenService;
use BetterAuth\Core\PasswordHasher;
use BetterAuth\Core\Config\AuthConfig;
use BetterAuth\Storage\Pdo\PdoUserRepository;
use BetterAuth\Storage\Pdo\PdoRefreshTokenRepository;
// The bundled PDO storage (createTable / upserts) targets MySQL/MariaDB.
// For SQLite or PostgreSQL, provide your own storage implementation.
$pdo = new PDO('mysql:host=127.0.0.1;dbname=auth', 'user', 'pass');
$config = AuthConfig::forApi('your-secret-key-min-32-chars');
$users = new PdoUserRepository($pdo);
$refresh = new PdoRefreshTokenRepository($pdo);
$hasher = new PasswordHasher();
$signer = new TokenService($config);
$tokenAuth = new TokenAuthManager($users, $refresh, $signer, $hasher, $config);
$auth = new AuthManager($config, tokenAuth: $tokenAuth);
// Sign in
$result = $auth->signIn('[email protected]', 'SecureP@ss2024!');
// $result = ['user' => UserDto, 'access_token' => '...', 'refresh_token' => '...', 'expires_in' => 3600]
// Refresh tokens (atomic rotation)
$newTokens = $auth->refresh($result['refresh_token']);
use BetterAuth\Core\Plugin\PluginManager;
use BetterAuth\Core\Plugin\PluginInterface;
use BetterAuth\Core\Plugin\PluginContext;
class AuditPlugin implements PluginInterface
{
public function getName(): string { return 'audit'; }
public function getVersion(): string { return '1.0.0'; }
public function getDependencies(): array { return []; }
public function isEnabled(): bool { return true; }
public function getConfig(): array { return []; }
public function install(PluginContext $context): void
{
$context->registerHook('user.logged_in', function (array $data) {
// log login event
});
}
}
$plugins = new PluginManager();
$plugins->register(new AuditPlugin());
$plugins->loadAll();
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.