PHP code example of devraeph / laravel-xtoken

1. Go to this page and download the library: Download devraeph/laravel-xtoken library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devraeph / laravel-xtoken example snippets



namespace App\Models;
use DevRaeph\XToken\Traits\HasXToken;

class User extends Authenticatable
{
    use HasFactory, HasXToken;

    ...
}


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

/*
 * Use exsisting `xToken` middleware
 */
Route::group(['middleware' => ['api','xToken']], function () {
    Route::post("verify",function (){
        echo "Checked Token -> Valid";
    });
});

use App\Models\User;
use DevRaeph\XToken\Facades\TokenizerClaim;

$myClaim = TokenizerClaim::setRequest($request)
    ->get();
    
$myUser = User::whereId($myClaim)->first();
shell
php artisan migrate
shell
php artisan vendor:publish --provider="DevRaeph\XToken\JWTTokenServiceProvider" --tag="config"