PHP code example of vkoori / laravel-stateless-auth
1. Go to this page and download the library: Download vkoori/laravel-stateless-auth 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/ */
vkoori / laravel-stateless-auth example snippets
'guards' => [
'api' => [
'driver' => 'jwt-auth',
'provider' => 'jwt-users',
],
],
'providers' => [
'jwt-users' => [
'driver' => 'jwt-auth-provider',
'model' => App\Models\User::class,
],
],
use Vkoori\JwtAuth\Auth\Traits\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
}
'stores' => [
// Other cache stores...
'redis_jwt' => [
'driver' => 'redis',
'connection' => env('REDIS_JWT_CONNECTION', 'jwt'),
'lock_connection' => env('REDIS_CACHE_LOCK_CONNECTION', 'default'),
],
],
'redis' => [
// Other connections...
'jwt' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_JWT_DB', '2'),
],
],
use Vkoori\JwtAuth\Auth\Traits\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
public ?string $jwtCacheDriver = 'redis_jwt';
}
->withMiddleware(function (Middleware $middleware): void {
$middleware
->alias([
'jwt.scope' => \Vkoori\JwtAuth\Middlewares\JwtScopeMiddleware::class,
]);
})
protected $routeMiddleware = [
// ...
'jwt.scope' => \Vkoori\JwtAuth\Middlewares\JwtScopeMiddleware::class,
];
$user->accessToken(scopes: ['admin', 'manager'])
Route::middleware(['jwt.scope:admin'])->group(function () {
Route::get('/admin/dashboard', [AdminController::class, 'index']);
});
Route::middleware(['jwt.scope:admin,other'])->group(function () {
Route::get('/admin/dashboard', [AdminController::class, 'index']);
});
bash
php artisan vendor:publish --provider="Vkoori\JwtAuth\AuthServiceProvider"
bash
php artisan cache:clear redis_jwt