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
This commit is contained in:
2026-10-27 05:36:16 +08:00
parent fe91afff98
commit bd52128031

View File

@@ -27,6 +27,33 @@ export function useSearchSession() {
const loading = ref(false)
const error = ref<string | null>(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<string, string> {
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',