LaravelPackages.net
Acme Inc.
Toggle sidebar
nanuc/laravel-tokenable

11
0
1.0.0
About nanuc/laravel-tokenable

nanuc/laravel-tokenable is a Laravel package. It currently has 0 GitHub stars and 11 downloads on Packagist (latest version 1.0.0). Install it with composer require nanuc/laravel-tokenable. Discover more Laravel packages by nanuc or browse all Laravel packages to compare alternatives.

Last updated

This package lets you add tokens to models.

Installation

composer require nanuc/laravel-tokenable

Usage

Add the HasTokens trait to your model.

Use the following methods:

$model->generateToken($lifetime, $type = null, $tokenGenerator = null);
$model->invalidateTokens($type);
$model::byToken($token, $type);

Lifetime is in seconds.

Token Generators

You can create your own token generators. They must extend Nanuc\LaravelTokenable\Generators\BaseTokenGenerator and implement the method generate.

This is the default token generator:

class NumericTokenGenerator extends BaseTokenGenerator
{
    protected $length;

    public function __construct($length = 4)
    {
        $this->length = $length;
    }

    protected function generate()
    {
        $start = 10 ** ($this->length - 1);
        $end = 10 * $start - 1;

        return rand($start, $end);
    }
}

Use your token generator like:

$yourModel->generateToken(60, 'a-type', new YourTokenGenerator());
Comments