feat: tighten auth guard in sidebar shell

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
rever-tecnologia 2025-10-06 11:17:07 -03:00
parent 1a71d49b4d
commit fe7025d433
10 changed files with 266 additions and 152 deletions

View file

@ -0,0 +1,29 @@
"use client"
import { useEffect } from "react"
import { usePathname, useRouter, useSearchParams } from "next/navigation"
import { useAuth } from "@/lib/auth-client"
export function AuthGuard() {
const { session, isLoading } = useAuth()
const router = useRouter()
const pathname = usePathname()
const searchParams = useSearchParams()
useEffect(() => {
if (isLoading) return
if (session?.user) return
const search = searchParams?.toString()
const callbackUrl = pathname
? search && search.length > 0
? `${pathname}?${search}`
: pathname
: undefined
const nextUrl = callbackUrl ? `/login?callbackUrl=${encodeURIComponent(callbackUrl)}` : "/login"
router.replace(nextUrl)
}, [isLoading, session?.user, pathname, searchParams, router])
return null
}