The following describes macros that allow JSON Web Tokens (JWTs) to be pulled from HTTP requests and pushed to HTTP responses.
use Illuminate\Http\Request;
use Illuminate\Routing\Router as Route;
Route::get('/example', function (Request $request) {
// Get the token
$token = $request->getToken();
// Get the JWT
$jwt = $request->getJwt();
});
Build a Illuminate\Http\JsonResponse
that contains information about the JWT:
use Illuminate\Support\Facades\Response;
$response = Response::withJwt($jwt);
When $response
is returned, the client will receive a JSON object with information about the JWT:
{
"access_token": "ey...",
"token_type": "bearer",
"expires_in": 3600,
"expires_at": "2022-02-04T03:17:07+0000"
}
The 'expires_at' date/time value is in ISO 8601 format.
Use the ResponseBuilder
to create the array before it's encoded in JSON:
use LittleApps\LittleJWT\Utils\ResponseBuilder;
$data = ResponseBuilder::buildFromJwt($jwt);
The method will return an array with information about the JWT:
$data = [
"access_token" => "ey...",
"token_type" => "bearer",
"expires_in" => 3600,
"expires_at" => "2022-02-04T03:17:07+0000"
];
Set the JWT or token as the 'Authorization' response header:
use Illuminate\Support\Facades\Response;
return response(['foo' => 'bar'])->attachJwt($jwt);
The client will receive a response with the 'Authorization' header set to 'Bearer ey...'
Information on generating a response for the LittleJWT guard can be found the Guard page.