diff --git a/src/features/auth/api/use-current.ts b/src/features/auth/api/use-current.ts new file mode 100644 index 0000000..fd9dd03 --- /dev/null +++ b/src/features/auth/api/use-current.ts @@ -0,0 +1,21 @@ +import { client } from "@/lib/rpc"; +import { useQuery } from "@tanstack/react-query"; + +export const useCurrent = () => { + const query = useQuery({ + queryKey: ["current"], + queryFn: async () => { + const response = await client.api.auth.current.$get(); + + if (!response.ok) { + return null; + } + + const { data } = await response.json(); + + return data; + }, + }); + + return query; +}; diff --git a/src/features/auth/api/use-logout.ts b/src/features/auth/api/use-logout.ts new file mode 100644 index 0000000..05bf8f9 --- /dev/null +++ b/src/features/auth/api/use-logout.ts @@ -0,0 +1,21 @@ +import { client } from "@/lib/rpc"; +import { InferResponseType } from "hono"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; + +type ResponseType = InferResponseType<(typeof client.api.auth.logout)["$post"]>; + +export const useLogout = () => { + const queryClient = useQueryClient(); + + const mutation = useMutation({ + mutationFn: async () => { + const response = await client.api.auth.logout["$post"](); + return await response.json(); + }, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ["current"] }); + }, + }); + + return mutation; +};