37 lines
765 B
TypeScript
37 lines
765 B
TypeScript
import { useMutation } from "@tanstack/react-query"
|
|
import { fetchApi } from "./api"
|
|
|
|
function useLogin() {
|
|
return useMutation({
|
|
mutationFn: async (creds: { username: string; password: string }) => {
|
|
return await fetch(`${import.meta.env.VITE_API_URL}/api/login`, {
|
|
method: "POST",
|
|
body: JSON.stringify(creds),
|
|
credentials: "include",
|
|
})
|
|
},
|
|
})
|
|
}
|
|
|
|
function useLogOut() {
|
|
return useMutation({
|
|
mutationFn: () =>
|
|
fetchApi("/logout", {
|
|
method: "POST",
|
|
}),
|
|
})
|
|
}
|
|
|
|
function useSignUp() {
|
|
return useMutation({
|
|
mutationFn: (creds: { username: string; password: string }) =>
|
|
fetchApi("/sign-up", {
|
|
method: "POST",
|
|
credentials: "omit",
|
|
body: JSON.stringify(creds),
|
|
}),
|
|
})
|
|
}
|
|
|
|
export { useLogin, useLogOut, useSignUp }
|