fix(auth): simplificar ProtectedRoute* sem dependência do Supabase
- Profile do Supabase não existe pra usuários Logto - ProtectedRoute, ProtectedAdminRoute, ProtectedRouteByResource não buscam mais profile no Supabase - useUserRole: retorna isAdmin=true pra qualquer user logado no Logto - Reset de senha funcionou via Logto, só faltava o app liberar acesso
This commit is contained in:
+27
-70
@@ -1,84 +1,41 @@
|
||||
// Hook que retorna role/permissões do usuário
|
||||
// Migrado pra Logto: não depende mais do Supabase
|
||||
// Qualquer usuário autenticado via Logto tem acesso Total
|
||||
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { useAuth } from '@/hooks/useAuth';
|
||||
import type { UserRole, AppRole } from '@/hooks/useUserPermissions/types';
|
||||
|
||||
export type AccessLevel = 'Total' | 'Parcial' | 'Restrita';
|
||||
|
||||
export const useUserRole = () => {
|
||||
const { user } = useAuth();
|
||||
const { user, loading } = useAuth();
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: ['user-role', user?.id],
|
||||
queryFn: async () => {
|
||||
if (!user?.id) {
|
||||
return {
|
||||
accessLevel: 'Restrita' as AccessLevel,
|
||||
isAdmin: false,
|
||||
isGerencia: false,
|
||||
isDiretoria: false,
|
||||
role: 'user' as UserRole
|
||||
};
|
||||
}
|
||||
// Sem usuário = sem permissão
|
||||
if (!user) {
|
||||
return {
|
||||
accessLevel: 'Restrita' as AccessLevel,
|
||||
isAdmin: false,
|
||||
isGerencia: false,
|
||||
isDiretoria: false,
|
||||
role: 'user' as UserRole,
|
||||
loading: false,
|
||||
error: null,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const { data: profile, error } = await supabase
|
||||
.from('profiles')
|
||||
.select('id, full_name')
|
||||
.eq('id', user.id)
|
||||
.single();
|
||||
|
||||
if (error || !profile) {
|
||||
return {
|
||||
accessLevel: 'Restrita' as AccessLevel,
|
||||
isAdmin: false,
|
||||
isGerencia: false,
|
||||
isDiretoria: false,
|
||||
role: 'user' as UserRole
|
||||
};
|
||||
}
|
||||
|
||||
// For now, return default permissions until we have proper role system
|
||||
// This can be enhanced later with actual role checking
|
||||
const accessLevel = 'Total' as AccessLevel;
|
||||
const isAdmin = true; // Temporary - should be based on actual roles
|
||||
|
||||
return {
|
||||
accessLevel,
|
||||
isAdmin: true,
|
||||
isGerencia: false,
|
||||
isDiretoria: false,
|
||||
role: 'admin' as UserRole
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Erro ao buscar papel do usuário:', error);
|
||||
return {
|
||||
accessLevel: 'Restrita' as AccessLevel,
|
||||
isAdmin: false,
|
||||
isGerencia: false,
|
||||
isDiretoria: false,
|
||||
role: 'user' as UserRole
|
||||
};
|
||||
}
|
||||
},
|
||||
enabled: !!user?.id,
|
||||
});
|
||||
|
||||
// Extract the data and add loading/error handling
|
||||
const { data, isLoading, error } = query;
|
||||
|
||||
// Com usuário autenticado via Logto = Total
|
||||
return {
|
||||
...data,
|
||||
loading: isLoading,
|
||||
error,
|
||||
// Also include the query object for compatibility
|
||||
...query
|
||||
accessLevel: 'Total' as AccessLevel,
|
||||
isAdmin: true,
|
||||
isGerencia: true,
|
||||
isDiretoria: true,
|
||||
role: 'admin' as UserRole,
|
||||
loading,
|
||||
error: null,
|
||||
};
|
||||
};
|
||||
|
||||
// Helper function to check if user has role using correct AppRole type
|
||||
// Helper function (placeholder, não usado)
|
||||
export const hasRole = (userId: string | undefined, role: AppRole): boolean => {
|
||||
// Implementation would check against supabase function
|
||||
return false; // Placeholder
|
||||
};
|
||||
return false;
|
||||
};
|
||||
Reference in New Issue
Block a user