PHP code example of randomstate / laravel-auth-jwt

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

    

randomstate / laravel-auth-jwt example snippets




use \RandomState\LaravelAuth\Strategies\JwtStrategy;
use \RandomState\LaravelAuth\Strategies\Jwt\Issuer;
use \Carbon\CarbonInterval;

class MyServiceProvider extends \Illuminate\Support\ServiceProvider {
    
    public function register() {
        $this->app->resolving(\Illuminate\Auth\AuthManager::class, function($manager) {
           $manager->register('jwt', $this->app->make(JwtStrategy::class));
        });
        
        $this->app->resolving(JwtStrategy::class, function($strategy) {
           $strategy->convertUsing(function(\RandomState\LaravelAuth\Strategies\JwtUser $user) {
              return User::find($user->id()); // assuming you are using Eloquent 
           });
        });
        
        $this->app->bind(Issuer::class, function() {
            $issuer = new Issuer();
            
            $issuer
                ->withIssuer('my_app') // chain and build your configuration
                ->withAudience('my_app')
                ->withExpirationWindow(CarbonInterval::minutes(60))
                ->signTokens(new \Lcobucci\JWT\Signer\Rsa\Sha256(), config('auth.jwt_signing_key')) // your private RSA key in this example
                ;
        });
    }
}



use \Illuminate\Http\Request;
use \App\Http\Controllers\Controller;
use \RandomState\LaravelAuth\Strategies\Jwt\Issuer;
use \Illuminate\Support\Facades\Auth;

class LoginController extends Controller {
    
    public function login(Request $request, Issuer $issuer) {
        $token = $issuer->issue(Auth::user()->getAuthIdentifier());
        
        return response($token);
    }
}