static login fix it
This commit is contained in:
+33
-3
@@ -1,5 +1,10 @@
|
||||
import { API_BASE } from '../lib/apiBase';
|
||||
|
||||
/** Frontend-only fallback when the Node API is not reachable on the host. */
|
||||
const STATIC_USER = 'admin';
|
||||
const STATIC_PASS = 'admin123';
|
||||
export const STATIC_TOKEN = 'static-admin-session';
|
||||
|
||||
export function getToken() {
|
||||
return localStorage.getItem('admin_token');
|
||||
}
|
||||
@@ -12,6 +17,17 @@ export function clearToken() {
|
||||
localStorage.removeItem('admin_token');
|
||||
}
|
||||
|
||||
export function isStaticSession(token = getToken()) {
|
||||
return token === STATIC_TOKEN;
|
||||
}
|
||||
|
||||
function staticLogin(username, password) {
|
||||
if (username === STATIC_USER && password === STATIC_PASS) {
|
||||
return { token: STATIC_TOKEN, expiresAt: null, mode: 'static' };
|
||||
}
|
||||
throw new Error('Invalid credentials');
|
||||
}
|
||||
|
||||
async function apiFetch(path, options = {}) {
|
||||
const headers = { ...options.headers };
|
||||
if (!(options.body instanceof FormData)) {
|
||||
@@ -35,10 +51,24 @@ async function apiFetch(path, options = {}) {
|
||||
}
|
||||
|
||||
export const adminApi = {
|
||||
login: (username, password) =>
|
||||
apiFetch('/api/auth/login', { method: 'POST', body: JSON.stringify({ username, password }) }),
|
||||
login: async (username, password) => {
|
||||
// Prefer Node API when it is up; otherwise allow static admin/admin123.
|
||||
try {
|
||||
return await apiFetch('/api/auth/login', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
} catch {
|
||||
return staticLogin(username, password);
|
||||
}
|
||||
},
|
||||
|
||||
me: () => apiFetch('/api/auth/me'),
|
||||
me: async () => {
|
||||
if (isStaticSession()) {
|
||||
return { username: STATIC_USER, mode: 'static' };
|
||||
}
|
||||
return apiFetch('/api/auth/me');
|
||||
},
|
||||
|
||||
getContent: () => apiFetch('/api/content'),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user