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
Requires
- php: ^7.3|^8.0
Requires (Dev)
- orchestra/testbench: ^6.0
This package is not auto-updated.
Last update: 2025-03-09 20:35:28 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());