feat: store logged account

This commit is contained in:
2025-05-24 12:05:57 +02:00
parent 06e0e90677
commit eb42b61b2c
8 changed files with 177 additions and 36 deletions

20
web/src/api/index.ts Normal file
View File

@ -0,0 +1,20 @@
export const handleApiError = async (response: Response) => {
try {
const json = await response.json();
console.log({ json });
const text = json.error ?? "unexpected error happpened";
return new Error(text[0].toUpperCase() + text.slice(1));
} catch (err) {
try {
console.log(err);
const text = await response.text();
if (text.length > 0) {
return new Error(text[0].toUpperCase() + text.slice(1));
}
} catch (err) {
console.log(err);
}
}
return new Error("Unexpected error happened");
};

34
web/src/api/login.ts Normal file
View File

@ -0,0 +1,34 @@
import { handleApiError } from ".";
export interface LoginRequest {
email: string;
password: string;
}
export interface LoginResponse {
id: string;
email: string;
full_name: string;
access: string;
refresh: string;
}
export const loginApi = async (req: LoginRequest) => {
const response = await fetch("/api/v1/login", {
method: "POST",
body: JSON.stringify({
email: req.email,
password: req.password,
}),
headers: {
"Content-Type": "application/json",
},
});
if (response.status !== 200 && response.status !== 201)
throw await handleApiError(response);
const data: LoginResponse = await response.json();
return data;
};