feat(auth): add hooks for current user and logout functionality

This commit is contained in:
ngc2207 2025-01-31 20:27:17 +08:00
parent 5bd06bc58e
commit a8abee3757
2 changed files with 42 additions and 0 deletions

View File

@ -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;
};

View File

@ -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<ResponseType, Error>({
mutationFn: async () => {
const response = await client.api.auth.logout["$post"]();
return await response.json();
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["current"] });
},
});
return mutation;
};