2025-10-18 01:42:23 +00:00
|
|
|
import { ref } from 'vue'
|
2025-11-01 06:47:42 +00:00
|
|
|
import { currentUser as apiCurrentUser, loginWithPassword as apiLoginWithPassword, logout as apiLogout, createAccount as apiCreateAccount } from '@/api/auth'
|
2025-11-01 02:55:55 +00:00
|
|
|
import { api } from '@/api/client'
|
2025-11-01 02:52:33 +00:00
|
|
|
import type { User } from '@/domain/types'
|
2025-10-18 01:42:23 +00:00
|
|
|
|
2025-11-01 02:44:58 +00:00
|
|
|
type Household = { id: number; name: string; slug: string }
|
|
|
|
|
|
2025-11-01 02:52:33 +00:00
|
|
|
const user = ref<User | null>(null)
|
2025-11-01 02:44:58 +00:00
|
|
|
const households = ref<Household[]>([])
|
|
|
|
|
const activeHousehold = ref<Household | null>(null)
|
2025-10-18 01:42:23 +00:00
|
|
|
let initialized = false
|
|
|
|
|
|
|
|
|
|
export async function loadUser() {
|
|
|
|
|
if (!initialized) {
|
|
|
|
|
user.value = await apiCurrentUser()
|
|
|
|
|
initialized = true
|
|
|
|
|
}
|
|
|
|
|
return user.value
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 06:47:42 +00:00
|
|
|
// Legacy username login removed; use loginWithPassword instead.
|
2025-10-18 01:42:23 +00:00
|
|
|
|
2025-11-01 02:44:58 +00:00
|
|
|
export async function loginWithPassword(email: string, password: string) {
|
|
|
|
|
user.value = await apiLoginWithPassword(email, password)
|
|
|
|
|
return user.value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function logout() {
|
|
|
|
|
await apiLogout()
|
|
|
|
|
user.value = null
|
|
|
|
|
households.value = []
|
|
|
|
|
activeHousehold.value = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setHouseholds(hs: Household[]) {
|
|
|
|
|
households.value = Array.isArray(hs) ? hs.slice() : []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setActiveHousehold(h: Household | string | null) {
|
|
|
|
|
if (h == null) {
|
|
|
|
|
activeHousehold.value = null
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (typeof h === 'string') {
|
|
|
|
|
activeHousehold.value = households.value.find((x) => x.slug === h) ?? null
|
|
|
|
|
} else {
|
|
|
|
|
activeHousehold.value = h
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-18 01:42:23 +00:00
|
|
|
export function useAuth() {
|
2025-11-01 02:55:55 +00:00
|
|
|
async function fetchHouseholds(): Promise<Household[]> {
|
|
|
|
|
const res = await api.GET('/api/v1/users/me/households', { params: {} })
|
|
|
|
|
const list = Array.isArray(res.data) ? res.data : []
|
|
|
|
|
const hs: Household[] = list.map((h) => ({ id: h.id, name: h.name, slug: h.slug }))
|
|
|
|
|
setHouseholds(hs)
|
|
|
|
|
return hs
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 03:05:15 +00:00
|
|
|
async function createHousehold(name: string): Promise<Household> {
|
|
|
|
|
const res = await api.POST('/api/v1/households', { body: { name } })
|
|
|
|
|
if (!res.response.ok || !res.data) throw new Error('Failed to create household')
|
|
|
|
|
const h: Household = { id: res.data.id, name: res.data.name, slug: res.data.slug }
|
|
|
|
|
// Update local state: append and set active
|
|
|
|
|
setHouseholds([...households.value, h])
|
|
|
|
|
setActiveHousehold(h)
|
|
|
|
|
return h
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 03:11:40 +00:00
|
|
|
async function createAccount(email: string, displayName: string, password: string) {
|
|
|
|
|
const u = await apiCreateAccount(email, displayName, password)
|
|
|
|
|
user.value = u
|
|
|
|
|
return u
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 06:47:42 +00:00
|
|
|
return { user, households, activeHousehold, loadUser, loginWithPassword, logout, setHouseholds, setActiveHousehold, fetchHouseholds, createHousehold, createAccount }
|
2025-10-18 01:42:23 +00:00
|
|
|
}
|