fix: session auth issues

This commit is contained in:
swve 2024-05-27 20:58:32 +02:00
parent 1708b36818
commit 08cc97f557
70 changed files with 607 additions and 427 deletions

View file

@ -1,43 +1,40 @@
import { useOrg } from '@components/Contexts/OrgContext'
import { useSession } from 'next-auth/react'
import { useEffect } from 'react'
import { useOrg } from '@components/Contexts/OrgContext';
import { useLHSession } from '@components/Contexts/LHSessionContext';
import { useEffect, useState, useMemo } from 'react';
interface Role {
org: { id: number };
role: { id: number; role_uuid: string };
}
function useAdminStatus() {
const session = useSession() as any
const org = useOrg() as any
console.log('useAdminStatus', {
session,
})
const session = useLHSession() as any;
const org = useOrg() as any;
const [isAdmin, setIsAdmin] = useState<boolean | null>(null);
const [loading, setLoading] = useState<boolean>(true);
// If session is not loaded, redirect to login
const userRoles = useMemo(() => session?.data?.roles || [], [session?.data?.roles]);
useEffect(() => {
if (session.status == 'loading') {
return
}
}
, [session])
const isUserAdmin = () => {
if (session.status == 'authenticated') {
const isAdmin = session?.data?.roles.some((role: any) => {
if (session.status === 'authenticated' && org?.id) {
const isAdminVar = userRoles.some((role: Role) => {
return (
role.org.id === org.id &&
(role.role.id === 1 ||
role.role.id === 2 ||
role.role.role_uuid === 'role_global_admin' ||
role.role.role_uuid === 'role_global_maintainer')
)
})
return isAdmin
);
});
setIsAdmin(isAdminVar);
setLoading(false); // Set loading to false once the status is determined
} else {
setIsAdmin(false);
setLoading(false); // Set loading to false if not authenticated or org not found
}
return false
}
// Return the user admin status
return isUserAdmin()
}, [session.status, userRoles, org.id]);
return { isAdmin, loading };
}
export default useAdminStatus
export default useAdminStatus;