auth: eliminar flash do login
- Simplifica AuthGuard para não redirecionar no cliente (gate feito no middleware) - Adiciona skeleton de carregamento no AppShell enquanto - Troca anchors por Next Link no sidebar para navegação client-side Sem mudanças de schema/DB; apenas UX e roteamento no cliente.
This commit is contained in:
parent
32488d48ca
commit
c88622d762
3 changed files with 61 additions and 43 deletions
|
|
@ -1,29 +1,14 @@
|
|||
"use client"
|
||||
|
||||
import { useEffect } from "react"
|
||||
import { usePathname, useRouter, useSearchParams } from "next/navigation"
|
||||
|
||||
import { useAuth } from "@/lib/auth-client"
|
||||
|
||||
// Melhor abordagem: sem redirecionamentos no cliente.
|
||||
// O middleware (middleware.ts) já gateia todas as rotas não públicas com base no cookie/sessão.
|
||||
// Mantemos este componente como no-op para evitar qualquer flash de /login.
|
||||
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])
|
||||
|
||||
// Podemos, se quisermos, ler isLoading para futuramente exibir um skeleton.
|
||||
// No momento, não fazemos nada aqui.
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { isLoading } = useAuth()
|
||||
return null
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue