The method of adding a global CSRF token on Laravel Inertia is as follows:
app/Http/Middleware/VerifyCsrfToken.php
file:use Illuminate\Http\Request;
protected function addCookieToResponse($request, $response)
{
$response->headers->set('X-CSRF-TOKEN', $request->session()->token());
return parent::addCookieToResponse($request, $response);
}
@inertia
and define a meta tag for the CSRF token:<template>
<div>
<!-- Your component here -->
</div>
</template>
<script>
export default {
metaInfo() {
return {
title: 'Your Page Title',
meta: [
{
name: 'csrf-token',
content: this.$page.props.csrfToken,
},
],
};
},
};
</script>
<style>
/*Your component's styles here*/
</style>
@inertia
<!doctype html>
<html>
<head>
<!-- Other head tags here -->
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
@inertia
</body>
</html>
With these steps, you have now added a global CSRF token which will be available for all your pages.
Asked: 2022-10-09 11:00:00 +0000
Seen: 11 times
Last updated: Nov 10 '22