nanuc/laravel-tokenable

There is no license information available for the latest version (1.0.0) of this package.

1.0.0 2021-01-30 04:13 UTC

This package is not auto-updated.

Last update: 2024-04-21 16:50:40 UTC


README

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());