feat(auth): implement session-based authentication and API token management
- Introduced a new authentication system using session cookies for the web dashboard and API tokens for sync clients. - Replaced the old SERVER_PASSWORD scheme with a more secure API token mechanism. - Added routes for user setup, login, logout, and credential updates. - Implemented middleware to require session and API token for protected routes. - Updated web UI to handle authentication states and provide user feedback. - Enhanced error handling in API requests and responses. - Updated README to reflect new authentication flow and database requirements.
This commit is contained in:
1 parent
240e1fda4a
commit
b150d835a1
18 files changed
+1022
-982
No files matched your search
@@ -1,9 +1,29 @@
|
||||
async function getJson(url) {
|
||||
const res = await fetch(url, { cache: 'no-store' });
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
async function request(url, options) {
|
||||
const res = await fetch(url, {
|
||||
...options,
|
||||
headers: options?.body ? { 'Content-Type': 'application/json', ...options?.headers } : options?.headers,
|
||||
});
|
||||
if (!res.ok) {
|
||||
let message = `HTTP ${res.status}`;
|
||||
try {
|
||||
const body = await res.json();
|
||||
// This backend's error responses are { error: "<category>", message: "<human text>" } -
|
||||
// message is the human-readable text; error is just a category label (e.g. "Unauthorized").
|
||||
if (body?.message) message = body.message;
|
||||
else if (body?.error) message = body.error;
|
||||
} catch {
|
||||
// Response body wasn't JSON (or was empty) - keep the generic status message.
|
||||
}
|
||||
throw new Error(message);
|
||||
}
|
||||
if (res.status === 204) return undefined;
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async function getJson(url) {
|
||||
return request(url);
|
||||
}
|
||||
|
||||
export async function fetchOverview() {
|
||||
return getJson('/web/data');
|
||||
}
|
||||
@@ -16,3 +36,26 @@ export async function fetchHealth() {
|
||||
const res = await fetch('/health');
|
||||
return { up: res.ok };
|
||||
}
|
||||
|
||||
export async function fetchAuthStatus() {
|
||||
return getJson('/api/auth/status');
|
||||
}
|
||||
|
||||
export async function setupAccount(username, password) {
|
||||
return request('/api/auth/setup', { method: 'POST', body: JSON.stringify({ username, password }) });
|
||||
}
|
||||
|
||||
export async function login(username, password) {
|
||||
return request('/api/auth/login', { method: 'POST', body: JSON.stringify({ username, password }) });
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
return request('/api/auth/logout', { method: 'POST' });
|
||||
}
|
||||
|
||||
export async function updateCredentials({ currentPassword, newUsername, newPassword }) {
|
||||
return request('/api/auth/credentials', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ currentPassword, newUsername, newPassword }),
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user