Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor(v5): extract csrfToken into shared lib/csrf.js module

Andras Bacsai6b8f50a90ef3
diff --git a/resources/js/v5/components/app-navbar.jsx b/resources/js/v5/components/app-navbar.jsx
index 7dda6301cb..b64c904026 100644
--- a/resources/js/v5/components/app-navbar.jsx
+++ b/resources/js/v5/components/app-navbar.jsx
@@ -1,10 +1,7 @@
import { useMemo, useState } from 'react';
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
-
-function csrfToken() {
- return document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') ?? '';
-}
+import { csrfToken } from '@/lib/csrf';
function persistSelection(projectUuid, environmentUuid) {
void fetch('/v5/selection', {
diff --git a/resources/js/v5/lib/csrf.js b/resources/js/v5/lib/csrf.js
new file mode 100644
index 0000000000..2a23b52e5e
--- /dev/null
+++ b/resources/js/v5/lib/csrf.js
@@ -0,0 +1,3 @@
+export function csrfToken() {
+ return document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') ?? '';
+}
diff --git a/tests/Feature/V5/HomeTest.php b/tests/Feature/V5/HomeTest.php
index e3f689a313..fc64c0112b 100644
--- a/tests/Feature/V5/HomeTest.php
+++ b/tests/Feature/V5/HomeTest.php
@@ -327,6 +327,8 @@ it('defines the v5 home page as a shadcn styled canvas shell', function () {
expect($homePage)
->toContain('Magic')
->toContain("import { AppNavbar } from '@/components/app-navbar';")
+ ->not->toContain("import { csrfToken } from '@/lib/csrf';")
+ ->not->toContain('function csrfToken()')
->toContain('<AppNavbar')
->toContain('bg-background text-foreground')
->toContain('h-dvh overflow-hidden bg-background text-foreground')
@@ -340,6 +342,8 @@ it('defines the v5 home page as a shadcn styled canvas shell', function () {
->not->toContain("'X-CSRF-TOKEN': csrfToken()");
expect($navbar)
+ ->toContain("import { csrfToken } from '@/lib/csrf';")
+ ->not->toContain('function csrfToken()')
->toContain('export function AppNavbar')
->toContain('/coolify-logo.svg')
->toContain('className="fixed inset-x-0 top-0 z-40 border-b border-border bg-background"')