|
3 | 3 | import {fade, fly, slide} from "svelte/transition"; |
4 | 4 | import {get, writable} from "svelte/store"; |
5 | 5 | import axios from "axios"; |
6 | | - import {authToken, logout as authLogout} from "../stores/auth.js"; |
| 6 | + import {isAuthenticated, logout as authLogout, verifyAuth} from "../stores/auth.js"; |
7 | 7 | import {addNotification} from "../stores/notifications.js"; |
8 | 8 | import Spinner from "../components/Spinner.svelte"; |
9 | 9 | import {navigate} from "svelte-routing"; |
|
78 | 78 | let showOptions = false; |
79 | 79 | let showSavedScripts = false; |
80 | 80 |
|
81 | | - let isAuthenticated = false; |
| 81 | + let authenticated = false; |
82 | 82 | let savedScripts = []; |
83 | 83 | let scriptName = createPersistentStore("scriptName", ""); |
84 | 84 | let currentScriptId = createPersistentStore("currentScriptId", null); |
|
126 | 126 | } |
127 | 127 |
|
128 | 128 | onMount(async () => { |
129 | | - unsubscribeAuth = authToken.subscribe(token => { |
130 | | - const wasAuthenticated = isAuthenticated; |
131 | | - isAuthenticated = !!token; |
132 | | - if (!wasAuthenticated && isAuthenticated && editorView) { |
| 129 | + // Verify authentication status on startup |
| 130 | + await verifyAuth(); |
| 131 | + |
| 132 | + unsubscribeAuth = isAuthenticated.subscribe(authStatus => { |
| 133 | + const wasAuthenticated = authenticated; |
| 134 | + authenticated = authStatus; |
| 135 | + if (!wasAuthenticated && authenticated && editorView) { |
133 | 136 | loadSavedScripts(); |
134 | | - } else if (wasAuthenticated && !isAuthenticated) { |
| 137 | + } else if (wasAuthenticated && !authenticated) { |
135 | 138 | savedScripts = []; |
136 | 139 | showSavedScripts = false; |
137 | 140 | currentScriptId.set(null); |
|
184 | 187 | } |
185 | 188 | }); |
186 | 189 |
|
187 | | - if (isAuthenticated) { |
| 190 | + if (authenticated) { |
188 | 191 | await loadSavedScripts(); |
189 | 192 | } |
190 | 193 | }); |
|
334 | 337 | } |
335 | 338 |
|
336 | 339 | async function loadSavedScripts() { |
337 | | - if (!isAuthenticated) return; |
338 | | - const authTokenValue = get(authToken); |
| 340 | + if (!authenticated) return; |
339 | 341 | try { |
340 | 342 | const response = await axios.get(`/api/v1/scripts`, { |
341 | | - headers: {Authorization: `Bearer ${authTokenValue}`}, |
| 343 | + withCredentials: true, // Use cookies for authentication |
342 | 344 | }); |
343 | 345 | savedScripts = response.data || []; |
344 | 346 | } catch (err) { |
|
371 | 373 | } |
372 | 374 |
|
373 | 375 | async function saveScript() { |
374 | | - if (!isAuthenticated) { |
| 376 | + if (!authenticated) { |
375 | 377 | addNotification("Please log in to save scripts.", "warning"); |
376 | 378 | return; |
377 | 379 | } |
|
381 | 383 | return; |
382 | 384 | } |
383 | 385 | const scriptValue = get(script); |
384 | | - const authTokenValue = get(authToken); |
385 | 386 | const currentIdValue = get(currentScriptId); |
386 | 387 | let operation = currentIdValue ? 'update' : 'create'; |
387 | 388 |
|
|
391 | 392 | response = await axios.put( |
392 | 393 | `/api/v1/scripts/${currentIdValue}`, |
393 | 394 | {name: nameValue, script: scriptValue}, |
394 | | - {headers: {Authorization: `Bearer ${authTokenValue}`}} |
| 395 | + {withCredentials: true} |
395 | 396 | ); |
396 | 397 | addNotification("Script updated successfully.", "success"); |
397 | 398 | } else { |
398 | 399 | response = await axios.post( |
399 | 400 | `/api/v1/scripts`, |
400 | 401 | {name: nameValue, script: scriptValue}, |
401 | | - {headers: {Authorization: `Bearer ${authTokenValue}`}} |
| 402 | + {withCredentials: true} |
402 | 403 | ); |
403 | 404 | currentScriptId.set(response.data.id); |
404 | 405 | addNotification("Script saved successfully.", "success"); |
|
414 | 415 | } |
415 | 416 |
|
416 | 417 | async function deleteScript(scriptIdToDelete) { |
417 | | - if (!isAuthenticated) return; |
| 418 | + if (!authenticated) return; |
418 | 419 | const scriptToDelete = savedScripts.find(s => s.id === scriptIdToDelete); |
419 | 420 | const confirmMessage = scriptToDelete |
420 | 421 | ? `Are you sure you want to delete "${scriptToDelete.name}"?` |
421 | 422 | : "Are you sure you want to delete this script?"; |
422 | 423 |
|
423 | 424 | if (!confirm(confirmMessage)) return; |
424 | 425 |
|
425 | | - const authTokenValue = get(authToken); |
426 | 426 | try { |
427 | 427 | await axios.delete(`/api/v1/scripts/${scriptIdToDelete}`, { |
428 | | - headers: {Authorization: `Bearer ${authTokenValue}`}, |
| 428 | + withCredentials: true, |
429 | 429 | }); |
430 | 430 | addNotification("Script deleted successfully.", "success"); |
431 | 431 | if (get(currentScriptId) === scriptIdToDelete) { |
|
517 | 517 |
|
518 | 518 | function toggleSavedScripts() { |
519 | 519 | showSavedScripts = !showSavedScripts; |
520 | | - if (showSavedScripts && isAuthenticated) { |
| 520 | + if (showSavedScripts && authenticated) { |
521 | 521 | loadSavedScripts(); |
522 | 522 | } |
523 | 523 | } |
|
893 | 893 |
|
894 | 894 | <!-- Right Column: Saved Scripts --> |
895 | 895 | <div class="w-1/2 space-y-3"> |
896 | | - {#if isAuthenticated} |
| 896 | + {#if authenticated} |
897 | 897 | <h4 class="text-xs font-medium text-fg-muted dark:text-dark-fg-muted uppercase tracking-wider"> |
898 | 898 | Saved Scripts |
899 | 899 | </h4> |
|
0 commit comments