51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
![]() |
import { CreateUserRequest, User } from "@root/model/auth";
|
||
|
import { authStore } from "@root/stores/makeRequest";
|
||
|
|
||
|
|
||
|
const apiUrl = process.env.NODE_ENV === "production" ? "/user" : "https://hub.pena.digital/user";
|
||
|
const authUrl = process.env.NODE_ENV === "production" ? "/auth" : "https://hub.pena.digital/auth";
|
||
|
|
||
|
const makeRequest = authStore.getState().makeRequest;
|
||
|
|
||
|
export async function getOrCreateUser({ userId, email, password }: {
|
||
|
userId: string;
|
||
|
email: string | null;
|
||
|
password: string | null;
|
||
|
}): Promise<User | null> {
|
||
|
try {
|
||
|
return await makeRequest<never, User>({
|
||
|
url: `${apiUrl}/${userId}`,
|
||
|
contentType: true,
|
||
|
method: "GET",
|
||
|
useToken: false,
|
||
|
withCredentials: false,
|
||
|
});
|
||
|
} catch (error) {
|
||
|
console.log("get user error", error);
|
||
|
|
||
|
if (!email || !password) return null;
|
||
|
|
||
|
return makeRequest<CreateUserRequest, User>({
|
||
|
url: apiUrl,
|
||
|
contentType: true,
|
||
|
method: "POST",
|
||
|
useToken: true,
|
||
|
withCredentials: false,
|
||
|
body: {
|
||
|
email,
|
||
|
login: email,
|
||
|
password,
|
||
|
phoneNumber: "-",
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function logout() {
|
||
|
return makeRequest<never, void>({
|
||
|
url: authUrl + "/logout",
|
||
|
method: "POST",
|
||
|
useToken: false,
|
||
|
withCredentials: true,
|
||
|
});
|
||
|
}
|