devraeph / laravel-xtoken
This Package creates a JWTToken for the Application and provides a middelware for Auth
Installs: 1 103
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- hisorange/browser-detect: ^4.3
- lcobucci/jwt: 4.1.5
README
DevRaeph / laravel-xtoken
A simple package with which you can issue JWT tokens and also verify them. Usable for multiple models, thanks to existing trait.
Installation
Package is available on Packagist, you can install it using Composer.
composer require devraeph/laravel-xtoken
Migrate Database for token table
php artisan migrate
Publish Config
php artisan vendor:publish --provider="DevRaeph\XToken\JWTTokenServiceProvider" --tag="config"
Documentation
Model-Trait HasXToken
Example:
namespace App\Models; use DevRaeph\XToken\Traits\HasXToken; class User extends Authenticatable { use HasFactory, HasXToken; ... }
Issuing a Token:
use DevRaeph\XToken\Facades\Tokenizer; use Carbon\CarbonImmutable; $myJWT = Tokenizer::setModel(/* MODEL eg USER */) ->setIssuedBy(/* Default is env("APP_URL") */) ->setExpiresAt(CarbonImmutable::now()->addDays(15)) ->createToken(); return $myJWT->toArray(); //Return Token Array return $myJWT->toJson(); //Return Token Json
Using Middelware in Routes to parse Token:
/* * Use exsisting `xToken` middleware */ Route::group(['middleware' => ['api','xToken']], function () { Route::post("verify",function (){ echo "Checked Token -> Valid"; }); });
Get TokenClaim -> ID of Model
use App\Models\User; use DevRaeph\XToken\Facades\TokenizerClaim; $myClaim = TokenizerClaim::setRequest($request) ->get(); $myUser = User::whereId($myClaim)->first();