63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
"use client"
|
|
|
|
import { createContext, useContext, useState, useEffect, type ReactNode } from "react"
|
|
import { useRouter } from "next/navigation"
|
|
|
|
interface AuthContextType {
|
|
isAuthenticated: boolean
|
|
isLoading: boolean
|
|
login: (email: string, password: string) => Promise<boolean>
|
|
logout: () => void
|
|
}
|
|
|
|
const AuthContext = createContext<AuthContextType | null>(null)
|
|
|
|
// Mock credentials - replace with real API auth
|
|
const MOCK_CREDENTIALS = {
|
|
email: "admin@atticl.com",
|
|
password: "admin123",
|
|
}
|
|
|
|
export function AuthProvider({ children }: { children: ReactNode }) {
|
|
const [isAuthenticated, setIsAuthenticated] = useState(false)
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
const router = useRouter()
|
|
|
|
useEffect(() => {
|
|
// Check for existing session
|
|
const session = localStorage.getItem("atticl_admin_session")
|
|
if (session === "authenticated") {
|
|
setIsAuthenticated(true)
|
|
}
|
|
setIsLoading(false)
|
|
}, [])
|
|
|
|
const login = async (email: string, password: string): Promise<boolean> => {
|
|
// Mock API call - replace with real auth
|
|
await new Promise((resolve) => setTimeout(resolve, 500))
|
|
|
|
if (email === MOCK_CREDENTIALS.email && password === MOCK_CREDENTIALS.password) {
|
|
localStorage.setItem("atticl_admin_session", "authenticated")
|
|
setIsAuthenticated(true)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
const logout = () => {
|
|
localStorage.removeItem("atticl_admin_session")
|
|
setIsAuthenticated(false)
|
|
router.push("/admin/login")
|
|
}
|
|
|
|
return <AuthContext.Provider value={{ isAuthenticated, isLoading, login, logout }}>{children}</AuthContext.Provider>
|
|
}
|
|
|
|
export function useAuth() {
|
|
const context = useContext(AuthContext)
|
|
if (!context) {
|
|
throw new Error("useAuth must be used within an AuthProvider")
|
|
}
|
|
return context
|
|
}
|