Make sure your session folder has the correct permissions.
Can't figure out 419 error on simple form :/
NB: I'm using laravel, inertia + react.
Hello there folks,
I have a simple form that users would enter their email address in and then submit. I reused a form from my login page, made the changes, and then created a controller, and updated my routes file. But to my surprised, I keep encountering 419 error.
When I check the network tab, the csrf token can be found. I'm super unclear on what could be causing the mismatch. Again, I have other forms like login and sign up, and I've never had this issue before.
Does anyone have any ideas? Thank you.
@jlrdw this is what my sessions file look like:
<?php
use Illuminate\Support\Str;
return [
'driver' => env('SESSION_DRIVER', 'database'),
'lifetime' => env('SESSION_LIFETIME', 120),
'expire_on_close' => env('SESSION_EXPIRE_ON_CLOSE', false),
'encrypt' => env('SESSION_ENCRYPT', false),
'files' => storage_path('framework/sessions'),
'connection' => env('SESSION_CONNECTION'),
'table' => env('SESSION_TABLE', 'sessions'),
'store' => env('SESSION_STORE'),
'lottery' => [2, 100],
'cookie' => env(
'SESSION_COOKIE',
Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
),
'path' => env('SESSION_PATH', '/'),
'domain' => env('SESSION_DOMAIN'),
'secure' => env('SESSION_SECURE_COOKIE'),
'http_only' => env('SESSION_HTTP_ONLY', true),
'same_site' => env('SESSION_SAME_SITE', 'lax'),
'partitioned' => env('SESSION_PARTITIONED_COOKIE', false),
];
Any thoughts?
@respect_ is this production and cloudfare used? If so see:
https://laracasts.com/discuss/channels/laravel/facing-strange-laravel-session-issue
You did php artisan migrate correct? While troubleshooting this, BACKUP any real live data so as not to loose any.
@jlrdw this is all local. All tables are already migrated. I wnet and cleared all the cache and such again.
Allow me to share some more code here if that's okay.
Waitlist Form
export default function Waitlist() {
const { data, setData, post, processing, errors } = useForm({
email: "",
});
const submit = (e) => {
e.preventDefault();
post(route("waitlist.store"));
};
return (
<div
id="waitlist"
className="bg-indigo-700 py-16 sm:py-24 lg:py-32 scroll-mt-28"
>
<h2 className="mx-auto max-w-2xl text-center text-4xl font-bold tracking-tight text-white sm:text-4xl">
We are launching soon.
</h2>
<p className="mx-auto mt-2 max-w-xl text-center text-lg leading-8 text-gray-300">
Discover What's Coming!
</p>
<form
onSubmit={submit}
className="mx-auto mt-10 flex max-w-md gap-x-4"
method="POST"
action="#"
noValidate
>
<input
id="email"
name="email"
type="email"
required
placeholder="Enter your email"
autoComplete="email"
className="min-w-0 flex-auto rounded-md border-0 bg-white/5 px-3.5 py-2 text-white shadow-sm ring-1 ring-inset ring-white/10 focus:ring-2 focus:ring-inset focus:ring-white sm:text-sm sm:leading-6"
value={data.email}
onChange={(e) => setData("email", e.target.value)}
/>
<button
type="submit"
className="flex-none rounded-md bg-white px-3.5 py-2.5 text-sm font-semibold text-gray-900 shadow-sm hover:bg-gray-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white"
disabled={processing}
>
{processing ? "Submitting..." : "Notify me"}
</button>
</form>
{errors.email && (
<div className="text-red-500 mt-2">{errors.email}</div>
)}
</div>
);
}
Waitlist Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Waitlist;
use Inertia\Inertia;
use Illuminate\Support\Facades\Log;
class WaitlistController extends Controller
{
public function index()
{
return Inertia::render('Waitlist');
}
public function store(Request $request)
{
Log::info('Waitlist store method hit', $request->all());
$request->validate([
'email' => 'required|email|unique:waitlists,email',
]);
Waitlist::create([
'email' => $request->email,
]);
return redirect()->back()->with('success', 'Email address added to the waitlist.');
}
}
web.php
Route::middleware(['web'])->group(function () {
Route::get('/waitlist', [WaitlistController::class, 'index'])->name('waitlist.index');
Route::post('/waitlist', [WaitlistController::class, 'store'])->name('waitlist.store');
});
Waitlist Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Waitlist extends Model
{
use HasFactory;
protected $fillable = ['email'];
}
I hope this helps to add some more context.
@respect_ check Inertia docs, you probably need a X-XSRF-TOKEN setup.
But i'm not that familiar with inertia.
Sure, but it does seem that the token is being added by default?
I also tried to add the token manually, but had the same issue.
It's such a pain.
@respect_ see if it's in the session database. The one you see has to match the one in session.
Edit:
Try writing something to session on a page, go to another page and make sure you can retrieve it.
Like:
$request->session()->put('myvalue', 'hello');
then
echo $request->session()->get('myvalue');
@jlrdw nothing is in the sessions table currently. The waitlist page is front facing, so no auth should be required.
@respect_ see my edited reply.
Reread this: https://inertiajs.com/csrf-protection
@jlrdw I tried to write to the sessions, nothing. I'll just hang up the towel for now. Thanks for trying to help.
@respect_ something must be setup wrong. I suggest watching some of the videos from this very site and you might catch what is wrong.
Many of the videos are free.
Or at least review each pertinent section in the documentation. Probably one little thing needs tweaked.
Thinking the very same, I may just go over it this weekend and write everything from scratch and see. Will post and update once I figure it out.
Just checking if you found a solution. Blade forms work but not react. I'm getting this same error despite many starting from scratch even on linux and windows. Tried all suggestions mentioned above. Just want to know before starting work on a workaround and wait and see on the next version if it works.
Please or to participate in this conversation.