feat: Implementação de submenus laterais (flyout), correções de UI e proteção de rotas (AuthGuard)
This commit is contained in:
@@ -4,7 +4,8 @@ import { useState, useEffect } from "react";
|
||||
import Link from "next/link";
|
||||
import { Button, Input, Checkbox } from "@/components/ui";
|
||||
import toast, { Toaster } from 'react-hot-toast';
|
||||
import { saveAuth, isAuthenticated } from '@/lib/auth';
|
||||
import { saveAuth, isAuthenticated, getToken, clearAuth } from '@/lib/auth';
|
||||
import { API_ENDPOINTS } from '@/lib/api';
|
||||
import dynamic from 'next/dynamic';
|
||||
|
||||
const ThemeToggle = dynamic(() => import('@/components/ThemeToggle'), { ssr: false });
|
||||
@@ -53,8 +54,26 @@ export default function LoginPage() {
|
||||
}
|
||||
|
||||
if (isAuthenticated()) {
|
||||
const target = superAdmin ? '/superadmin' : '/dashboard';
|
||||
window.location.href = target;
|
||||
// Validar token antes de redirecionar para evitar loops
|
||||
const token = getToken();
|
||||
fetch(API_ENDPOINTS.me, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
})
|
||||
.then(res => {
|
||||
if (res.ok) {
|
||||
const target = superAdmin ? '/superadmin' : '/dashboard';
|
||||
window.location.href = target;
|
||||
} else {
|
||||
// Token inválido ou expirado
|
||||
clearAuth();
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Erro ao validar sessão:', err);
|
||||
// Em caso de erro de rede, não redireciona nem limpa, deixa o usuário tentar logar
|
||||
});
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
Reference in New Issue
Block a user