static login fix it

This commit is contained in:
sandhiya-hepl
2026-07-16 10:33:55 +05:30
parent 78749c860d
commit ed459d5943
5 changed files with 37 additions and 7 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>CITPL Admin</title>
<script type="module" crossorigin src="/citpl_website/assets/admin-Blm7TeBi.js"></script>
<script type="module" crossorigin src="/citpl_website/assets/admin-D28kGkNT.js"></script>
<link rel="modulepreload" crossorigin href="/citpl_website/assets/x-DnG5sti3.js">
<link rel="stylesheet" crossorigin href="/citpl_website/assets/admin-yhMCgkf-.css">
</head>
-1
View File
File diff suppressed because one or more lines are too long
+1
View File
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -168,8 +168,8 @@ function LoginPreview() {
}
function Login({ onLogin }) {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [username, setUsername] = useState('admin');
const [password, setPassword] = useState('admin123');
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);
+33 -3
View File
@@ -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'),