'use client'; import React, { createContext, useContext, useState, useEffect } from 'react'; import { pb, getCurrentUser, isAuthenticated } from '@/lib/pocketbase'; interface AuthContextType { isLoggedIn: boolean; user: ReturnType; isLoading: boolean; refresh: () => void; } const AuthContext = createContext({ isLoggedIn: false, user: null, isLoading: true, refresh: () => {}, }); export function AuthProvider({ children }: { children: React.ReactNode }) { const [isLoggedIn, setIsLoggedIn] = useState(false); const [user, setUser] = useState>(null); const [isLoading, setIsLoading] = useState(true); const refresh = () => { const valid = isAuthenticated(); setIsLoggedIn(valid); setUser(getCurrentUser()); }; useEffect(() => { refresh(); setIsLoading(false); // Listen for auth store changes const unsubscribe = pb.authStore.onChange(() => { refresh(); }); return () => { unsubscribe(); }; }, []); return ( {children} ); } export const useAuth = () => useContext(AuthContext);