Full authentication in front

This commit is contained in:
2025-04-07 02:33:27 +02:00
parent cd248c4aa9
commit 738a9bebf0
52 changed files with 7399 additions and 2199 deletions

View File

@@ -0,0 +1,107 @@
import type { DataProvider } from "@refinedev/core";
const API_URL = "http://localhost:8000";
const fetcher = async (url: string, options?: RequestInit) => {
return fetch(url, {
...options,
headers: {
...options?.headers,
Authorization: "Bearer " + localStorage.getItem("access_token"),
},
});
};
export const dataProvider: DataProvider = {
getOne: async ({ resource, id, meta }) => {
const response = id !== "" ? await fetcher(`${API_URL}/${resource}/${id}`) : await fetcher(`${API_URL}/${resource}`);
if (response.status < 200 || response.status > 299) throw response;
const data = await response.json();
return {
data
};
},
update: async ({ resource, id, variables }) => {
const response = await fetcher(`${API_URL}/${resource}/${id}`, {
method: "PUT",
body: JSON.stringify(variables),
headers: {
"Content-Type": "application/json",
},
});
if (response.status < 200 || response.status > 299) throw response;
const data = await response.json();
return { data };
},
getList: async ({ resource, pagination, filters, sorters, meta }) => {
const params = new URLSearchParams();
if (pagination) {
params.append("page", String(pagination.current));
params.append("size", String(pagination.pageSize));
}
if (sorters && sorters.length > 0) {
params.append("order_by", sorters.map((sorter) => (sorter.order == "asc" ? "+" : "-") + sorter.field).join(","));
}
if (filters && filters.length > 0) {
filters.forEach((filter) => {
if ("field" in filter && filter.value && filter.operator === "contains") {
params.append(filter.field + "__like", "%" + filter.value + "%");
}
});
}
const response = await fetcher(`${API_URL}/${resource}?${params.toString()}`);
if (response.status < 200 || response.status > 299) throw response;
const data = await response.json();
return {
data: data.items,
total: data.total, // We'll cover this in the next steps.
};
},
create: async ({ resource, variables }) => {
const response = await fetcher(`${API_URL}/${resource}`, {
method: "POST",
body: JSON.stringify(variables),
headers: {
"Content-Type": "application/json",
},
});
if (response.status < 200 || response.status > 299) throw response;
const data = await response.json();
return { data };
},
deleteOne: async ({ resource, id, variables, meta }) => {
const response = await fetcher(`${API_URL}/${resource}/${id}`,{
method: "DELETE",
});
if (response.status < 200 || response.status > 299) throw response;
const data = await response.json();
return {
data
};
},
getApiUrl: () => API_URL,
// Optional methods:
// getMany: () => { /* ... */ },
// createMany: () => { /* ... */ },
// deleteMany: () => { /* ... */ },
// updateMany: () => { /* ... */ },
// custom: () => { /* ... */ },
};