Why do you need a middleware for changing the expiration time when you can do it through the config?
Aug 9, 2024
10
Level 63
Access the authenticated user inside a middleware
Hello,
I need to refresh the expiration date for the Sanctum API token.
So I have created a middleware with this code.
$request->user()->currentAccessToken()->forceFill([
'expires_at' => now()->addHour(),
])->updated();
I'm using the middleware for all API routes and I have placed it after the auth:sanctum middleware to be sure that the authentication has been done.
Route::prefix('/v1')->middleware(['auth:sanctum', RefreshApiTokenExpirationMiddleware::class])->group(function () {
But I get an error saying that $request->user() is null.
Why ?
Any idea ?
Thanks for your help.
V
Level 122
remember where you put the middleware in the stack is important.
1 like
Please or to participate in this conversation.