feat: versão 1.5 - CRM Beta com leads, funis, campanhas e portal do cliente
This commit is contained in:
@@ -2,9 +2,14 @@
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useRouter, usePathname } from 'next/navigation';
|
||||
import { isAuthenticated, clearAuth } from '@/lib/auth';
|
||||
import { isAuthenticated, getUser, clearAuth } from '@/lib/auth';
|
||||
|
||||
export default function AuthGuard({ children }: { children: React.ReactNode }) {
|
||||
interface AuthGuardProps {
|
||||
children: React.ReactNode;
|
||||
allowedTypes?: ('agency_user' | 'customer' | 'superadmin')[];
|
||||
}
|
||||
|
||||
export default function AuthGuard({ children, allowedTypes }: AuthGuardProps) {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const [authorized, setAuthorized] = useState<boolean | null>(null);
|
||||
@@ -19,16 +24,34 @@ export default function AuthGuard({ children }: { children: React.ReactNode }) {
|
||||
|
||||
const checkAuth = () => {
|
||||
const isAuth = isAuthenticated();
|
||||
const user = getUser();
|
||||
|
||||
if (!isAuth) {
|
||||
setAuthorized(false);
|
||||
// Evitar redirect loop se já estiver no login
|
||||
if (pathname !== '/login') {
|
||||
router.push('/login?error=unauthorized');
|
||||
}
|
||||
} else {
|
||||
setAuthorized(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Verificar tipo de usuário se especificado
|
||||
if (allowedTypes && user) {
|
||||
const userType = user.user_type;
|
||||
if (!userType || !allowedTypes.includes(userType)) {
|
||||
console.warn(`🚫 Access denied for user type: ${userType}. Allowed: ${allowedTypes}`);
|
||||
setAuthorized(false);
|
||||
|
||||
// Redirecionar para o dashboard apropriado se estiver no lugar errado
|
||||
if (userType === 'customer') {
|
||||
router.push('/cliente/dashboard');
|
||||
} else {
|
||||
router.push('/login?error=forbidden');
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setAuthorized(true);
|
||||
};
|
||||
|
||||
checkAuth();
|
||||
|
||||
Reference in New Issue
Block a user