mirror of
https://gitlab.massbug.com/massbug/judge4c.git
synced 2025-07-04 04:12:02 +00:00
refactor(auth): update auth hooks to refresh page on success
This commit is contained in:
parent
2572b4dae5
commit
afb1b05c0a
@ -1,12 +1,16 @@
|
||||
import { client } from "@/lib/rpc";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { InferRequestType, InferResponseType } from "hono";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
type ResponseType = InferResponseType<(typeof client.api.auth.login)["$post"]>;
|
||||
|
||||
type RequestType = InferRequestType<(typeof client.api.auth.login)["$post"]>;
|
||||
|
||||
export const useLogin = () => {
|
||||
const router = useRouter();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const mutation = useMutation<ResponseType, Error, RequestType>({
|
||||
mutationFn: async ({ json }) => {
|
||||
const response = await client.api.auth.login["$post"]({
|
||||
@ -14,6 +18,10 @@ export const useLogin = () => {
|
||||
});
|
||||
return await response.json();
|
||||
},
|
||||
onSuccess: () => {
|
||||
router.refresh();
|
||||
queryClient.invalidateQueries({ queryKey: ["current"] });
|
||||
},
|
||||
});
|
||||
|
||||
return mutation;
|
||||
|
@ -1,10 +1,12 @@
|
||||
import { client } from "@/lib/rpc";
|
||||
import { InferResponseType } from "hono";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
type ResponseType = InferResponseType<(typeof client.api.auth.logout)["$post"]>;
|
||||
|
||||
export const useLogout = () => {
|
||||
const router = useRouter();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const mutation = useMutation<ResponseType, Error>({
|
||||
@ -13,6 +15,7 @@ export const useLogout = () => {
|
||||
return await response.json();
|
||||
},
|
||||
onSuccess: () => {
|
||||
router.refresh();
|
||||
queryClient.invalidateQueries({ queryKey: ["current"] });
|
||||
},
|
||||
});
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { client } from "@/lib/rpc";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { InferRequestType, InferResponseType } from "hono";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
type ResponseType = InferResponseType<
|
||||
(typeof client.api.auth.register)["$post"]
|
||||
@ -9,6 +10,9 @@ type ResponseType = InferResponseType<
|
||||
type RequestType = InferRequestType<(typeof client.api.auth.register)["$post"]>;
|
||||
|
||||
export const useRegister = () => {
|
||||
const router = useRouter();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const mutation = useMutation<ResponseType, Error, RequestType>({
|
||||
mutationFn: async ({ json }) => {
|
||||
const response = await client.api.auth.register["$post"]({
|
||||
@ -16,6 +20,10 @@ export const useRegister = () => {
|
||||
});
|
||||
return await response.json();
|
||||
},
|
||||
onSuccess: () => {
|
||||
router.refresh();
|
||||
queryClient.invalidateQueries({ queryKey: ["current"] });
|
||||
},
|
||||
});
|
||||
|
||||
return mutation;
|
||||
|
Loading…
Reference in New Issue
Block a user