reset endpoint + frontend reset and login pages add

This commit is contained in:
2026-09-26 19:26:57 +03:00
parent 489014667c
commit bcdf2b5154
32 changed files with 294 additions and 38 deletions
+14
View File
@@ -0,0 +1,14 @@
import { API_URL, setAccessToken } from "../token";
export async function login(email: string, password: string) {
const body = new URLSearchParams({ username: email, password });
const res = await fetch(`${API_URL}/protected/token`, {
method: "POST",
body,
credentials: "include",
});
if (!res.ok) throw new Error("Неверный email или пароль");
const data = await res.json();
setAccessToken(data.access_token);
return data;
}
+11
View File
@@ -0,0 +1,11 @@
export async function requestPasswordReset(email: string): Promise<void> {
const res = await fetch(`http://localhost:8000/protected/reset?${new URLSearchParams({ email })}`, {
method: "POST",
credentials: "include",
});
if (!res.ok) {
const body = await res.json().catch(() => null);
throw new Error(typeof body?.detail === "string" ? body.detail : "Не удалось отправить запрос");
}
}
+17
View File
@@ -0,0 +1,17 @@
export const API_URL = import.meta.env.VITE_BACKEND_URL;
let accessToken: string | null = null;
export function setAccessToken(token: string | null) {
accessToken = token;
}
export function getAccessToken() {
return accessToken;
}
export async function authFetch(path: string, init: RequestInit = {}) {
const headers = new Headers(init.headers);
if (accessToken) headers.set("Authorization", `Bearer ${accessToken}`);
return fetch(`${API_URL}${path}`, { ...init, headers, credentials: "include" });
}