I have been going through all the questions and answers that I can find concerning this issue but I am getting nowhere.
I am using reactjs and inertiajs with laravel and I can’t fix what I am assuming is the csrf issue. I keep getting 419 Page Expired
when I try to submit log in credentials on a login page.
setup
laravel v10.4,
inertiajs v1.0,
reactjs v18.2
Login.tsx (edited for brevity)
const form = useForm({
email: '',
password: '',
});
function login(e) {
e.preventDefault();
form.post(route('login'), {
onFinish: () => {
form.reset('password');
},
});
}
...return (
<div>
<!-- name input div -->
<!-- password input div -->
<Button onClick={login}> Log in </Button>
</div>
)
The session driver was initially set to database
but I have set it to file
.env
SESSSION_DRIVER=file
Firstly I was hoping that it would work out of the box since the docs state
Laravel automatically includes the proper CSRF token when making requests via Inertia or Axios. However, if you’re using Laravel, be
sure to omit thecsrf-token
meta tag from your project, as this will
prevent the CSRF token from refreshing properly.
but I had no joy. Is there more to this?
I have tried:
- removing
StartSession
,ShareErrorsFromSesssion
from$middlewareGroups
in Kernel.php - adding
<meta name="csrf-token" content="{{ csrf_token() }}">
to my app.blade.php, and then adding<input type="hidden" name="_token" value="{{ csrf_token() }}" />
into the form JSX - adding csrf_token manually in form
const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
const form = useForm({
email: '',
password: '',
_token: token,
});
Nothing is working and I still get the `419` error. Are there any other options I can try?