PHP code example of firehed / auth

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

    

firehed / auth example snippets



use Firehed\Auth;
use Firehed\JWT;
use Firehed\Security\Secret;

// General setup
$keys = new JWT\KeyContainer();
$keys->addKey('20130101',
    JWT\Algorithm::HMAC_SHA_256(),
    new Secret('some randomly-generated secret'));

$auth = new Auth\Auth();
$auth->setKeys($keys)
    ->setLoader(function($uid): Auth\Authable {
        return (new User())->find($uid);
    });

// Authenticating a user
$user = User::findByEmail($_POST['email']);
$password = new Auth\Factors\KnowledgeFactor(new Secret($_POST['password'));
$auth->setUser($user);
try {
    $auth->validateFactor($password);
    setcookie('auth_token',
        $auth->getEncodedToken(),
        time()+(86400*90),
        '/',
        'yourdomain.com',
        true,
        true);
} catch (Auth\Exceptions\AuthException $e) {
    // password was incorrect
}

// Accessing a previously-authenticated user
try {
    $user = $auth->setEncodedToken($_COOKIE['auth_token'])
        ->setRequiredLevel(Auth\Level::LOGIN())
        ->getUser();
} catch (Auth\Exceptions\AuthException $e) {
    // Authentication failed, prompt for login
    header('Location: /login');
}

function($uid): Firehed\Auth\Authable