Files
chore/frontend/vue-app/src/stores/auth.ts
Ryan Kegel e42c6c1ef2
All checks were successful
Gitea Actions Demo / build-and-push (push) Successful in 34s
feat: Implement logic to prevent deletion of system tasks and rewards; update APIs and tests accordingly
2026-02-01 16:57:12 -05:00

57 lines
1.4 KiB
TypeScript

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
}