'use client' import { createContext, useContext, useEffect, useState, ReactNode } from 'react' import { useRouter } from 'next/navigation' interface AuthContextType { isAuthenticated: boolean userEmail: string | null userType: string | null login: (email: string, password: string, userType: string) => boolean logout: () => void checkAuth: () => void } const AuthContext = createContext(undefined) export function AuthProvider({ children }: { children: ReactNode }) { const [isAuthenticated, setIsAuthenticated] = useState(false) const [userEmail, setUserEmail] = useState(null) const [userType, setUserType] = useState(null) const [isLoading, setIsLoading] = useState(true) const router = useRouter() const checkAuth = () => { if (typeof window !== 'undefined') { const auth = localStorage.getItem('isAuthenticated') const email = localStorage.getItem('userEmail') const type = localStorage.getItem('userType') if (auth === 'true' && email) { setIsAuthenticated(true) setUserEmail(email) setUserType(type) } else { setIsAuthenticated(false) setUserEmail(null) setUserType(null) } } setIsLoading(false) } useEffect(() => { checkAuth() }, []) const login = (email: string, password: string, userType: string): boolean => { if (email === 'teste@gmail.com' && password === '123456') { localStorage.setItem('isAuthenticated', 'true') localStorage.setItem('userEmail', email) localStorage.setItem('userType', userType) setIsAuthenticated(true) setUserEmail(email) setUserType(userType) return true } return false } const logout = () => { // Usar o estado atual em vez do localStorage para evitar condição de corrida const currentUserType = userType || localStorage.getItem('userType') localStorage.removeItem('isAuthenticated') localStorage.removeItem('userEmail') localStorage.removeItem('userType') setIsAuthenticated(false) setUserEmail(null) setUserType(null) // Redirecionar para a página de login correta baseado no tipo de usuário if (currentUserType === 'administrador') { router.push('/login-admin') } else { router.push('/login') } } if (isLoading) { return (

Carregando...

) } return ( {children} ) } export const useAuth = () => { const context = useContext(AuthContext) if (context === undefined) { throw new Error('useAuth deve ser usado dentro de AuthProvider') } return context }