refactor(admin): update form flows for session auth

This commit is contained in:
Justin Edmund 2025-10-07 05:57:45 -07:00
parent 94fb5f6daf
commit e2949bff20
7 changed files with 36 additions and 53 deletions

View file

@ -9,7 +9,6 @@
import UnifiedMediaModal from './UnifiedMediaModal.svelte'
import SmartImage from '../SmartImage.svelte'
import Composer from './composer'
import { authenticatedFetch } from '$lib/admin-auth'
import { toast } from '$lib/stores/toast'
import type { Album, Media } from '@prisma/client'
import type { JSONContent } from '@tiptap/core'
@ -99,7 +98,9 @@
if (!album) return
try {
const response = await authenticatedFetch(`/api/albums/${album.id}`)
const response = await fetch(`/api/albums/${album.id}`, {
credentials: 'same-origin'
})
if (response.ok) {
const data = await response.json()
albumMedia = data.media || []
@ -158,12 +159,13 @@
const url = mode === 'edit' ? `/api/albums/${album?.id}` : '/api/albums'
const method = mode === 'edit' ? 'PUT' : 'POST'
const response = await authenticatedFetch(url, {
const response = await fetch(url, {
method,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
body: JSON.stringify(payload),
credentials: 'same-origin'
})
if (!response.ok) {
@ -181,12 +183,13 @@
if (mode === 'create' && pendingMediaIds.length > 0) {
const photoToastId = toast.loading('Adding selected photos to album...')
try {
const photoResponse = await authenticatedFetch(`/api/albums/${savedAlbum.id}/media`, {
const photoResponse = await fetch(`/api/albums/${savedAlbum.id}/media`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ mediaIds: pendingMediaIds })
body: JSON.stringify({ mediaIds: pendingMediaIds }),
credentials: 'same-origin'
})
if (!photoResponse.ok) {

View file

@ -61,11 +61,9 @@
async function loadAlbums() {
try {
isLoading = true
const auth = localStorage.getItem('admin_auth')
if (!auth) return
const response = await fetch('/api/albums', {
headers: { Authorization: `Basic ${auth}` }
credentials: 'same-origin'
})
if (!response.ok) {
@ -98,13 +96,10 @@
try {
isSaving = true
error = ''
const auth = localStorage.getItem('admin_auth')
if (!auth) return
const response = await fetch('/api/albums', {
method: 'POST',
headers: {
Authorization: `Basic ${auth}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
@ -112,7 +107,8 @@
slug: newAlbumSlug.trim(),
isPhotography: true,
status: 'draft'
})
}),
credentials: 'same-origin'
})
if (!response.ok) {
@ -143,8 +139,6 @@
try {
isSaving = true
error = ''
const auth = localStorage.getItem('admin_auth')
if (!auth) return
// Get the list of albums to add/remove
const currentAlbumIds = new Set(currentAlbums.map((a) => a.id))
@ -158,10 +152,10 @@
const response = await fetch(`/api/albums/${albumId}/media`, {
method: 'POST',
headers: {
Authorization: `Basic ${auth}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ mediaIds: [mediaId] })
body: JSON.stringify({ mediaIds: [mediaId] }),
credentials: 'same-origin'
})
if (!response.ok) {
@ -174,10 +168,10 @@
const response = await fetch(`/api/albums/${albumId}/media`, {
method: 'DELETE',
headers: {
Authorization: `Basic ${auth}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ mediaIds: [mediaId] })
body: JSON.stringify({ mediaIds: [mediaId] }),
credentials: 'same-origin'
})
if (!response.ok) {

View file

@ -34,16 +34,14 @@
try {
isSaving = true
error = ''
const auth = localStorage.getItem('admin_auth')
if (!auth) return
const response = await fetch(`/api/albums/${selectedAlbumId}/media`, {
method: 'POST',
headers: {
Authorization: `Basic ${auth}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ mediaIds: selectedMediaIds })
body: JSON.stringify({ mediaIds: selectedMediaIds }),
credentials: 'same-origin'
})
if (!response.ok) {

View file

@ -146,12 +146,6 @@ $effect(() => {
try {
isSaving = true
const auth = localStorage.getItem('admin_auth')
if (!auth) {
goto('/admin/login')
return
}
const payload = {
title,
slug,
@ -167,13 +161,17 @@ $effect(() => {
const response = await fetch(url, {
method,
headers: {
Authorization: `Basic ${auth}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
body: JSON.stringify(payload),
credentials: 'same-origin'
})
if (!response.ok) {
if (response.status === 401) {
goto('/admin/login')
return
}
throw new Error(`Failed to ${mode === 'edit' ? 'save' : 'create'} essay`)
}

View file

@ -185,12 +185,6 @@ $effect(() => {
}
}
const auth = localStorage.getItem('admin_auth')
if (!auth) {
goto('/admin/login')
return
}
// Generate slug from title
const slug = createSlug(title)
@ -215,13 +209,17 @@ $effect(() => {
const response = await fetch(url, {
method,
headers: {
Authorization: `Basic ${auth}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
body: JSON.stringify(payload),
credentials: 'same-origin'
})
if (!response.ok) {
if (response.status === 401) {
goto('/admin/login')
return
}
throw new Error(`Failed to ${mode === 'edit' ? 'update' : 'create'} photo post`)
}

View file

@ -242,12 +242,6 @@
try {
isSaving = true
const auth = localStorage.getItem('admin_auth')
if (!auth) {
goto('/admin/login')
return
}
const payload = {
title: formData.title,
subtitle: formData.subtitle,

View file

@ -136,12 +136,6 @@ $effect(() => {
try {
isSaving = true
const auth = localStorage.getItem('admin_auth')
if (!auth) {
goto('/admin/login')
return
}
const payload: any = {
type: 'post', // Use simplified post type
status: publishStatus,
@ -161,13 +155,17 @@ $effect(() => {
const response = await fetch(url, {
method,
headers: {
Authorization: `Basic ${auth}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
body: JSON.stringify(payload),
credentials: 'same-origin'
})
if (!response.ok) {
if (response.status === 401) {
goto('/admin/login')
return
}
throw new Error(`Failed to ${mode === 'edit' ? 'save' : 'create'} post`)
}