PHP code example of popphp / pop-auth

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

    

popphp / pop-auth example snippets


use Pop\Auth;

$auth = new Auth\File('/path/to/.htmyauth');

if ($auth->authenticate('admin', 'password')) {
    // User is authenticated
} else {
    // Handle failed authentication attempt
}

var_dump($auth->isAuthenticated()); // Returns bool

use Pop\Auth;

$auth = new Auth\File('/path/to/.htmyauth');

try {
    if ($auth->authenticate('admin', 'password')) {
        // User is authenticated
    } else {
        // Credentials didn't match
    }
} catch (Auth\Exception $e) {
    // The adapter couldn't perform the check at all
}

use Pop\Auth;

$auth = new Auth\File('/path/to/.htmyauth');
$auth->authenticate('testuser1', 'password'); // Returns int

if ($auth->isAuthenticated()) { } // Returns bool

use Pop\Auth;

$auth = new Auth\File('/path/to/.htmyauth', 'example.com');
$auth->authenticate('testuser1', 'password'); // Returns int

use Pop\Auth\Jwt;

$auth = new Jwt('HS256', $secret);
$auth->authenticate($token); // Returns int

if ($auth->isAuthenticated()) { } // Returns bool

use Pop\Auth\Jwt;

$auth = new Jwt('RS256', $publicKeyPem);
$auth->authenticate($token); // Returns int

use Pop\Auth\Jwt;

$auth = new Jwt('HS256', $secret);
$auth->setAudience('my-api')
    ->setIssuer('https://auth.example.com')
    ->setLeeway(30);

$auth->authenticate($token); // Returns int

use Pop\Auth\Jwt;

$auth = new Jwt('HS256', $secret);
$auth->authenticate($token); // Returns int

if ($auth->isAuthenticated()) {
    $claims = $auth->getUser();
}

use Pop\Auth;

$auth = new Auth\File('/path/to/.htmyauth');
$auth->authenticate('admin', 'password');

if ($auth->isAuthenticated() && $auth->needsRehash()) {
    $newHash = password_hash('password', PASSWORD_DEFAULT);
    // persist $newHash into the file/database yourself — pop-auth never writes to storage
}