Redirect Handling Using Exception Handler When CSRF Token Mismatch Occurs #54539
Unanswered
bigstone1990
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is my first time posting a question. Thank you in advance for your help.
I am building a multi-login system with Laravel 11, Inertia, and Vue 3. I have successfully managed to separate session tables and cookies for different user types, but I am having trouble with handling the XSFR-TOKEN cookie.
When logged in as different user types in multiple tabs of the same browser and both forms are open at the same time, one user type works fine, but for the other, a 419 error page appears.
It would have been ideal to manage separate XSFR-TOKEN cookies for each user type, but that implementation is beyond my current level of expertise. Therefore, following the official Inertia documentation, I decided to implement a redirect back to the original request when a 419 error page occurs, using the exception handler.
I have added the following code to bootstrap/app.php:
use Symfony\Component\HttpFoundation\Response;
->withExceptions(function (Exceptions $exceptions) {
$exceptions->respond(function (Response $response) {
if ($response->getStatusCode() === 419) {
return back()->with([
'message' => 'The page expired, please try again.',
]);
}
return $response;
});
});
This code successfully redirects POST requests when a 419 error occurs. However, I am encountering an error with PUT, PATCH, and DELETE requests. The image shows the error for a PUT request.
It seems that the redirect method does not switch to a GET request and instead retains the original request method.
What would be the best practice in this case?
Beta Was this translation helpful? Give feedback.
All reactions