vue.js 419|页面在生产中过期,Laravel 9和Inertia是否存在CSRF问题?

ff29svar  于 2022-11-25  发布在  Vue.js
关注(0)|答案(1)|浏览(93)

部署后,当我尝试使用delete route时,会抛出以下错误:

419 | PAGE EXPIRED
Failed to load resource: the server responded with a status of 419 ()

触发路由的vue组件内的函数:

const destroyEvent = (record) => {
    if (confirm('Delete event?')) {
        Inertia.delete(route('admin.event.destroy', {
            id: record.id,
            title: record.title,
            image_path: record.image_path,
        }), { preserveScroll: true })
    }
}

Web路由:

Route::delete('/admin/event/destroy', [EventsController::class, 'destroyEvent'])->name('admin.event.destroy');

请求似乎没有到达EventsController,我已经用dd()测试过了。
根据this stack question,这可能是一个CSRF令牌问题。我已经尝试了所有建议,但它没有帮助。虽然注解:

\App\Http\Middleware\VerifyCsrfToken::class

app\Kernel.php移除419 | PAGE EXPIRED屏幕,而显示空白页面。这是否表示CSRF有问题?
从这个laracasts question,我还尝试添加:

public function boot()
    {
        if($this->app->environment('production') || $this->app->environment('staging'))
        {
            \URL::forceScheme('https');
        }
    }

AppServiceProvider.php,而问题没有改善。
有办法解决吗?

1yjd4xko

1yjd4xko1#

我找到了一个变通办法,即使用form.delete而不是Inertia.delete

const destroyEvent = (record) => {
    if (confirm('Delete Event?')) {
        const form = useForm({
            id: record.id,
            title: record.title,
            image_path: record.image_path
        })
        form.delete(route('admin.event.destroy'), { preserveScroll: true })
    }
}

了解form帮助器处理CSRF与处理Inertia的不同之处将是非常有用的。

相关问题