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