Files
zetaloop c9dbf5037e refactor(auth): support async deferred actions for login gating
Allow deferred login actions to return promises and execute them safely without violating lint rules. Also initialize order detail timer state without impure render-time Date.now calls so React hook purity checks pass.
2026-02-22 09:50:48 +08:00

25 lines
648 B
TypeScript

import { useLoginDialogStore } from "@/store/login-dialog"
type RequestExecutor<T> = () => Promise<T>
interface RequestOptions {
onUnauthorized?: () => void | Promise<void>
}
export async function requestWithAuth<T>(executor: RequestExecutor<T>, options?: RequestOptions) {
try {
return await executor()
} catch (error) {
if (error instanceof Error && error.message === "UNAUTHORIZED") {
if (options?.onUnauthorized) {
useLoginDialogStore.getState().openLoginDialog(options.onUnauthorized)
} else {
useLoginDialogStore.getState().openLoginDialog()
}
return null
}
throw error
}
}