fix: enhance admin user validation and logging in AdminLayout
This commit is contained in:
parent
e81d5ac424
commit
54f3d48c1d
@ -6,6 +6,9 @@ import useAuthStore from '../store/authStore'
|
||||
|
||||
function isUserAdmin(user: any): boolean {
|
||||
if (!user) return false
|
||||
if (user.user && typeof user.user === 'object') {
|
||||
return isUserAdmin(user.user)
|
||||
}
|
||||
const role = user.role ?? user.userType ?? user.user_type
|
||||
if (role === 'admin' || role === 'super_admin') return true
|
||||
if (user.isAdmin === true || user.isSuperAdmin === true) return true
|
||||
@ -32,8 +35,16 @@ export default function AdminLayout({ children }: { children: React.ReactNode })
|
||||
const guard = async () => {
|
||||
if (!mounted || !isAuthReady) return
|
||||
|
||||
console.log('🔐 AdminLayout guard:start', {
|
||||
mounted,
|
||||
isAuthReady,
|
||||
hasUser: !!user,
|
||||
userRole: (user && (user.role ?? user.userType ?? user.user_type)) || null
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
try {
|
||||
console.log('🔐 AdminLayout: no user, attempting refresh')
|
||||
await refreshAuthToken?.()
|
||||
} catch {}
|
||||
}
|
||||
@ -41,6 +52,12 @@ export default function AdminLayout({ children }: { children: React.ReactNode })
|
||||
const currentUser = useAuthStore.getState().user
|
||||
const ok = isUserAdmin(currentUser)
|
||||
|
||||
console.log('🔐 AdminLayout guard:resolved', {
|
||||
hasUser: !!currentUser,
|
||||
userRole: currentUser && (currentUser.role ?? currentUser.userType ?? currentUser.user_type),
|
||||
isAdmin: ok
|
||||
})
|
||||
|
||||
if (!currentUser) {
|
||||
router.replace('/login')
|
||||
return
|
||||
|
||||
Loading…
Reference in New Issue
Block a user