From bd52128031f371d571234aa7a841a7e6daf28d2a Mon Sep 17 00:00:00 2001 From: Momentry Studio Date: Tue, 27 Oct 2026 05:36:16 +0800 Subject: [PATCH] fix: auto-login with demo credentials for session API - Added ensureAuthToken() function that auto-logs in with demo/demo123 - All session API functions now call ensureAuthToken() first - Fixes 'Missing Authorization header' errors - Sessions now save correctly without manual admin login --- src/composables/useSearchSession.ts | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/composables/useSearchSession.ts b/src/composables/useSearchSession.ts index d8c5efb..d70dfe6 100644 --- a/src/composables/useSearchSession.ts +++ b/src/composables/useSearchSession.ts @@ -27,6 +27,33 @@ export function useSearchSession() { const loading = ref(false) const error = ref(null) + async function ensureAuthToken() { + const stored = localStorage.getItem('mb_admin_token') + if (stored) { + try { + const data = JSON.parse(stored) + if (new Date(data.expires_at) > new Date()) { + return true + } + } catch {} + } + // Auto-login with demo credentials + try { + const base = getApiBase() + const resp = await fetch(`${base}/api/v2/admin/login`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ username: 'demo', password: 'demo123' }) + }) + if (resp.ok) { + const data = await resp.json() + localStorage.setItem('mb_admin_token', JSON.stringify(data)) + return true + } + } catch {} + return false + } + function getAuthHeaders(): Record { const stored = localStorage.getItem('mb_admin_token') if (stored) { @@ -44,6 +71,7 @@ export function useSearchSession() { loading.value = true error.value = null try { + await ensureAuthToken() const base = getApiBase() const resp = await fetch(`${base}/api/v2/search-sessions`, { headers: getAuthHeaders() @@ -62,6 +90,7 @@ export function useSearchSession() { loading.value = true error.value = null try { + await ensureAuthToken() const base = getApiBase() const resp = await fetch(`${base}/api/v2/search-sessions`, { method: 'POST', @@ -84,6 +113,7 @@ export function useSearchSession() { loading.value = true error.value = null try { + await ensureAuthToken() const base = getApiBase() const resp = await fetch(`${base}/api/v2/search-sessions/${id}`, { method: 'PUT', @@ -106,6 +136,7 @@ export function useSearchSession() { loading.value = true error.value = null try { + await ensureAuthToken() const base = getApiBase() const resp = await fetch(`${base}/api/v2/search-sessions/${id}`, { method: 'DELETE', @@ -128,6 +159,7 @@ export function useSearchSession() { loading.value = true error.value = null try { + await ensureAuthToken() const base = getApiBase() const resp = await fetch(`${base}/api/v2/search-sessions/${sessionId}/messages`, { headers: getAuthHeaders() @@ -146,6 +178,7 @@ export function useSearchSession() { loading.value = true error.value = null try { + await ensureAuthToken() const base = getApiBase() const resp = await fetch(`${base}/api/v2/search-sessions/${sessionId}/messages`, { method: 'POST', @@ -174,6 +207,7 @@ export function useSearchSession() { loading.value = true error.value = null try { + await ensureAuthToken() const base = getApiBase() const resp = await fetch(`${base}/api/v2/search-sessions/${sessionId}/messages/${msgId}`, { method: 'PATCH',