fix: correct generate endpoint return type, migration server_default SQL, and auth loading propagation
This commit is contained in:
@@ -10,20 +10,22 @@ export interface User {
|
||||
|
||||
// Simple module-level state (no external lib needed)
|
||||
let currentUser: User | null = null
|
||||
let isLoading = true
|
||||
let fetchPromise: Promise<void> | null = null
|
||||
const listeners = new Set<() => void>()
|
||||
|
||||
export function useAuth() {
|
||||
const [user, setUser] = useState<User | null>(currentUser)
|
||||
const [loading, setLoading] = useState(currentUser === null)
|
||||
const [loading, setLoading] = useState(isLoading)
|
||||
|
||||
useEffect(() => {
|
||||
const update = () => setUser(currentUser)
|
||||
const update = () => { setUser(currentUser); setLoading(isLoading) }
|
||||
listeners.add(update)
|
||||
if (currentUser === null) {
|
||||
authApi.me()
|
||||
.then((res) => { currentUser = res.data; listeners.forEach((l) => l()) })
|
||||
.catch(() => { currentUser = null; listeners.forEach((l) => l()) })
|
||||
.finally(() => setLoading(false))
|
||||
if (!fetchPromise) {
|
||||
fetchPromise = authApi.me()
|
||||
.then((res) => { currentUser = res.data })
|
||||
.catch(() => { currentUser = null })
|
||||
.finally(() => { isLoading = false; listeners.forEach((l) => l()) })
|
||||
}
|
||||
return () => { listeners.delete(update) }
|
||||
}, [])
|
||||
|
||||
Reference in New Issue
Block a user