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