PHP code example of clystnet / laravel-jwt

1. Go to this page and download the library: Download clystnet/laravel-jwt 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/ */

    

clystnet / laravel-jwt example snippets


PHPOpenSourceSaver\JWTAuth\Providers\LaravelServiceProvider::class,
Clystnet\JWT\JWTServiceProvider::class,

'JWTAuth' => 'PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth',
'JWTFactory' => 'PHPOpenSourceSaver\JWTAuth\Facades\JWTFactory'

php artisan vendor:publish --provider="PHPOpenSourceSaver\JWTAuth\Providers\JWTAuthServiceProvider"
php artisan vendor:publish --provider="Clystnet\JWT\JWTServiceProvider"

$ php artisan jwt:secret
 php
Route::get('api/content', ['middleware' => 'laravel.jwt', 'uses' => 'ContentController@content']);
 php


namespace App\Http\Controllers;

class ContentController extends Controller
{
    public function __construct() 
    {
        $this->middleware('laravel.jwt');
    }
}
 php
// This will attempt to authenticate the user using the credentials passed and returns a JWT Auth Token for subsequent requests.
$token = Auth::attempt(['email' => '[email protected]', 'password' => '123456']);
 php
if(Auth::onceUsingId(1)) {
    // Do something with the authenticated user
}
 php
if(Auth::once(['email' => '[email protected]', 'password' => '123456'])) {
    // Do something with the authenticated user
}
 php
if(Auth::validate(['email' => '[email protected]', 'password' => '123456'])) {
    // Credentials are valid
}
 php
if(Auth::check()) {
    // User is authenticated
}
 php
if(Auth::guest()) {
    // Welcome guests!
}
 php
Auth::logout(); // This will invalidate the current token and unset user/token values.
 php
$token = Auth::generateTokenById(1);

echo $token;
 php
$user = Auth::user();
 php
$user = Auth::setToken('YourJWTAuthToken')->user();
 php
$userId = Auth::id();
 php
$token = Auth::refresh();
 php
Auth::setToken('ExpiredToken')->refresh();
 php
$forceForever = false;
Auth::invalidate($forceForever);
 php
$forceForever = false;
Auth::setToken('TokenToInvalidate')->invalidate($forceForever);
 php
$token = Auth::getToken(); // Returns current token passed in request.
 php
$payload = Auth::getPayload();
 php
$payload = Auth::setToken('TokenToGetPayload')->getPayload();