Files
OpenFlare/openflare-server/frontend/hooks/use-auth-redirect.ts
T
ryan 88360350a0 refactor(repo): replace legacy openflare-server with Wavelet rename
Delete the old monolithic openflare-server implementation and rename
Wavelet/ to openflare-server/ to complete the migration consolidation.
Update CI workflows, agent Dockerfiles, and deployment docs for the new
layout (frontend/, docker/Dockerfile).
2026-06-19 11:29:17 +08:00

27 lines
834 B
TypeScript

"use client"
import {useEffect} from "react"
import {usePathname, useRouter} from "next/navigation"
import {useUser} from "@/contexts/user-context"
/** Redirects unauthenticated users to login while allowing the shell to render first. */
export function useAuthRedirect() {
const router = useRouter()
const pathname = usePathname()
const {user, loading} = useUser()
useEffect(() => {
if (loading || user) {
return
}
const queryString = window.location.search
const callbackUrl = queryString ? `${pathname}${queryString}` : pathname
const loginUrl = new URL("/login", window.location.origin)
loginUrl.searchParams.set("callbackUrl", callbackUrl)
sessionStorage.setItem("redirect_after_login", callbackUrl)
router.replace(loginUrl.toString())
}, [loading, pathname, router, user])
}