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

@ -23,10 +23,11 @@ export default function LoginPage() {
const [isHydrated, setIsHydrated] = useState(false)
useEffect(() => {
if (isPending) return
if (!session?.user) return
const destination = callbackUrl ?? "/dashboard"
router.replace(destination)
}, [callbackUrl, router, session?.user])
}, [callbackUrl, isPending, router, session?.user])
useEffect(() => {
setIsHydrated(true)

View file

@ -1,17 +1,22 @@
import { AppShell } from "@/components/app-shell"
import { BacklogReport } from "@/components/reports/backlog-report"
import { SiteHeader } from "@/components/site-header"
export const dynamic = "force-dynamic"
export default function ReportsBacklogPage() {
return (
<main className="mx-auto w-full max-w-6xl px-4 py-10 lg:px-0">
<header className="mb-8 space-y-2">
<h1 className="text-3xl font-semibold tracking-tight text-neutral-900">Backlog e Prioridades</h1>
<p className="text-sm text-neutral-600">
Avalie o volume de tickets em aberto, prioridades e filas mais pressionadas.
</p>
</header>
<BacklogReport />
</main>
<AppShell
header={
<SiteHeader
title="Backlog e Prioridades"
lead="Avalie o volume de tickets em aberto, prioridades e filas mais pressionadas."
/>
}
>
<div className="mx-auto w-full max-w-6xl px-4 pb-12 lg:px-6">
<BacklogReport />
</div>
</AppShell>
)
}

View file

@ -1,17 +1,22 @@
import { AppShell } from "@/components/app-shell"
import { CsatReport } from "@/components/reports/csat-report"
import { SiteHeader } from "@/components/site-header"
export const dynamic = "force-dynamic"
export default function ReportsCsatPage() {
return (
<main className="mx-auto w-full max-w-6xl px-4 py-10 lg:px-0">
<header className="mb-8 space-y-2">
<h1 className="text-3xl font-semibold tracking-tight text-neutral-900">Relatório de CSAT</h1>
<p className="text-sm text-neutral-600">
Visualize a satisfação dos clientes e identifique pontos de melhoria na entrega.
</p>
</header>
<CsatReport />
</main>
<AppShell
header={
<SiteHeader
title="Relatório de CSAT"
lead="Visualize a satisfação dos clientes e identifique pontos de melhoria na entrega."
/>
}
>
<div className="mx-auto w-full max-w-6xl px-4 pb-12 lg:px-6">
<CsatReport />
</div>
</AppShell>
)
}

View file

@ -1,17 +1,22 @@
import { AppShell } from "@/components/app-shell"
import { SlaReport } from "@/components/reports/sla-report"
import { SiteHeader } from "@/components/site-header"
export const dynamic = "force-dynamic"
export default function ReportsSlaPage() {
return (
<main className="mx-auto w-full max-w-6xl px-4 py-10 lg:px-0">
<header className="mb-8 space-y-2">
<h1 className="text-3xl font-semibold tracking-tight text-neutral-900">Relatório de SLA</h1>
<p className="text-sm text-neutral-600">
Acompanhe tempos de resposta, resolução e balanço de filas em tempo real.
</p>
</header>
<SlaReport />
</main>
<AppShell
header={
<SiteHeader
title="Relatório de SLA"
lead="Acompanhe tempos de resposta, resolução e balanço de filas em tempo real."
/>
}
>
<div className="mx-auto w-full max-w-6xl px-4 pb-12 lg:px-6">
<SlaReport />
</div>
</AppShell>
)
}

View file

@ -150,6 +150,7 @@ export default function NewTicketPage() {
className="min-h-[96px] w-full rounded-lg border border-slate-300 bg-white px-3 py-2 text-sm text-neutral-800 shadow-sm outline-none transition-colors focus-visible:border-[#00d6eb] focus-visible:ring-[3px] focus-visible:ring-[#00e8ff]/20"
value={summary}
onChange={(event) => setSummary(event.target.value)}
placeholder="Resuma rapidamente o cenário ou impacto do ticket."
/>
</div>
<div className="space-y-2">

View file

@ -1,6 +1,7 @@
import type { ReactNode } from "react"
import { AppSidebar } from "@/components/app-sidebar"
import { AppSidebar } from "@/components/app-sidebar"
import { AuthGuard } from "@/components/auth/auth-guard"
import { SidebarInset, SidebarProvider } from "@/components/ui/sidebar"
interface AppShellProps {
@ -13,6 +14,7 @@ export function AppShell({ header, children }: AppShellProps) {
<SidebarProvider>
<AppSidebar />
<SidebarInset>
<AuthGuard />
{header}
<main className="flex flex-1 flex-col gap-8 bg-gradient-to-br from-background via-background to-primary/10 pb-12 pt-6">
{children}

View file

@ -21,19 +21,21 @@ import { usePathname } from "next/navigation"
import { SearchForm } from "@/components/search-form"
import { VersionSwitcher } from "@/components/version-switcher"
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarHeader,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarRail,
} from "@/components/ui/sidebar"
import {
Sidebar,
SidebarContent,
SidebarFooter,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarHeader,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarRail,
} from "@/components/ui/sidebar"
import { Skeleton } from "@/components/ui/skeleton"
import { NavUser } from "@/components/nav-user"
import { useAuth } from "@/lib/auth-client"
import type { LucideIcon } from "lucide-react"
@ -103,7 +105,7 @@ const navigation: { versions: string[]; navMain: NavigationGroup[] } = {
export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
const pathname = usePathname()
const { isAdmin, isStaff, isCustomer } = useAuth()
const { session, isLoading, isAdmin, isStaff, isCustomer } = useAuth()
const [isHydrated, setIsHydrated] = React.useState(false)
React.useEffect(() => {
@ -194,6 +196,25 @@ export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
)
})}
</SidebarContent>
<SidebarFooter>
{isLoading ? (
<div className="flex items-center gap-3 rounded-lg border border-border/70 bg-sidebar p-3 shadow-sm">
<Skeleton className="h-9 w-9 rounded-lg" />
<div className="flex-1 space-y-1">
<Skeleton className="h-3.5 w-24 rounded" />
<Skeleton className="h-3 w-32 rounded" />
</div>
</div>
) : (
<NavUser
user={{
name: session?.user?.name,
email: session?.user?.email,
avatarUrl: session?.user?.avatarUrl ?? undefined,
}}
/>
)}
</SidebarFooter>
<SidebarRail />
</Sidebar>
)

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
}

View file

@ -1,110 +1,154 @@
"use client"
import {
IconCreditCard,
IconDotsVertical,
IconLogout,
IconNotification,
IconUserCircle,
} from "@tabler/icons-react"
import {
Avatar,
AvatarFallback,
AvatarImage,
} from "@/components/ui/avatar"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import {
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
useSidebar,
} from "@/components/ui/sidebar"
export function NavUser({
user,
}: {
user: {
name: string
email: string
avatar: string
}
}) {
const { isMobile } = useSidebar()
return (
<SidebarMenu>
<SidebarMenuItem>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<SidebarMenuButton
size="lg"
className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground"
>
<Avatar className="h-8 w-8 rounded-lg grayscale">
<AvatarImage src={user.avatar} alt={user.name} />
<AvatarFallback className="rounded-lg">CN</AvatarFallback>
</Avatar>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-medium">{user.name}</span>
<span className="text-muted-foreground truncate text-xs">
{user.email}
</span>
</div>
<IconDotsVertical className="ml-auto size-4" />
</SidebarMenuButton>
</DropdownMenuTrigger>
<DropdownMenuContent
className="w-(--radix-dropdown-menu-trigger-width) min-w-56 rounded-lg"
side={isMobile ? "bottom" : "right"}
align="end"
sideOffset={4}
>
<DropdownMenuLabel className="p-0 font-normal">
<div className="flex items-center gap-2 px-1 py-1.5 text-left text-sm">
<Avatar className="h-8 w-8 rounded-lg">
<AvatarImage src={user.avatar} alt={user.name} />
<AvatarFallback className="rounded-lg">CN</AvatarFallback>
</Avatar>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-medium">{user.name}</span>
<span className="text-muted-foreground truncate text-xs">
{user.email}
</span>
</div>
</div>
</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuItem>
<IconUserCircle />
Account
</DropdownMenuItem>
<DropdownMenuItem>
<IconCreditCard />
Billing
</DropdownMenuItem>
<DropdownMenuItem>
<IconNotification />
Notifications
</DropdownMenuItem>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuItem>
<IconLogout />
Log out
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</SidebarMenuItem>
</SidebarMenu>
)
}
"use client"
import { useCallback, useMemo, useState } from "react"
import { useRouter } from "next/navigation"
import {
IconDotsVertical,
IconLogout,
IconNotification,
IconUserCircle,
} from "@tabler/icons-react"
import { toast } from "sonner"
import {
Avatar,
AvatarFallback,
AvatarImage,
} from "@/components/ui/avatar"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import {
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
useSidebar,
} from "@/components/ui/sidebar"
import { signOut } from "@/lib/auth-client"
type NavUserProps = {
user?: {
name?: string | null
email?: string | null
avatarUrl?: string | null
} | null
}
export function NavUser({ user }: NavUserProps) {
const normalizedUser = user ?? { name: null, email: null, avatarUrl: null }
const { isMobile } = useSidebar()
const router = useRouter()
const [isSigningOut, setIsSigningOut] = useState(false)
const initials = useMemo(() => {
const source = normalizedUser.name?.trim() || normalizedUser.email?.trim() || ""
if (!source) return "US"
const parts = source.split(" ").filter(Boolean)
const firstTwo = parts.slice(0, 2).map((part) => part[0]).join("")
if (firstTwo) return firstTwo.toUpperCase()
return source.slice(0, 2).toUpperCase()
}, [normalizedUser.name, normalizedUser.email])
const displayName = normalizedUser.name?.trim() || "Usuário"
const displayEmail = normalizedUser.email?.trim() || "Sem e-mail definido"
const handleProfile = useCallback(() => {
router.push("/settings")
}, [router])
const handleSignOut = useCallback(async () => {
if (isSigningOut) return
setIsSigningOut(true)
try {
await signOut()
toast.success("Sessão encerrada.")
router.replace("/login")
} catch (error) {
console.error("Erro ao encerrar sessão", error)
toast.error("Não foi possível encerrar a sessão.")
} finally {
setIsSigningOut(false)
}
}, [isSigningOut, router])
return (
<SidebarMenu>
<SidebarMenuItem>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<SidebarMenuButton
size="lg"
className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground"
>
<Avatar className="h-8 w-8 rounded-lg grayscale">
<AvatarImage src={normalizedUser.avatarUrl ?? undefined} alt={displayName} />
<AvatarFallback className="rounded-lg">{initials}</AvatarFallback>
</Avatar>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-medium">{displayName}</span>
<span className="text-muted-foreground truncate text-xs">
{displayEmail}
</span>
</div>
<IconDotsVertical className="ml-auto size-4" />
</SidebarMenuButton>
</DropdownMenuTrigger>
<DropdownMenuContent
className="w-(--radix-dropdown-menu-trigger-width) min-w-56 rounded-lg"
side={isMobile ? "bottom" : "right"}
align="end"
sideOffset={4}
>
<DropdownMenuLabel className="p-0 font-normal">
<div className="flex items-center gap-2 px-1 py-1.5 text-left text-sm">
<Avatar className="h-8 w-8 rounded-lg">
<AvatarImage src={normalizedUser.avatarUrl ?? undefined} alt={displayName} />
<AvatarFallback className="rounded-lg">{initials}</AvatarFallback>
</Avatar>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-medium">{displayName}</span>
<span className="text-muted-foreground truncate text-xs">
{displayEmail}
</span>
</div>
</div>
</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuItem
onSelect={(event) => {
event.preventDefault()
handleProfile()
}}
>
<IconUserCircle className="size-4" />
<span>Meu perfil</span>
</DropdownMenuItem>
<DropdownMenuItem disabled>
<IconNotification className="size-4" />
<span>Notificações (em breve)</span>
</DropdownMenuItem>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuItem
onSelect={(event) => {
event.preventDefault()
handleSignOut()
}}
disabled={isSigningOut}
>
<IconLogout className="size-4" />
<span>{isSigningOut ? "Encerrando…" : "Encerrar sessão"}</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</SidebarMenuItem>
</SidebarMenu>
)
}

View file

@ -199,6 +199,7 @@ export function NewTicketDialog() {
id="summary"
className="min-h-[96px] w-full rounded-lg border border-slate-300 bg-background p-3 text-sm shadow-sm focus-visible:border-[#00d6eb] focus-visible:outline-none"
{...form.register("summary")}
placeholder="Explique em poucas linhas o contexto do chamado."
/>
</Field>
<Field>