import { api } from '@/api/client' import type { Person } from '@/domain/types' let cachedUser: Person | null = null export async function currentUser(): Promise { if (cachedUser) return cachedUser try { const res = await api.POST('/api/v1/auth/refresh', { params: { cookie: { user_id: 0 } } }) if (!res.response.ok) return null cachedUser = res.data ?? null } catch (_) { cachedUser = null } return cachedUser } export async function login(username: string): Promise { const res = await api.POST('/api/v1/auth/login', { body: { username } }) if (!res.response.ok) { const err = res.error throw ( (err instanceof Error && err) || (typeof err === 'string' ? new Error(err) : new Error(`${res.response.status} ${res.response.statusText || 'HTTP error'}`)) ) } cachedUser = res.data ?? null if (!cachedUser) throw new Error('Login failed: empty response') return cachedUser } // New multitenant-ready API surface (backward compatible) export async function loginWithPassword(email: string, _password: string): Promise { // Backend currently expects { username }; map email to username until OpenAPI updates return login(email) } export async function createAccount(_email: string, _displayName: string, _password: string): Promise { // Placeholder until backend endpoints and OpenAPI are finalized throw new Error('createAccount not implemented yet') } export async function handleGoogleLogin(_token: string): Promise { // Placeholder until Google OAuth flow is wired throw new Error('handleGoogleLogin not implemented yet') } export async function logout(): Promise { // Clear local cache; server session is cookie-based and will be refreshed on next call cachedUser = null }