2024-04-16 19:01:22 +00:00
|
|
|
import makeRequest from "@root/api/makeRequest";
|
2023-09-01 13:17:24 +00:00
|
|
|
|
|
|
|
|
import { parseAxiosError } from "@root/utils/parse-error";
|
|
|
|
|
|
|
|
|
|
import type {
|
|
|
|
|
LoginRequest,
|
|
|
|
|
RegisterRequest,
|
|
|
|
|
RegisterResponse,
|
|
|
|
|
} from "@frontend/kitui";
|
|
|
|
|
|
2024-01-23 18:31:02 +00:00
|
|
|
const baseUrl = process.env.REACT_APP_DOMAIN + "/auth"
|
2023-09-01 13:17:24 +00:00
|
|
|
|
|
|
|
|
export const signin = async (
|
|
|
|
|
login: string,
|
|
|
|
|
password: string
|
|
|
|
|
): Promise<[RegisterResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const signinResponse = await makeRequest<LoginRequest, RegisterResponse>({
|
|
|
|
|
url: baseUrl + "/login",
|
|
|
|
|
body: { login, password },
|
|
|
|
|
useToken: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [signinResponse];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
2024-04-16 19:01:22 +00:00
|
|
|
console.error(error)
|
2023-09-01 13:17:24 +00:00
|
|
|
return [null, `Ошибка авторизации. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const register = async (
|
|
|
|
|
login: string,
|
|
|
|
|
password: string,
|
|
|
|
|
phoneNumber: string = "--"
|
|
|
|
|
): Promise<[RegisterResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const registerResponse = await makeRequest<
|
|
|
|
|
RegisterRequest,
|
|
|
|
|
RegisterResponse
|
|
|
|
|
>({
|
|
|
|
|
url: baseUrl + "/register",
|
|
|
|
|
body: { login, password, phoneNumber },
|
|
|
|
|
useToken: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [registerResponse];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
|
|
|
|
return [null, `Ошибка регистрации. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const logout = async (): Promise<[unknown, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const logoutResponse = await makeRequest<never, unknown>({
|
|
|
|
|
url: baseUrl + "/logout",
|
|
|
|
|
method: "post",
|
|
|
|
|
contentType: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [logoutResponse];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
|
|
|
|
return [null, `Ошибка выхода из аккаунта. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
};
|