PHP code example of janfish / auth

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

    

janfish / auth example snippets


$auth = Janfish\Auth\Auth::getInstance([
    'class' => MyIdentity::class,
    'type' => 'jwt',
    'setting' => [
        'alg' => 'HS256',
        'secret' => '123123123',
     ],
]);
$token = $auth->generateToken(1, ['name' => 'username']);

try{
    if ($auth->authorize($token) === false) {
        echo '失败';
        die();
    }
    print_r([
        $auth->getIdentity(),
        $auth->getExtendedData(),
    ]);
}catch (\Janfish\Auth\Exception\ExpiredException $e){
     echo '超时了' . PHP_EOL;
     die();
}catch (\Exception $e){
     echo '失败ma' . PHP_EOL;
     die();
}



$auth = Janfish\Auth\Auth::getInstance([
    'class' => MyIdentity::class,
    'type' => 'basic'
]);
$token = $auth->generateToken('zeng444', 'password');
if ($auth->authorize($token) === false) {
    echo '失败';
    die();
}
print_r([
    $auth->getIdentity(),
    $auth->getExtendedData(),
]);