Files
markone/packages/web/src/auth.ts

37 lines
765 B
TypeScript
Raw Normal View History

2025-05-06 11:00:35 +01:00
import { useMutation } from "@tanstack/react-query"
2025-05-07 23:40:03 +01:00
import { fetchApi } from "./api"
2025-05-06 11:00:35 +01:00
function useLogin() {
return useMutation({
mutationFn: async (creds: { username: string; password: string }) => {
2025-05-07 15:47:08 +01:00
return await fetch(`${import.meta.env.VITE_API_URL}/api/login`, {
2025-05-06 11:00:35 +01:00
method: "POST",
body: JSON.stringify(creds),
2025-05-07 15:47:08 +01:00
credentials: "include",
2025-05-06 11:00:35 +01:00
})
},
})
}
2025-05-07 23:40:03 +01:00
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 }