wearesho-team / yii2-tokens
Storing tokens (authorization, registration) in redis database.
1.4.0
2026-10-26 13:45 UTC
Requires
- php: >=8.4
- nesbot/carbon: ^3.2 || ^2.73
- ramsey/uuid: ^4.7.6
- yiisoft/yii2: ^2.0.54
- yiisoft/yii2-redis: ^2.0.20
Requires (Dev)
- horat1us/yii2-asset-free: ^1.1
- phpunit/phpunit: ^13.4
- squizlabs/php_codesniffer: ^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-26 13:56:37 UTC
README
Storing tokens (authorization, registration) in redis database.
Compatibility: tested on PHP 8.4, PHP 8.5
Installation
composer require wearesho-team/yii2-tokens:^1.4
Usage
Repository
To store tokens you should use Repository that will receive token entity and generate UUIDv4 hash. Then, you can receive token using generated hash.
<?php use Wearesho\Yii2\Token; $repository = new Token\Repository([ 'redis' => 'redis', // your yii2-redis connection ]); $token = new Token\Entity( $type = 'registration', $tokenOwner = 'foo@bar.com', $value = 'random-token', (new \DateTime())->add(new \DateInterval('PT30M')) // expire after 30 minutes ); $hash = $repository->put($token); // If you want to receive token $token = $repository->get($hash); // entity or null
ValidationBehavior
To validate model attributes, that contains hash, owner and value you should use
validation behavior, instead of validators because three attributes have to be validated
one-time.
{attribute} is invalid. message will be add to attribute errors and validation will fail (beforeValidate event).
<?php use yii\base; use Wearesho\Yii2\Token; class Model extends base\Model { /** @var string */ public $hash; /** @var string */ public $tokenOwner; // or `phone`, `email` etc. /** @var string */ public $token; public function behaviors(): array { return [ 'validateToken' => [ 'class' => Token\ValidationBehavior::class, 'type' => 'registration', ], ]; } public function rules(): array { return [ [['hash', 'tokenOwner', 'token',], 'safe',], // to load using $model->load ]; } } $repository = new Token\Repository; $model = new Model; $model->hash = 'some-invalid-hash'; $model->tokenOwner = 'foo@bar.com'; $model->token = 'some-random-token'; $model->validate(); // false $hash = $repository->put(new Token\Entity( 'registration', $model->tokenOwner, $model->token, (new \DateTime)->add(new \DateInterval('PT1M')) )); $model->hash = $hash; $model->validate(); // true