1. Go to this page and download the library: Download mydaniel/laravel-paseto 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/ */
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use MyDaniel\Paseto\Contracts\PasetoSubject;
use MyDaniel\Paseto\Traits\HasPaseto;
class User extends Authenticatable implements PasetoSubject
{
use HasPaseto;
// ... your model properties and methods
}
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if (!Auth::attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
/** @var \App\Models\User $user */
$user = Auth::user();
$token = $user->generateToken();
return response()->json(['token' => $token]);
}
}
use Illuminate\Support\Facades\Route;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
public function logout()
{
Auth::logout();
return response()->json(['message' => 'Successfully logged out']);
}
// In App\Models\User
public function getJwtCustomClaims(): array
{
return [
'foo' => 'bar',
'role' => $this->role,
];
}