hardening
This commit is contained in:
@@ -2,7 +2,14 @@
|
||||
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { authFetch, clearToken, getApiBase, getToken } from '../lib/auth'
|
||||
import {
|
||||
authFetch,
|
||||
authFetchOrThrow,
|
||||
ForbiddenError,
|
||||
getApiBase,
|
||||
getToken,
|
||||
UnauthorizedError,
|
||||
} from '../lib/auth'
|
||||
import AdminShell from '../ui/AdminShell'
|
||||
|
||||
type AdminSetting = {
|
||||
@@ -109,17 +116,8 @@ export default function SettingsPage({ section }: SettingsPageProps) {
|
||||
|
||||
const loadSettings = async () => {
|
||||
const baseUrl = getApiBase()
|
||||
const response = await authFetch(`${baseUrl}/admin/settings`)
|
||||
const response = await authFetchOrThrow(`${baseUrl}/admin/settings`)
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
clearToken()
|
||||
router.push('/login')
|
||||
return
|
||||
}
|
||||
if (response.status === 403) {
|
||||
router.push('/')
|
||||
return
|
||||
}
|
||||
throw new Error('Failed to load settings')
|
||||
}
|
||||
const data = await response.json()
|
||||
@@ -199,6 +197,14 @@ export default function SettingsPage({ section }: SettingsPageProps) {
|
||||
await loadArtworkPrefetchStatus()
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof UnauthorizedError) {
|
||||
router.push('/login')
|
||||
return
|
||||
}
|
||||
if (err instanceof ForbiddenError) {
|
||||
router.push('/')
|
||||
return
|
||||
}
|
||||
console.error(err)
|
||||
setStatus('Could not load admin settings.')
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user