mirror of
https://gitlab.massbug.com/massbug/judge4c.git
synced 2025-07-04 21:00:54 +00:00
feat(auth): add hooks for current user and logout functionality
This commit is contained in:
parent
5bd06bc58e
commit
a8abee3757
21
src/features/auth/api/use-current.ts
Normal file
21
src/features/auth/api/use-current.ts
Normal 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;
|
||||||
|
};
|
21
src/features/auth/api/use-logout.ts
Normal file
21
src/features/auth/api/use-logout.ts
Normal 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;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user