'use client' import { createContext, useContext, useEffect, useState, ReactNode } from 'react' import { useRouter } from 'next/navigation' interface AuthContextType { isAuthenticated: boolean userEmail: string | null login: (email: string, password: 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 [isLoading, setIsLoading] = useState(true) const router = useRouter() const checkAuth = () => { if (typeof window !== 'undefined') { const auth = localStorage.getItem('isAuthenticated') const email = localStorage.getItem('userEmail') if (auth === 'true' && email) { setIsAuthenticated(true) setUserEmail(email) } else { setIsAuthenticated(false) setUserEmail(null) } } setIsLoading(false) } useEffect(() => { checkAuth() }, []) const login = (email: string, password: string): boolean => { if (email === 'teste@gmail.com' && password === '123456') { localStorage.setItem('isAuthenticated', 'true') localStorage.setItem('userEmail', email) setIsAuthenticated(true) setUserEmail(email) return true } return false } const logout = () => { localStorage.removeItem('isAuthenticated') localStorage.removeItem('userEmail') setIsAuthenticated(false) setUserEmail(null) 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 }