front-hub/src/api/auth.ts

74 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { makeRequest } from "@frontend/kitui"
import { parseAxiosError } from "@root/utils/parse-error"
import type {
LoginRequest,
LoginResponse,
RegisterRequest,
RegisterResponse,
} from "@frontend/kitui"
const apiUrl = "https://" + process.env.REACT_APP_DOMAIN + "/auth"
console.log("переменная", apiUrl)
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}`]
}
}
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)
return [null, `Не удалось выйти. ${error}`]
}
}