fix: push token generation via SvelteKit form action (fixes cookie forwarding)
Author: Aaron Steven White
Commit
db3d0c7b6bb0fa6a6e1a2da82e55b1aab3e441e6Parent: 90ef9d9c89
Structural diff unavailable
These commits were pushed via plain git push, so no pre-parsed
schemas are available. Install git-remote-cospan and re-push via panproto:// to
see scope-level changes, breaking change detection, and semantic diffs.
brew install panproto/tap/git-remote-cospan2 files changed +34 -28
@@ -0,0 +1,21 @@
1+import type { Actions } from './$types'; 2+import { fail } from '@sveltejs/kit'; 3+ 4+export const actions: Actions = { 5+ createPushToken: async ({ request, fetch }) => { 6+ // Use SvelteKit's fetch which automatically forwards cookies 7+ const response = await fetch('/xrpc/dev.cospan.repo.createPushToken', { 8+ method: 'POST', 9+ headers: { 'Content-Type': 'application/json' }, 10+ body: '{}', 11+ }); 12+ 13+ if (!response.ok) { 14+ const body = await response.json().catch(() => ({ message: response.statusText })); 15+ return fail(response.status, { error: body.message ?? 'Failed to generate token' }); 16+ } 17+ 18+ const data = await response.json(); 19+ return { token: data.token, did: data.did, expiresIn: data.expiresIn }; 20+ }, 21+};
@@ -3,7 +3,7 @@
33 import { getAuth } from '$lib/stores/auth.svelte'; 44 import KeyForm from '$lib/components/settings/KeyForm.svelte'; 55 import { listKeys, addKey, deleteKey, type Key, type KeyType } from '$lib/api/keys.js'; 6- import { xrpcProcedure } from '$lib/api/client.js'; 6+ import { enhance } from '$app/forms'; 77 import { formatDate } from '$lib/utils/time.js'; 88 99 let auth = $derived(getAuth());
@@ -15,29 +15,13 @@
1515 1616 let filteredKeys = $derived(keys.filter((k) => k.type === activeTab)); 1717 18+ let { form } = $props(); 19+ 1820 // Push token state 19- let pushToken = $state<string | null>(null); 20- let pushTokenLoading = $state(false); 21- let pushTokenError = $state<string | null>(null); 21+ let pushToken = $derived(form?.token as string | undefined); 22+ let pushTokenError = $derived(form?.error as string | undefined); 2223 let pushTokenCopied = $state(false); 2324 24- async function generatePushToken() { 25- pushTokenLoading = true; 26- pushTokenError = null; 27- pushToken = null; 28- try { 29- const result = await xrpcProcedure<{ token: string; did: string; expiresIn: number }>( 30- 'dev.cospan.repo.createPushToken', 31- {} 32- ); 33- pushToken = result.token; 34- } catch (e: any) { 35- pushTokenError = e.message ?? 'Failed to generate token'; 36- } finally { 37- pushTokenLoading = false; 38- } 39- } 40- 4125 async function copyToken() { 4226 if (!pushToken) return; 4327 await navigator.clipboard.writeText(pushToken);
@@ -196,13 +180,14 @@
196180 </div> 197181 {/if} 198182 199- <button 200- onclick={generatePushToken} 201- disabled={pushTokenLoading} 202- class="rounded-md bg-accent px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-accent-hover disabled:opacity-50" 203- > 204- {pushTokenLoading ? 'Generating...' : 'Generate Token'} 205- </button> 183+ <form method="POST" action="?/createPushToken" use:enhance> 184+ <button 185+ type="submit" 186+ class="rounded-md bg-accent px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-accent-hover" 187+ > 188+ Generate Token 189+ </button> 190+ </form> 206191 </div> 207192 {:else} 208193 <!-- Add key button / form -->