fix(auth): handle zero-permission users gracefully and prevent redirect loops

This commit is contained in:
2026-08-11 07:36:01 +08:00
parent 04c12ad340
commit 2e02eee66c
6 changed files with 84 additions and 7 deletions
+46 -2
View File
@@ -1,7 +1,51 @@
import { createFileRoute, redirect } from '@tanstack/react-router'
import { createFileRoute, redirect, useNavigate } from '@tanstack/react-router'
import { ShieldAlert, LogOut } from 'lucide-react'
import { getFirstAccessibleRoute } from '@/components/layout/nav'
import { useAuthStore } from '@/auth/store'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
export const Route = createFileRoute('/_authed/')({
beforeLoad: () => {
throw redirect({ to: '/categories' })
const target = getFirstAccessibleRoute()
if (target && target !== '/') {
throw redirect({ to: target })
}
},
component: DashboardIndexPage,
})
function DashboardIndexPage() {
const navigate = useNavigate()
const user = useAuthStore((s) => s.user)
const logout = () => {
useAuthStore.getState().clear()
void navigate({ to: '/login' })
}
return (
<div className="flex min-h-[60vh] items-center justify-center p-4">
<Card className="w-full max-w-md text-center shadow-xs">
<CardHeader>
<div className="mx-auto mb-2 flex size-12 items-center justify-center rounded-full bg-amber-500/10 text-amber-600 dark:bg-amber-500/20 dark:text-amber-400">
<ShieldAlert className="size-6" />
</div>
<CardTitle className="text-xl"></CardTitle>
<CardDescription>
{user?.display_name ?? user?.username ?? user?.uid}
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<p className="text-xs text-muted-foreground">
访
</p>
<Button variant="outline" onClick={logout} className="gap-2">
<LogOut className="size-4" />
退
</Button>
</CardContent>
</Card>
</div>
)
}