2024-04-16 20:20:46 +00:00
|
|
|
|
import makeRequest from "@api/makeRequest";
|
2023-11-08 12:51:40 +00:00
|
|
|
|
|
|
|
|
|
import type {
|
|
|
|
|
LoginRequest,
|
|
|
|
|
LoginResponse,
|
|
|
|
|
RegisterRequest,
|
|
|
|
|
RegisterResponse,
|
|
|
|
|
} from "@frontend/kitui";
|
|
|
|
|
import { parseAxiosError } from "../utils/parse-error";
|
|
|
|
|
|
2024-01-19 14:24:35 +00:00
|
|
|
|
const apiUrl = process.env.REACT_APP_DOMAIN + "/auth";
|
2023-11-08 12:51:40 +00:00
|
|
|
|
|
|
|
|
|
export async function register(
|
|
|
|
|
login: string,
|
|
|
|
|
password: string,
|
2023-12-31 02:53:25 +00:00
|
|
|
|
phoneNumber: string,
|
2023-11-08 12:51:40 +00:00
|
|
|
|
): 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,
|
2023-12-31 02:53:25 +00:00
|
|
|
|
password: string,
|
2023-11-08 12:51:40 +00:00
|
|
|
|
): 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}`];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function logout(): Promise<[unknown, string?]> {
|
|
|
|
|
try {
|
|
|
|
|
const logoutResponse = await makeRequest<never, void>({
|
|
|
|
|
url: apiUrl + "/logout",
|
|
|
|
|
method: "POST",
|
|
|
|
|
useToken: true,
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [logoutResponse];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
2024-04-26 14:41:36 +00:00
|
|
|
|
return [null];
|
2023-11-08 12:51:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-21 01:13:11 +00:00
|
|
|
|
|
|
|
|
|
export async function recover(
|
|
|
|
|
email: string,
|
|
|
|
|
): Promise<[unknown | null, string?]> {
|
|
|
|
|
try {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append("email", email);
|
|
|
|
|
formData.append(
|
|
|
|
|
"RedirectionURL",
|
|
|
|
|
process.env.REACT_APP_DOMAIN + "/changepwd",
|
|
|
|
|
);
|
|
|
|
|
const recoverResponse = await makeRequest<unknown, unknown>({
|
|
|
|
|
url: process.env.REACT_APP_DOMAIN + "/codeword/recover",
|
|
|
|
|
body: formData,
|
|
|
|
|
useToken: false,
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
});
|
|
|
|
|
return [recoverResponse];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
|
|
|
|
return [null, `Не удалось восстановить пароль. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
}
|