@@ -974,10 +974,28 @@ async function api<T>(path: string, init: RequestInit = {}): Promise<T> {
974974 headers .set (' Content-Type' , ' application/json' )
975975 if (token .value ) headers .set (' Authorization' , ` Bearer ${token .value } ` )
976976 const response = await fetch (path , { ... init , headers })
977- if (! response .ok ) throw new Error (await response .text ())
977+ if (! response .ok ) {
978+ const message = await response .text ()
979+ if (response .status === 401 && token .value && ! isAuthBootstrapPath (path )) {
980+ handleAuthExpired ()
981+ }
982+ throw new Error (message )
983+ }
978984 return response .json () as Promise <T >
979985}
980986
987+ function isAuthBootstrapPath(path : string ) {
988+ return path === ' /api/login' || path === ' /api/setup' || path === ' /api/setup-status'
989+ }
990+
991+ function handleAuthExpired() {
992+ token .value = ' '
993+ clearStoredToken ()
994+ stopDashboardPolling ()
995+ mode .value = ' login'
996+ error .value = ' 登录已过期,请重新登录'
997+ }
998+
981999async function fetchPublicKey() {
9821000 const response = await api <PublicKeyResponse >(' /api/public-key' )
9831001 if (! hasWebCrypto ()) {
@@ -999,7 +1017,7 @@ async function fetchPublicKey() {
9991017}
10001018
10011019async function encryptPayload(name : string , value : unknown ) {
1002- if ( ! publicKey . value ) publicKey .value = await fetchPublicKey ()
1020+ publicKey .value = await fetchPublicKey ()
10031021 const aesKey = randomBytes (32 )
10041022 const fieldName = randomFieldName ()
10051023 const iv = randomBytes (12 )
0 commit comments