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:
@@ -27,6 +27,33 @@ export function useSearchSession() {
|
|||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
const error = ref<string | null>(null)
|
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> {
|
function getAuthHeaders(): Record<string, string> {
|
||||||
const stored = localStorage.getItem('mb_admin_token')
|
const stored = localStorage.getItem('mb_admin_token')
|
||||||
if (stored) {
|
if (stored) {
|
||||||
@@ -44,6 +71,7 @@ export function useSearchSession() {
|
|||||||
loading.value = true
|
loading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
try {
|
try {
|
||||||
|
await ensureAuthToken()
|
||||||
const base = getApiBase()
|
const base = getApiBase()
|
||||||
const resp = await fetch(`${base}/api/v2/search-sessions`, {
|
const resp = await fetch(`${base}/api/v2/search-sessions`, {
|
||||||
headers: getAuthHeaders()
|
headers: getAuthHeaders()
|
||||||
@@ -62,6 +90,7 @@ export function useSearchSession() {
|
|||||||
loading.value = true
|
loading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
try {
|
try {
|
||||||
|
await ensureAuthToken()
|
||||||
const base = getApiBase()
|
const base = getApiBase()
|
||||||
const resp = await fetch(`${base}/api/v2/search-sessions`, {
|
const resp = await fetch(`${base}/api/v2/search-sessions`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -84,6 +113,7 @@ export function useSearchSession() {
|
|||||||
loading.value = true
|
loading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
try {
|
try {
|
||||||
|
await ensureAuthToken()
|
||||||
const base = getApiBase()
|
const base = getApiBase()
|
||||||
const resp = await fetch(`${base}/api/v2/search-sessions/${id}`, {
|
const resp = await fetch(`${base}/api/v2/search-sessions/${id}`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
@@ -106,6 +136,7 @@ export function useSearchSession() {
|
|||||||
loading.value = true
|
loading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
try {
|
try {
|
||||||
|
await ensureAuthToken()
|
||||||
const base = getApiBase()
|
const base = getApiBase()
|
||||||
const resp = await fetch(`${base}/api/v2/search-sessions/${id}`, {
|
const resp = await fetch(`${base}/api/v2/search-sessions/${id}`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
@@ -128,6 +159,7 @@ export function useSearchSession() {
|
|||||||
loading.value = true
|
loading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
try {
|
try {
|
||||||
|
await ensureAuthToken()
|
||||||
const base = getApiBase()
|
const base = getApiBase()
|
||||||
const resp = await fetch(`${base}/api/v2/search-sessions/${sessionId}/messages`, {
|
const resp = await fetch(`${base}/api/v2/search-sessions/${sessionId}/messages`, {
|
||||||
headers: getAuthHeaders()
|
headers: getAuthHeaders()
|
||||||
@@ -146,6 +178,7 @@ export function useSearchSession() {
|
|||||||
loading.value = true
|
loading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
try {
|
try {
|
||||||
|
await ensureAuthToken()
|
||||||
const base = getApiBase()
|
const base = getApiBase()
|
||||||
const resp = await fetch(`${base}/api/v2/search-sessions/${sessionId}/messages`, {
|
const resp = await fetch(`${base}/api/v2/search-sessions/${sessionId}/messages`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -174,6 +207,7 @@ export function useSearchSession() {
|
|||||||
loading.value = true
|
loading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
try {
|
try {
|
||||||
|
await ensureAuthToken()
|
||||||
const base = getApiBase()
|
const base = getApiBase()
|
||||||
const resp = await fetch(`${base}/api/v2/search-sessions/${sessionId}/messages/${msgId}`, {
|
const resp = await fetch(`${base}/api/v2/search-sessions/${sessionId}/messages/${msgId}`, {
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
|
|||||||
Reference in New Issue
Block a user