vuer / token
A tool for easy public token implementation in some common tasks: password reset, account activation etc.
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 241
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 1
Open Issues: 0
Type:utils
This package is not auto-updated.
Last update: 2019-07-08 03:14:53 UTC
README
Installation
You can install this package via composer using this command:
composer require vuer/token
Next, you must install the service provider:
// config/app.php 'providers' => [ ... Vuer\Token\Providers\TokenServiceProvider::class, ];
Publish migration:
php artisan vendor:publish --provider="Vuer\Token\Providers\TokenServiceProvider"
After the migration has been published you can create the token-table by running the migrations:
php artisan migrate
Usage
Preparing your model
To associate token with a model, the model must implement the following trait:
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Vuer\Token\Traits\Tokenable; class User extends Model { use Tokenable; ... }
Generating token
You can associate a token with a model like this:
$user = User::find(1); $token = $user->createToken('token_name');
To specify own expiration time or token length you should add second and third parameters:
// 180 minutes, 100 characters. $token = $user->createToken('token_name', 180, 100);
You can also pass an array with custom properties:
$token = $user->createToken('token_name', 180, 100, ['email' => 'john@doe.com']);
To check token you can use checkToken method:
if ($user->checkToken('tc0kml61DT3t6xciInw7gjqwmfvZ2799max7lMMGWl2yL9TB')) { // token is valid }
You can also delete token:
// Delete token by name. $user->deleteToken('token_name'); // Delete token by value. $user->deleteToken('tc0kml61DT3t6xciInw7gjqwmfvZ2799max7lMMGWl2yL9TB', 'token');
To get Token instance use methods:
// Get one token by name. $user->getToken('token_name'); // Get collection of tokens by name. $user->getTokens('token_name'); // Get one token by value. $user->getToken('tc0kml61DT3t6xciInw7gjqwmfvZ2799max7lMMGWl2yL9TB', 'token'); // Get collection of tokens by value. $user->getTokens('tc0kml61DT3t6xciInw7gjqwmfvZ2799max7lMMGWl2yL9TB', 'token'); // Get last token by value. $user->getLastToken('tc0kml61DT3t6xciInw7gjqwmfvZ2799max7lMMGWl2yL9TB', 'token'); // Get last token by name. $user->getLastToken('token_name');