hardening
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { authFetch, clearToken, getApiBase, getToken } from '../lib/auth'
|
||||
import { authFetchOrThrow, getApiBase, getToken, UnauthorizedError } from '../lib/auth'
|
||||
|
||||
export default function HeaderIdentity() {
|
||||
const [identity, setIdentity] = useState<string | null>(null)
|
||||
@@ -16,9 +16,8 @@ export default function HeaderIdentity() {
|
||||
const load = async () => {
|
||||
try {
|
||||
const baseUrl = getApiBase()
|
||||
const response = await authFetch(`${baseUrl}/auth/me`)
|
||||
const response = await authFetchOrThrow(`${baseUrl}/auth/me`)
|
||||
if (!response.ok) {
|
||||
clearToken()
|
||||
setIdentity(null)
|
||||
return
|
||||
}
|
||||
@@ -27,6 +26,10 @@ export default function HeaderIdentity() {
|
||||
setIdentity(`${data.username}${data.role ? ` (${data.role})` : ''}`)
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof UnauthorizedError) {
|
||||
setIdentity(null)
|
||||
return
|
||||
}
|
||||
console.error(err)
|
||||
setIdentity(null)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user