frontPanel/src/api/auth.ts

99 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-05-13 13:24:41 +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-05-13 13:24:41 +00:00
const API_URL = process.env.REACT_APP_DOMAIN + "/auth";
2023-11-08 12:51:40 +00:00
2024-05-13 13:24:41 +00:00
export const register = async (
2023-11-08 12:51:40 +00:00
login: string,
password: string,
2023-12-31 02:53:25 +00:00
phoneNumber: string,
2024-05-13 13:24:41 +00:00
): Promise<[RegisterResponse | null, string?]> => {
2023-11-08 12:51:40 +00:00
try {
const registerResponse = await makeRequest<
RegisterRequest,
RegisterResponse
>({
2024-05-13 13:24:41 +00:00
url: `${API_URL}/register`,
2023-11-08 12:51:40 +00:00
body: { login, password, phoneNumber },
useToken: false,
withCredentials: true,
});
return [registerResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось зарегестрировать аккаунт. ${error}`];
}
2024-05-13 13:24:41 +00:00
};
2023-11-08 12:51:40 +00:00
2024-05-13 13:24:41 +00:00
export const login = async (
2023-11-08 12:51:40 +00:00
login: string,
2023-12-31 02:53:25 +00:00
password: string,
2024-05-13 13:24:41 +00:00
): Promise<[LoginResponse | null, string?]> => {
2023-11-08 12:51:40 +00:00
try {
const loginResponse = await makeRequest<LoginRequest, LoginResponse>({
2024-05-13 13:24:41 +00:00
url: `${API_URL}/login`,
2023-11-08 12:51:40 +00:00
body: { login, password },
useToken: false,
withCredentials: true,
});
return [loginResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось войти. ${error}`];
}
2024-05-13 13:24:41 +00:00
};
2023-11-08 12:51:40 +00:00
2024-05-13 13:24:41 +00:00
export const logout = async (): Promise<[unknown, string?]> => {
2023-11-08 12:51:40 +00:00
try {
const logoutResponse = await makeRequest<never, void>({
method: "POST",
2024-05-13 13:24:41 +00:00
url: `${API_URL}/logout`,
2023-11-08 12:51:40 +00:00
useToken: true,
withCredentials: true,
});
return [logoutResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
2024-05-13 13:24:41 +00:00
return [null, error];
2023-11-08 12:51:40 +00:00
}
2024-05-13 13:24:41 +00:00
};
2024-05-13 13:24:41 +00:00
export const recover = async (
email: string,
2024-05-13 13:24:41 +00:00
): Promise<[unknown | null, string?]> => {
try {
const formData = new FormData();
formData.append("email", email);
formData.append(
"RedirectionURL",
process.env.REACT_APP_DOMAIN + "/changepwd",
);
2024-05-13 13:24:41 +00:00
const recoverResponse = await makeRequest<unknown, unknown>({
url: process.env.REACT_APP_DOMAIN + "/codeword/recover",
body: formData,
useToken: false,
withCredentials: true,
});
2024-05-13 13:24:41 +00:00
return [recoverResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось восстановить пароль. ${error}`];
}
2024-05-13 13:24:41 +00:00
};