Merge pull request #40 from rede5/codex/fix-dropdown-disappearance-issue
Fix dropdown role loading from storage
This commit is contained in:
commit
051d4a015c
1 changed files with 29 additions and 2 deletions
|
|
@ -11,6 +11,15 @@ export const useAuth = (): AuthContextType => {
|
||||||
const [userRole, setUserRole] = useState<UserRole | null>(null);
|
const [userRole, setUserRole] = useState<UserRole | null>(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
||||||
|
const normalizeRole = useCallback((role?: string | null): UserRole | null => {
|
||||||
|
if (!role) return null;
|
||||||
|
const normalized = role.toLowerCase();
|
||||||
|
if (normalized === UserRole.SUPERADMIN) return UserRole.SUPERADMIN;
|
||||||
|
if (normalized === UserRole.ADMIN) return UserRole.ADMIN;
|
||||||
|
if (normalized === UserRole.COLABORADOR) return UserRole.COLABORADOR;
|
||||||
|
if (normalized === UserRole.ENTREGADOR) return UserRole.ENTREGADOR;
|
||||||
|
return null;
|
||||||
|
}, []);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifica se o usuário tem um role específico
|
* Verifica se o usuário tem um role específico
|
||||||
|
|
@ -95,7 +104,25 @@ export const useAuth = (): AuthContextType => {
|
||||||
// Marcar como autenticado se há token
|
// Marcar como autenticado se há token
|
||||||
setIsAuthenticated(true);
|
setIsAuthenticated(true);
|
||||||
|
|
||||||
// Por enquanto, não buscar dados do usuário - apenas marcar como autenticado
|
const storedUser = localStorage.getItem('user');
|
||||||
|
if (storedUser) {
|
||||||
|
try {
|
||||||
|
const parsedUser = JSON.parse(storedUser) as UserData;
|
||||||
|
setUser(parsedUser);
|
||||||
|
const storedRole =
|
||||||
|
parsedUser?.nivel ||
|
||||||
|
(parsedUser as unknown as { role?: string; userRole?: string })?.role ||
|
||||||
|
(parsedUser as unknown as { role?: string; userRole?: string })?.userRole;
|
||||||
|
setUserRole(normalizeRole(storedRole));
|
||||||
|
} catch (parseError) {
|
||||||
|
console.error('Erro ao ler usuário salvo:', parseError);
|
||||||
|
setUser(null);
|
||||||
|
setUserRole(null);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setUser(null);
|
||||||
|
setUserRole(null);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erro na verificação de autenticação:', error);
|
console.error('Erro na verificação de autenticação:', error);
|
||||||
|
|
@ -126,4 +153,4 @@ export const useAuth = (): AuthContextType => {
|
||||||
getUserPermissions: () => typeof ROLE_PERMISSIONS[UserRole] | null;
|
getUserPermissions: () => typeof ROLE_PERMISSIONS[UserRole] | null;
|
||||||
redirectToDefaultRoute: () => void;
|
redirectToDefaultRoute: () => void;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue