feat: auth store, API client, router setup
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { useAuthStore } from "../store/auth";
|
||||
|
||||
const BASE = "/api";
|
||||
|
||||
async function request(
|
||||
url: string,
|
||||
options: RequestInit = {},
|
||||
withAuth = true
|
||||
): Promise<any> {
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
...(options.headers as Record<string, string>),
|
||||
};
|
||||
|
||||
if (withAuth) {
|
||||
const token = useAuthStore.getState().accessToken;
|
||||
if (token) headers["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const res = await fetch(`${BASE}${url}`, { ...options, headers });
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export const api = {
|
||||
register: (email: string, password: string, name?: string) =>
|
||||
request("/web/register", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ email, password, name }),
|
||||
}, false),
|
||||
|
||||
login: (email: string, password: string) =>
|
||||
request("/web/login", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ email, password }),
|
||||
}, false),
|
||||
|
||||
tgAuth: (data: Record<string, any>) =>
|
||||
request("/web/tg_auth", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(data),
|
||||
}, false),
|
||||
|
||||
refresh: (refresh_token: string) =>
|
||||
request("/web/refresh", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ refresh_token }),
|
||||
}, false),
|
||||
|
||||
action: (action: string, payload: Record<string, any> = {}) =>
|
||||
request("/web/action", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ action, payload }),
|
||||
}),
|
||||
};
|
||||
Reference in New Issue
Block a user