auth with api

This commit is contained in:
Nikolai Petukhov
2024-10-03 21:15:48 +03:00
parent a3484f4525
commit a9b683797b
15 changed files with 523 additions and 23 deletions

45
src/backend/api.js Normal file
View File

@@ -0,0 +1,45 @@
import {getConfigValue} from "@brojs/cli";
export const BASE_API_URL = "http://localhost:8099" + getConfigValue("enterfront.api");
// fetch(`${BASE_API_URL}/books/list`)
export async function post(path, body) {
const res = await fetch(`${BASE_API_URL}${path}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body)
});
if (res.status === 200) {
const data = await res.json();
console.log("Received post:", data);
return {ok: true, data: data};
} else {
const errorData = await res.json();
console.log("Error during post:", errorData.message);
return {ok: false, data: errorData};
}
}
export async function get(path){
const res = await fetch(`${BASE_API_URL}${path}`, {
method: "GET"
});
if (res.status === 200) {
const data = await res.json();
console.log("Received get:", data);
return {ok: true, data: data};
} else {
const errorData = await res.json();
console.log("Error during get:", errorData.message);
return {ok: false, data: errorData};
}
}

View File

@@ -0,0 +1,6 @@
export enum MessageType {
ERROR,
SUCCESS,
INFO,
WARN
}

View File

@@ -0,0 +1,28 @@
import {toast} from "react-toastify";
import {MessageType} from "./message.tsx";
export const displayMessage = (message, type) => {
switch (type) {
default:
case MessageType.ERROR:
toast.error(message, {
position: 'bottom-right',
});
break;
case MessageType.INFO:
toast.info(message, {
position: 'bottom-right',
});
break;
case MessageType.SUCCESS:
toast.success(message, {
position: 'bottom-right',
});
break;
case MessageType.WARN:
toast.warn(message, {
position: 'bottom-right',
});
break;
}
}