PHP code example of krubio / perfect-authentication
1. Go to this page and download the library: Download krubio/perfect-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/ */
krubio / perfect-authentication example snippets
erfectApp\Auth\PasswordHasher;
use PerfectApp\Auth\AuthenticationService;
// Initialize services
$passwordHasher = new PasswordHasher();
$authService = new AuthenticationService($passwordHasher);
// Hash a password
$hashedPassword = $authService->hashPassword('securepassword123');
// Verify credentials
if ($authService->verifyCredentials('securepassword123', $hashedPassword)) {
echo "Authentication successful!";
}
// Check if rehashing needed
if ($authService->needsRehash($hashedPassword)) {
$newHash = $authService->hashPassword('securepassword123');
}
use PerfectApp\Auth\AuthenticationService;
use PerfectApp\Auth\PasswordHasher;
use PDO;
// Setup
$pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
$authService = new AuthenticationService(new PasswordHasher());
// User registration
function registerUser($pdo, $authService, $username, $password) {
$hash = $authService->hashPassword($password);
$stmt = $pdo->prepare('INSERT INTO users (username, password_hash) VALUES (?, ?)');
return $stmt->execute([$username, $hash]);
}
// User login
function loginUser($pdo, $authService, $username, $password) {
$stmt = $pdo->prepare('SELECT password_hash FROM users WHERE username = ?');
$stmt->execute([$username]);
$user = $stmt->fetch();
return $user && $authService->verifyCredentials($password, $user['password_hash']);
}