feat: add admin panel (#31)

* feat: add admin panel

* feature: update limit
This commit is contained in:
Dream Hunter
2023-10-09 23:03:15 +08:00
committed by GitHub
parent 0f74bde850
commit 5cfc78d70d
15 changed files with 343 additions and 10 deletions

View File

@@ -1,7 +1,7 @@
import { useGlobalState } from '../store'
const API_BASE = import.meta.env.VITE_API_BASE || "";
const { loading, auth, jwt, openSettings, showAuth } = useGlobalState();
const { loading, auth, jwt, openSettings, showAuth, adminAuth, showAdminAuth } = useGlobalState();
const apiFetch = async (path, options = {}) => {
loading.value = true;
@@ -10,6 +10,7 @@ const apiFetch = async (path, options = {}) => {
method: options.method || 'GET',
headers: {
'x-custom-auth': auth.value,
'x-admin-auth': adminAuth.value,
'Authorization': `Bearer ${jwt.value}`,
'Content-Type': 'application/json',
},
@@ -18,6 +19,10 @@ const apiFetch = async (path, options = {}) => {
showAuth.value = true;
throw new Error("Unauthorized");
}
if (response.status === 401 && path.startsWith("/admin")) {
showAdminAuth.value = true;
throw new Error("Unauthorized");
}
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}` || "error");
}
@@ -58,9 +63,39 @@ const getSettings = async () => {
return res["address"];
}
const adminShowPassword = async (id) => {
try {
const { password } = await apiFetch(`/admin/show_password/${id}`);
return password;
} catch (error) {
throw error;
}
}
const adminDeleteAddress = async (id) => {
try {
await apiFetch(`/admin/delete_address/${id}`, {
method: 'DELETE'
});
} catch (error) {
throw error;
}
}
const adminGetAddress = async () => {
try {
return await apiFetch("/admin/addresss");
} catch (error) {
throw error;
}
}
export const api = {
fetch: apiFetch,
getSettings: getSettings,
getOpenSettings: getOpenSettings,
adminShowPassword: adminShowPassword,
adminDeleteAddress: adminDeleteAddress,
adminGetAddress: adminGetAddress,
}