2023-06-11 10:18:37 +00:00
|
|
|
|
import { makeRequest } from "@frontend/kitui";
|
2023-05-17 11:20:11 +00:00
|
|
|
|
|
2023-08-30 09:56:14 +00:00
|
|
|
|
import { parseAxiosError } from "@root/utils/parse-error";
|
2023-05-17 11:20:11 +00:00
|
|
|
|
|
2023-08-31 10:02:11 +00:00
|
|
|
|
import type {
|
|
|
|
|
LoginRequest,
|
|
|
|
|
LoginResponse,
|
|
|
|
|
RegisterRequest,
|
|
|
|
|
RegisterResponse,
|
|
|
|
|
} from "@frontend/kitui";
|
|
|
|
|
|
2023-08-30 09:56:14 +00:00
|
|
|
|
const apiUrl =
|
2023-08-31 10:02:11 +00:00
|
|
|
|
process.env.NODE_ENV === "production"
|
|
|
|
|
? "/auth"
|
|
|
|
|
: "https://hub.pena.digital/auth";
|
|
|
|
|
|
|
|
|
|
export async function register(
|
|
|
|
|
login: string,
|
|
|
|
|
password: string,
|
|
|
|
|
phoneNumber: string
|
|
|
|
|
): Promise<[RegisterResponse | null, string?]> {
|
|
|
|
|
try {
|
|
|
|
|
const registerResponse = await makeRequest<
|
|
|
|
|
RegisterRequest,
|
|
|
|
|
RegisterResponse
|
|
|
|
|
>({
|
|
|
|
|
url: apiUrl + "/register",
|
|
|
|
|
body: { login, password, phoneNumber },
|
|
|
|
|
useToken: false,
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [registerResponse];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
|
|
|
|
return [null, `Не удалось зарегестрировать аккаунт. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function login(
|
|
|
|
|
login: string,
|
|
|
|
|
password: string
|
|
|
|
|
): Promise<[LoginResponse | null, string?]> {
|
|
|
|
|
try {
|
|
|
|
|
const loginResponse = await makeRequest<LoginRequest, LoginResponse>({
|
|
|
|
|
url: apiUrl + "/login",
|
|
|
|
|
body: { login, password },
|
|
|
|
|
useToken: false,
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [loginResponse];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
|
|
|
|
return [null, `Не удалось войти. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-17 11:20:11 +00:00
|
|
|
|
|
2023-08-30 09:56:14 +00:00
|
|
|
|
export async function logout(): Promise<[unknown, string?]> {
|
|
|
|
|
try {
|
|
|
|
|
const logoutResponse = await makeRequest<never, void>({
|
2023-08-31 10:02:11 +00:00
|
|
|
|
url: apiUrl + "/logout",
|
2023-08-30 09:56:14 +00:00
|
|
|
|
method: "POST",
|
|
|
|
|
useToken: true,
|
|
|
|
|
withCredentials: true,
|
2023-05-17 11:20:11 +00:00
|
|
|
|
});
|
2023-08-30 09:56:14 +00:00
|
|
|
|
|
|
|
|
|
return [logoutResponse];
|
|
|
|
|
} catch (nativeError) {
|
2023-08-30 11:31:49 +00:00
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
2023-08-30 09:56:14 +00:00
|
|
|
|
|
|
|
|
|
return [null, `Не удалось выйти. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
}
|