feat(auth): enhance authentication flow with TOTP and admin reset features
- Added TOTP setup, enable, and disable functions to the API. - Implemented admin reset request and confirmation functionalities. - Updated AuthPage to handle new reset functionalities and TOTP inputs. - Enhanced auth store to manage user creation, deletion, and listing. - Improved AppShell and SettingsDialog to support user management features. - Refactored API calls for better organization and clarity.
This commit is contained in:
1 parent
ac08be0248
commit
652e81dacb
9 files changed
+4333
-3737
No files matched your search
+92
-47
@@ -1,61 +1,106 @@
|
||||
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');
|
||||
}
|
||||
|
||||
export async function fetchTechStack() {
|
||||
return getJson('/web/info');
|
||||
}
|
||||
|
||||
export async function fetchHealth() {
|
||||
const res = await fetch('/health');
|
||||
return { up: res.ok };
|
||||
}
|
||||
|
||||
export async function fetchAuthStatus() {
|
||||
return getJson('/api/auth/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');
|
||||
}
|
||||
|
||||
export async function fetchTechStack() {
|
||||
return getJson('/web/info');
|
||||
}
|
||||
|
||||
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, email, password) {
|
||||
return request('/api/auth/setup', { method: 'POST', body: JSON.stringify({ username, email, password }) });
|
||||
}
|
||||
|
||||
export async function login(identifier, password) {
|
||||
return request('/api/auth/login', { method: 'POST', body: JSON.stringify({ identifier, password }) });
|
||||
export async function login(identifier, password, totpToken = '') {
|
||||
return request('/api/auth/login', { method: 'POST', body: JSON.stringify({ identifier, password, totpToken }) });
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
return request('/api/auth/logout', { method: 'POST' });
|
||||
export async function fetchTotpStatus() {
|
||||
return request('/api/auth/totp/status');
|
||||
}
|
||||
|
||||
export async function setupTotp() {
|
||||
return request('/api/auth/totp/setup', { method: 'POST' });
|
||||
}
|
||||
|
||||
export async function enableTotp(token) {
|
||||
return request('/api/auth/totp/enable', { method: 'POST', body: JSON.stringify({ token }) });
|
||||
}
|
||||
|
||||
export async function disableTotp(token) {
|
||||
return request('/api/auth/totp/disable', { method: 'POST', body: JSON.stringify({ token }) });
|
||||
}
|
||||
|
||||
export async function requestAdminReset() {
|
||||
return request('/api/auth/reset/request', { method: 'POST' });
|
||||
}
|
||||
|
||||
export async function confirmAdminReset(code) {
|
||||
const result = await request('/api/auth/reset/confirm', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ code }),
|
||||
});
|
||||
window.location.reload();
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
return request('/api/auth/logout', { method: 'POST' });
|
||||
}
|
||||
|
||||
export async function updateCredentials({ newUsername, newEmail, newPassword }) {
|
||||
return request('/api/auth/credentials', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ newUsername, newEmail, newPassword }),
|
||||
});
|
||||
}
|
||||
|
||||
export async function listUsers() {
|
||||
return getJson('/api/auth/users');
|
||||
}
|
||||
|
||||
export async function createUser(input) {
|
||||
return request('/api/auth/users', { method: 'POST', body: JSON.stringify(input) });
|
||||
}
|
||||
|
||||
export async function updateUser(id, input) {
|
||||
return request(`/api/auth/users/${encodeURIComponent(id)}`, { method: 'PUT', body: JSON.stringify(input) });
|
||||
}
|
||||
|
||||
export async function deleteUser(id) {
|
||||
return request(`/api/auth/users/${encodeURIComponent(id)}`, { method: 'DELETE' });
|
||||
}
|
||||
Reference in new issue
Block a user