PHP code example of fivesqrd / una

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

    

fivesqrd / una example snippets


Use Una\Adapter;

/* Initialise the factory class with some defaults */
$factory = new Una\Factory([
    'storage'    => Adapter\Storage\BegoAdapter::instance([
        'table' => 'My-Table',
        'aws'   => [
            'region' => 'eu-west-1',
            'credentials'   => [
                'key'    => 'mykey',
                'secret' => 'mysecret',
            ]
        ],
    ]),
    'randomiser' => new Adapter\Randomiser\Numeric([
        'length' => 5
    ]),
    'hasher'     => new Adapter\Hash\Password()
]);

/* Create a fresh token */
$token = $factory->create()
    ->ttl(86400)
    ->notify(function($secret) {
        mail();    
    });

echo $token->raw();
echo $token->hashed();

$token = $factory->create()
    ->payload($userId)
    ->ttl(86400)
    ->notify(function($secret) {
        mail();    
    });

$token = $factory->create()
    ->payload([
        'userId' => $userId,
        'time'   => time()
    ])
    ->ttl(86400)
    ->notify(function($secret) {
        mail();    
    });

$token = $factory->create(12345)
    ->notify(function($secret) {
        mail();
    });

echo $token->raw(); //12345

echo $token->hashed();

use Una\Adapter\Randomiser\AlphaNumeric;

$token = $factory->create()
    ->randomise(new AlphaNumeric())
    ->notify(function($secret) {
        mail();    
    });

echo $token->raw(); //random alphanumeric value

echo $token->hashed();

use Una\Adapter\Randomiser\AlphaNumeric;

$token = $factory->create()
    ->ttl(86400) //24 hours
    ->notify(function($secret) {
        mail();    
    });

echo $token->raw(); //random alphanumeric value

echo $token->hashed();