"use client"; import { createContext, useContext, useState, useEffect, useCallback, type ReactNode } from "react"; type User = { id: string; nickname: string; isAdmin: boolean } | null; type AuthContext = { user: User; login: (nickname: string, password: string) => Promise; register: (nickname: string, password: string) => Promise; logout: () => Promise; loading: boolean; refetch: () => Promise; }; const ctx = createContext({ user: null, login: async () => null, register: async () => null, logout: async () => {}, loading: true, refetch: async () => {}, }); export function AuthProvider({ children }: { children: ReactNode }) { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); const refetch = useCallback(async () => { try { const res = await fetch("/api/auth/me"); const data = await res.json(); setUser(data.user); } catch { setUser(null); } finally { setLoading(false); } }, []); useEffect(() => { refetch(); }, [refetch]); const login = async (nickname: string, password: string): Promise => { const res = await fetch("/api/auth/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ nickname, password }), }); const data = await res.json(); if (data.error) return data.error; setUser(data); return null; }; const register = async (nickname: string, password: string): Promise => { const res = await fetch("/api/auth/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ nickname, password }), }); const data = await res.json(); if (data.error) return data.error; const loginErr = await login(nickname, password); return loginErr; }; const logout = async () => { await fetch("/api/auth/login", { method: "DELETE" }); setUser(null); }; return ( {children} ); } export const useAuth = () => useContext(ctx);