deploy change, update token system, fix abuses and add admin verify
This commit is contained in:
+63
-20
@@ -1,25 +1,34 @@
|
||||
// Базовый путь теперь смотрит на /api/v1
|
||||
export const API_BASE = import.meta.env.VITE_API_BASE || "/api/v1";
|
||||
export const TOKEN_KEY = "nrxp_token";
|
||||
|
||||
export const fetchProfile = async () => {
|
||||
const token = localStorage.getItem(TOKEN_KEY);
|
||||
if (!token) throw new Error("No token found");
|
||||
export const removeTokenCookie = () => {
|
||||
document.cookie = `${TOKEN_KEY}=;expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/;`;
|
||||
};
|
||||
|
||||
// Путь: /api/v1/users/me
|
||||
const res = await fetch(`${API_BASE}/users/me`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
export const getTokenCookie = (): string | null => {
|
||||
const match = document.cookie.match(
|
||||
new RegExp("(^| )" + TOKEN_KEY + "=([^;]+)"),
|
||||
);
|
||||
return match ? match[2] : null;
|
||||
};
|
||||
|
||||
// --- API Запросы ---
|
||||
export const fetchProfile = async () => {
|
||||
const res = await fetch(`${API_BASE}/users/me`);
|
||||
|
||||
if (res.status === 401) {
|
||||
window.location.href = "/"; // Сервер сам "выкинул" куку или она протухла
|
||||
return;
|
||||
}
|
||||
|
||||
if (!res.ok) throw new Error("Failed to fetch profile");
|
||||
return res.json();
|
||||
};
|
||||
|
||||
export const submitHackResult = async (score: number) => {
|
||||
const token = localStorage.getItem(TOKEN_KEY);
|
||||
const token = getTokenCookie();
|
||||
if (!token) throw new Error("No token found");
|
||||
|
||||
// Путь: /api/v1/users/hack/result
|
||||
const res = await fetch(`${API_BASE}/users/hack/result`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -29,17 +38,31 @@ export const submitHackResult = async (score: number) => {
|
||||
body: JSON.stringify({ score }),
|
||||
});
|
||||
|
||||
if (res.status === 401) {
|
||||
removeTokenCookie();
|
||||
window.location.href = "/";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!res.ok) throw new Error("Failed to sync breach data");
|
||||
return res.json();
|
||||
};
|
||||
|
||||
export const activateTrial = async () => {
|
||||
const token = localStorage.getItem(TOKEN_KEY);
|
||||
// Путь: /api/v1/billing/trial
|
||||
const token = getTokenCookie();
|
||||
if (!token) throw new Error("No token found");
|
||||
|
||||
const res = await fetch(`${API_BASE}/billing/trial`, {
|
||||
method: "POST",
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
|
||||
if (res.status === 401) {
|
||||
removeTokenCookie();
|
||||
window.location.href = "/";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const errData = await res.json().catch(() => ({}));
|
||||
throw new Error(errData.error || res.statusText || "Activation failed");
|
||||
@@ -47,9 +70,12 @@ export const activateTrial = async () => {
|
||||
return res.json();
|
||||
};
|
||||
|
||||
|
||||
export const initiatePayment = async (plan: string, provider: string, currency: string) => {
|
||||
const token = localStorage.getItem(TOKEN_KEY);
|
||||
export const initiatePayment = async (
|
||||
plan: string,
|
||||
provider: string,
|
||||
currency: string,
|
||||
) => {
|
||||
const token = getTokenCookie();
|
||||
if (!token) throw new Error("No token found");
|
||||
|
||||
const res = await fetch(`${API_BASE}/billing/pay/initiate`, {
|
||||
@@ -60,7 +86,13 @@ export const initiatePayment = async (plan: string, provider: string, currency:
|
||||
},
|
||||
body: JSON.stringify({ plan, provider, currency }),
|
||||
});
|
||||
|
||||
|
||||
if (res.status === 401) {
|
||||
removeTokenCookie();
|
||||
window.location.href = "/";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const errData = await res.json().catch(() => ({}));
|
||||
throw new Error(errData.error || "Initiate payment failed");
|
||||
@@ -68,8 +100,13 @@ export const initiatePayment = async (plan: string, provider: string, currency:
|
||||
return res.json();
|
||||
};
|
||||
|
||||
export const confirmPayment = async (invoice_id: string, external_tx_id: string) => {
|
||||
const token = localStorage.getItem(TOKEN_KEY);
|
||||
export const confirmPayment = async (
|
||||
invoice_id: string,
|
||||
external_tx_id: string,
|
||||
) => {
|
||||
const token = getTokenCookie();
|
||||
if (!token) throw new Error("No token found");
|
||||
|
||||
const res = await fetch(`${API_BASE}/billing/pay/confirm`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -78,10 +115,16 @@ export const confirmPayment = async (invoice_id: string, external_tx_id: string)
|
||||
},
|
||||
body: JSON.stringify({ invoice_id, external_tx_id }),
|
||||
});
|
||||
|
||||
|
||||
if (res.status === 401) {
|
||||
removeTokenCookie();
|
||||
window.location.href = "/";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const errData = await res.json().catch(() => ({}));
|
||||
throw new Error(errData.error || "Payment verification failed");
|
||||
}
|
||||
return res.json();
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user