refactor(admin): update form flows for session auth
This commit is contained in:
parent
94fb5f6daf
commit
e2949bff20
7 changed files with 36 additions and 53 deletions
|
|
@ -9,7 +9,6 @@
|
||||||
import UnifiedMediaModal from './UnifiedMediaModal.svelte'
|
import UnifiedMediaModal from './UnifiedMediaModal.svelte'
|
||||||
import SmartImage from '../SmartImage.svelte'
|
import SmartImage from '../SmartImage.svelte'
|
||||||
import Composer from './composer'
|
import Composer from './composer'
|
||||||
import { authenticatedFetch } from '$lib/admin-auth'
|
|
||||||
import { toast } from '$lib/stores/toast'
|
import { toast } from '$lib/stores/toast'
|
||||||
import type { Album, Media } from '@prisma/client'
|
import type { Album, Media } from '@prisma/client'
|
||||||
import type { JSONContent } from '@tiptap/core'
|
import type { JSONContent } from '@tiptap/core'
|
||||||
|
|
@ -99,7 +98,9 @@
|
||||||
if (!album) return
|
if (!album) return
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await authenticatedFetch(`/api/albums/${album.id}`)
|
const response = await fetch(`/api/albums/${album.id}`, {
|
||||||
|
credentials: 'same-origin'
|
||||||
|
})
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
albumMedia = data.media || []
|
albumMedia = data.media || []
|
||||||
|
|
@ -158,12 +159,13 @@
|
||||||
const url = mode === 'edit' ? `/api/albums/${album?.id}` : '/api/albums'
|
const url = mode === 'edit' ? `/api/albums/${album?.id}` : '/api/albums'
|
||||||
const method = mode === 'edit' ? 'PUT' : 'POST'
|
const method = mode === 'edit' ? 'PUT' : 'POST'
|
||||||
|
|
||||||
const response = await authenticatedFetch(url, {
|
const response = await fetch(url, {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify(payload)
|
body: JSON.stringify(payload),
|
||||||
|
credentials: 'same-origin'
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|
@ -181,12 +183,13 @@
|
||||||
if (mode === 'create' && pendingMediaIds.length > 0) {
|
if (mode === 'create' && pendingMediaIds.length > 0) {
|
||||||
const photoToastId = toast.loading('Adding selected photos to album...')
|
const photoToastId = toast.loading('Adding selected photos to album...')
|
||||||
try {
|
try {
|
||||||
const photoResponse = await authenticatedFetch(`/api/albums/${savedAlbum.id}/media`, {
|
const photoResponse = await fetch(`/api/albums/${savedAlbum.id}/media`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ mediaIds: pendingMediaIds })
|
body: JSON.stringify({ mediaIds: pendingMediaIds }),
|
||||||
|
credentials: 'same-origin'
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!photoResponse.ok) {
|
if (!photoResponse.ok) {
|
||||||
|
|
|
||||||
|
|
@ -61,11 +61,9 @@
|
||||||
async function loadAlbums() {
|
async function loadAlbums() {
|
||||||
try {
|
try {
|
||||||
isLoading = true
|
isLoading = true
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) return
|
|
||||||
|
|
||||||
const response = await fetch('/api/albums', {
|
const response = await fetch('/api/albums', {
|
||||||
headers: { Authorization: `Basic ${auth}` }
|
credentials: 'same-origin'
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|
@ -98,13 +96,10 @@
|
||||||
try {
|
try {
|
||||||
isSaving = true
|
isSaving = true
|
||||||
error = ''
|
error = ''
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) return
|
|
||||||
|
|
||||||
const response = await fetch('/api/albums', {
|
const response = await fetch('/api/albums', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Basic ${auth}`,
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
|
@ -112,7 +107,8 @@
|
||||||
slug: newAlbumSlug.trim(),
|
slug: newAlbumSlug.trim(),
|
||||||
isPhotography: true,
|
isPhotography: true,
|
||||||
status: 'draft'
|
status: 'draft'
|
||||||
})
|
}),
|
||||||
|
credentials: 'same-origin'
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|
@ -143,8 +139,6 @@
|
||||||
try {
|
try {
|
||||||
isSaving = true
|
isSaving = true
|
||||||
error = ''
|
error = ''
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) return
|
|
||||||
|
|
||||||
// Get the list of albums to add/remove
|
// Get the list of albums to add/remove
|
||||||
const currentAlbumIds = new Set(currentAlbums.map((a) => a.id))
|
const currentAlbumIds = new Set(currentAlbums.map((a) => a.id))
|
||||||
|
|
@ -158,10 +152,10 @@
|
||||||
const response = await fetch(`/api/albums/${albumId}/media`, {
|
const response = await fetch(`/api/albums/${albumId}/media`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Basic ${auth}`,
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ mediaIds: [mediaId] })
|
body: JSON.stringify({ mediaIds: [mediaId] }),
|
||||||
|
credentials: 'same-origin'
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|
@ -174,10 +168,10 @@
|
||||||
const response = await fetch(`/api/albums/${albumId}/media`, {
|
const response = await fetch(`/api/albums/${albumId}/media`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Basic ${auth}`,
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ mediaIds: [mediaId] })
|
body: JSON.stringify({ mediaIds: [mediaId] }),
|
||||||
|
credentials: 'same-origin'
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|
|
||||||
|
|
@ -34,16 +34,14 @@
|
||||||
try {
|
try {
|
||||||
isSaving = true
|
isSaving = true
|
||||||
error = ''
|
error = ''
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) return
|
|
||||||
|
|
||||||
const response = await fetch(`/api/albums/${selectedAlbumId}/media`, {
|
const response = await fetch(`/api/albums/${selectedAlbumId}/media`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Basic ${auth}`,
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ mediaIds: selectedMediaIds })
|
body: JSON.stringify({ mediaIds: selectedMediaIds }),
|
||||||
|
credentials: 'same-origin'
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|
|
||||||
|
|
@ -146,12 +146,6 @@ $effect(() => {
|
||||||
try {
|
try {
|
||||||
isSaving = true
|
isSaving = true
|
||||||
|
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
title,
|
title,
|
||||||
slug,
|
slug,
|
||||||
|
|
@ -167,13 +161,17 @@ $effect(() => {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Basic ${auth}`,
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify(payload)
|
body: JSON.stringify(payload),
|
||||||
|
credentials: 'same-origin'
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
if (response.status === 401) {
|
||||||
|
goto('/admin/login')
|
||||||
|
return
|
||||||
|
}
|
||||||
throw new Error(`Failed to ${mode === 'edit' ? 'save' : 'create'} essay`)
|
throw new Error(`Failed to ${mode === 'edit' ? 'save' : 'create'} essay`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -185,12 +185,6 @@ $effect(() => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate slug from title
|
// Generate slug from title
|
||||||
const slug = createSlug(title)
|
const slug = createSlug(title)
|
||||||
|
|
||||||
|
|
@ -215,13 +209,17 @@ $effect(() => {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Basic ${auth}`,
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify(payload)
|
body: JSON.stringify(payload),
|
||||||
|
credentials: 'same-origin'
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
if (response.status === 401) {
|
||||||
|
goto('/admin/login')
|
||||||
|
return
|
||||||
|
}
|
||||||
throw new Error(`Failed to ${mode === 'edit' ? 'update' : 'create'} photo post`)
|
throw new Error(`Failed to ${mode === 'edit' ? 'update' : 'create'} photo post`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -242,12 +242,6 @@
|
||||||
try {
|
try {
|
||||||
isSaving = true
|
isSaving = true
|
||||||
|
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
title: formData.title,
|
title: formData.title,
|
||||||
subtitle: formData.subtitle,
|
subtitle: formData.subtitle,
|
||||||
|
|
|
||||||
|
|
@ -136,12 +136,6 @@ $effect(() => {
|
||||||
try {
|
try {
|
||||||
isSaving = true
|
isSaving = true
|
||||||
|
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const payload: any = {
|
const payload: any = {
|
||||||
type: 'post', // Use simplified post type
|
type: 'post', // Use simplified post type
|
||||||
status: publishStatus,
|
status: publishStatus,
|
||||||
|
|
@ -161,13 +155,17 @@ $effect(() => {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Basic ${auth}`,
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify(payload)
|
body: JSON.stringify(payload),
|
||||||
|
credentials: 'same-origin'
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
if (response.status === 401) {
|
||||||
|
goto('/admin/login')
|
||||||
|
return
|
||||||
|
}
|
||||||
throw new Error(`Failed to ${mode === 'edit' ? 'save' : 'create'} post`)
|
throw new Error(`Failed to ${mode === 'edit' ? 'save' : 'create'} post`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue