fix: redirect authenticated dash login
This commit is contained in:
@@ -4,7 +4,7 @@ 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 } from '@/lib/auth';
|
||||
import { saveAuth, isAuthenticated } from '@/lib/auth';
|
||||
import dynamic from 'next/dynamic';
|
||||
|
||||
const ThemeToggle = dynamic(() => import('@/components/ThemeToggle'), { ssr: false });
|
||||
@@ -20,12 +20,17 @@ export default function LoginPage() {
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
// Detectar se é dash (SUPERADMIN) ou agência
|
||||
if (typeof window !== 'undefined') {
|
||||
const hostname = window.location.hostname;
|
||||
const sub = hostname.split('.')[0];
|
||||
const superAdmin = sub === 'dash';
|
||||
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();
|
||||
|
||||
localStorage.setItem('token', data.token);
|
||||
localStorage.setItem('user', JSON.stringify(data.user));
|
||||
saveAuth(data.token, data.user);
|
||||
|
||||
console.log('Login successful:', data.user);
|
||||
|
||||
toast.success('Login realizado com sucesso! Redirecionando...');
|
||||
|
||||
setTimeout(() => {
|
||||
window.location.href = '/dashboard';
|
||||
const target = isSuperAdmin ? '/superadmin' : '/dashboard';
|
||||
window.location.href = target;
|
||||
}, 1000);
|
||||
} catch (error: any) {
|
||||
toast.error(error.message || 'Erro ao fazer login. Verifique suas credenciais.');
|
||||
|
||||
Reference in New Issue
Block a user