From 6b8f50a90ef36fdaac58416b73fd04d7669d3a10 Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Tue, 16 Jun 2026 19:42:26 +0200 Subject: [PATCH] refactor(v5): extract csrfToken into shared lib/csrf.js module --- resources/js/v5/components/app-navbar.jsx | 5 +---- resources/js/v5/lib/csrf.js | 3 +++ tests/Feature/V5/HomeTest.php | 4 ++++ 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 resources/js/v5/lib/csrf.js diff --git a/resources/js/v5/components/app-navbar.jsx b/resources/js/v5/components/app-navbar.jsx index 7dda6301c..b64c90402 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 000000000..2a23b52e5 --- /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 e3f689a31..fc64c0112 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('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"')