24 lines
589 B
TypeScript
24 lines
589 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) => {
|
|
if (isAuthenticated) {
|
|
action()
|
|
} else {
|
|
openLoginDialog(action)
|
|
}
|
|
},
|
|
[isAuthenticated, openLoginDialog],
|
|
)
|
|
|
|
return { isAuthenticated, requireAuth }
|
|
}
|