fix: redirect authenticated dash login

This commit is contained in:
Erik Silva
2025-12-09 01:58:39 -03:00
parent f553114c06
commit a33fb2f544

View File

@@ -4,7 +4,7 @@ import { useState, useEffect } from "react";
import Link from "next/link"; import Link from "next/link";
import { Button, Input, Checkbox } from "@/components/ui"; import { Button, Input, Checkbox } from "@/components/ui";
import toast, { Toaster } from 'react-hot-toast'; import toast, { Toaster } from 'react-hot-toast';
import { saveAuth } from '@/lib/auth'; import { saveAuth, isAuthenticated } from '@/lib/auth';
import dynamic from 'next/dynamic'; import dynamic from 'next/dynamic';
const ThemeToggle = dynamic(() => import('@/components/ThemeToggle'), { ssr: false }); const ThemeToggle = dynamic(() => import('@/components/ThemeToggle'), { ssr: false });
@@ -20,12 +20,17 @@ export default function LoginPage() {
}); });
useEffect(() => { useEffect(() => {
// Detectar se é dash (SUPERADMIN) ou agência
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
const hostname = window.location.hostname; const hostname = window.location.hostname;
const sub = hostname.split('.')[0]; const sub = hostname.split('.')[0];
const superAdmin = sub === 'dash';
setSubdomain(sub); setSubdomain(sub);
setIsSuperAdmin(sub === 'dash'); setIsSuperAdmin(superAdmin);
if (isAuthenticated()) {
const target = superAdmin ? '/superadmin' : '/dashboard';
window.location.href = target;
}
} }
}, []); }, []);
@@ -68,15 +73,15 @@ export default function LoginPage() {
const data = await response.json(); const data = await response.json();
localStorage.setItem('token', data.token); saveAuth(data.token, data.user);
localStorage.setItem('user', JSON.stringify(data.user));
console.log('Login successful:', data.user); console.log('Login successful:', data.user);
toast.success('Login realizado com sucesso! Redirecionando...'); toast.success('Login realizado com sucesso! Redirecionando...');
setTimeout(() => { setTimeout(() => {
window.location.href = '/dashboard'; const target = isSuperAdmin ? '/superadmin' : '/dashboard';
window.location.href = target;
}, 1000); }, 1000);
} catch (error: any) { } catch (error: any) {
toast.error(error.message || 'Erro ao fazer login. Verifique suas credenciais.'); toast.error(error.message || 'Erro ao fazer login. Verifique suas credenciais.');