PHP code example of irazasyed / jwt-auth-guard

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

    

irazasyed / jwt-auth-guard example snippets


Irazasyed\JwtAuthGuard\JwtAuthGuardServiceProvider::class
 php
$app->register(Irazasyed\JwtAuthGuard\JwtAuthGuardServiceProvider::class);
 php
Route::get('api/content', ['middleware' => 'auth:api', 'uses' => 'ContentController@content']);
 php


namespace App\Http\Controllers;

class ContentController extends Controller
{
    public function __construct() 
    {
        $this->middleware('auth:api');
    }
}
 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();