import { ref, watch } from 'vue' const hasLocalStorage = typeof localStorage !== 'undefined' && typeof localStorage.getItem === 'function' export const isParentAuthenticated = ref( hasLocalStorage ? localStorage.getItem('isParentAuthenticated') === 'true' : false, ) export const isUserLoggedIn = ref(false) export const isAuthReady = ref(false) export const currentUserId = ref('') watch(isParentAuthenticated, (val) => { if (hasLocalStorage && typeof localStorage.setItem === 'function') { localStorage.setItem('isParentAuthenticated', val ? 'true' : 'false') } }) export function authenticateParent() { isParentAuthenticated.value = true } export function logoutParent() { isParentAuthenticated.value = false if (hasLocalStorage && typeof localStorage.removeItem === 'function') { localStorage.removeItem('isParentAuthenticated') } } export function loginUser() { isUserLoggedIn.value = true } export function logoutUser() { isUserLoggedIn.value = false currentUserId.value = '' logoutParent() } export async function checkAuth() { try { const res = await fetch('/api/me', { method: 'GET' }) if (res.ok) { const data = await res.json() currentUserId.value = data.id isUserLoggedIn.value = true } else { isUserLoggedIn.value = false currentUserId.value = '' } } catch { isUserLoggedIn.value = false currentUserId.value = '' } isAuthReady.value = true }