c9dbf5037e
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.
24 lines
610 B
TypeScript
24 lines
610 B
TypeScript
"use client"
|
|
|
|
import { useCallback } from "react"
|
|
import { useAuthStore } from "@/store/auth"
|
|
import { useLoginDialogStore } from "@/store/login-dialog"
|
|
|
|
export function useRequireAuth() {
|
|
const isAuthenticated = useAuthStore((s) => s.isAuthenticated)
|
|
const openLoginDialog = useLoginDialogStore((s) => s.openLoginDialog)
|
|
|
|
const requireAuth = useCallback(
|
|
(action: () => void | Promise<void>) => {
|
|
if (isAuthenticated) {
|
|
void action()
|
|
} else {
|
|
openLoginDialog(action)
|
|
}
|
|
},
|
|
[isAuthenticated, openLoginDialog],
|
|
)
|
|
|
|
return { isAuthenticated, requireAuth }
|
|
}
|