Bilal_swl's avatar

CSRF Token Issue

Hi there, I am new in react, and I try to create a user management system. i make controller for CRUD operations. i use react as frontend with breeze. on localhost, every task working perfectly like, creating edit and delete. but when i push code on server (heroku), its show error (419) while creating, edit, or delete. user.tsx:

  const handleDelete = () => {
    router.delete(route("users.destroy", selectedUser?.id), {
      preserveScroll: true,
      preserveState: true,
      onSuccess: () => {
        toast.success("User has been deleted successfully");
        setSelectedUser(undefined);
      },
    });
  };

How I can add csrf token there, it's work on localhost perfectly but show error 419 on server. I am check bootstrap/app.php but there is no middleware for csrf bootstrap/app.php:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->web(append: [
            \App\Http\Middleware\HandleInertiaRequests::class,
            \Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
        ]);

        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

How I can fix this issue?

0 likes
6 replies
JussiMannisto's avatar

What session driver are you using? If it's file-based sessions, this might be a file/directory permission issue. The directory storage/framework/ and all its contents should be owned by the web server user, e.g. www-data.

Please or to participate in this conversation.