PHP code example of sokkian / simpleauth
1. Go to this page and download the library: Download sokkian/simpleauth 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/ */
sokkian / simpleauth example snippets
/**
* SimpleAuth Autoloader
*
* PSR-4 autoloader for manual installation
*/
spl_autoload_register(function ($class) {
$prefix = 'SimpleAuth\\';
$base_dir = __DIR__ . '/src/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
// Autoload (works for both Composer and manual installation)
if (file_exists('vendor/autoload.php')) {
ost=localhost;dbname=mydb', 'user', 'pass');
// Generate token for user ID 1
$tokenGenerator = new Token($db);
$token = $tokenGenerator->generate(1, 900); // 900 seconds = 15 minutes
// Build magic link
$magicLink = 'https://example.com/auth/verify.php?t=' . $token;
// Send via email
mail('[email protected] ', 'Login Link', "Click to login: $magicLink");
// Autoload (works for both Composer and manual installation)
if (file_exists('vendor/autoload.php')) {
t=localhost;dbname=mydb', 'user', 'pass');
$verifier = new Verifier($db, 120); // 120 seconds clock skew tolerance
$token = $_GET['t'] ?? '';
$result = $verifier->verify($token);
if ($result->isOk()) {
$_SESSION['user_id'] = $result->getUserId();
header('Location: /dashboard.php');
exit;
} else {
echo 'Error: ' . $result->getReason();
}
// Autoload (works for both Composer and manual installation)
if (file_exists('vendor/autoload.php')) {
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
if (!$email) {
die('Invalid email address');
}
try {
$db = new PDO('mysql:host=localhost;dbname=mydb', 'user', 'pass');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Find user
$stmt = $db->prepare("SELECT id, name FROM users WHERE email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if (!$user) {
// Don't reveal if user exists
$message = 'If an account exists, you will receive a login link.';
} else {
// Generate token
$tokenGenerator = new Token($db);
$token = $tokenGenerator->generate($user['id'], 900);
// Build magic link
$magicLink = 'http://localhost/test-verify.php?t=' . $token;
// For testing: display link instead of sending email
$message = 'Magic link (for testing): <a href="' . $magicLink . '">' . $magicLink . '</a>';
}
} catch (Exception $e) {
die('Error: ' . $e->getMessage());
}
}
// Autoload (works for both Composer and manual installation)
if (file_exists('vendor/autoload.php')) {
';
if (empty($token)) {
die('No token provided');
}
try {
$db = new PDO('mysql:host=localhost;dbname=mydb', 'user', 'pass');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$verifier = new Verifier($db, 120);
$result = $verifier->verify($token);
if ($result->isOk()) {
$_SESSION['user_id'] = $result->getUserId();
$success = true;
} else {
$error = $result->getReason();
}
} catch (Exception $e) {
die('Error: ' . $e->getMessage());
}
session_start();
session_destroy();
$token = $tokenGenerator->generate(123, 600); // 10 minutes
$stats = $tokenGenerator->cleanup(4);
echo "Deleted {$stats['tokens_deleted']} tokens";
$result = $verifier->verify($token);
if ($result->isOk()) {
$userId = $result->getUserId();
}
return [
'token_expired' => 'Tu enlace ha caducado. Solicita uno nuevo.',
// Only override what you need
];
// Autoload
if (file_exists('vendor/autoload.php')) {
en;
$db = new PDO('mysql:host=localhost;dbname=mydb', 'user', 'pass');
$tokenGenerator = new Token($db);
$stats = $tokenGenerator->cleanup(4);
echo "Cleanup completed: {$stats['tokens_deleted']} tokens, {$stats['nonces_deleted']} nonces deleted\n";
your-project/
├── src/
│ ├── Token.php
│ ├── Verifier.php
│ ├── Result.php
│ └── locales/
│ ├── en_US.php
│ ├── es_ES.php
│ └── it_IT.php
└── autoload.php
bash
0 2 * * * /usr/bin/php /path/to/cleanup.php