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

View File

@ -1,5 +1,6 @@
import { redirect } from '@tanstack/react-router'
import { can, useAuthStore } from '@/auth/store'
import { getFirstAccessibleRoute } from '@/components/layout/nav'
/**
* Admin permission keys — mirrors the backend catalog
@ -50,6 +51,7 @@ export type Permission = (typeof PERM)[keyof typeof PERM]
export function requirePermission(permission: Permission): void {
const { user } = useAuthStore.getState()
if (user && !can(permission)) {
throw redirect({ to: '/' })
const target = getFirstAccessibleRoute()
throw redirect({ to: target && target !== '/' ? target : '/' })
}
}