PHP code example of unisharp / laravel-jwt

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

    

unisharp / laravel-jwt example snippets


Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
UniSharp\JWT\JWTServiceProvider::class,

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

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
php artisan vendor:publish --provider="UniSharp\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();