feat: global login dialog with useRequireAuth hook for 401 auth gating
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
"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()
|
||||
}
|
||||
},
|
||||
[isAuthenticated, openLoginDialog],
|
||||
)
|
||||
|
||||
return { isAuthenticated, requireAuth }
|
||||
}
|
||||
Reference in New Issue
Block a user