mirror of
https://gitlab.massbug.com/massbug/judge4c.git
synced 2025-07-04 15:52:22 +00:00
feat(auth): add toast notifications for login, logout, and registration success/error
This commit is contained in:
parent
ca0512ff14
commit
139796c89e
@ -1,3 +1,4 @@
|
|||||||
|
import { toast } from "sonner";
|
||||||
import { client } from "@/lib/rpc";
|
import { client } from "@/lib/rpc";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { InferRequestType, InferResponseType } from "hono";
|
import { InferRequestType, InferResponseType } from "hono";
|
||||||
@ -16,12 +17,21 @@ export const useLogin = () => {
|
|||||||
const response = await client.api.auth.login["$post"]({
|
const response = await client.api.auth.login["$post"]({
|
||||||
json,
|
json,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Failed to login");
|
||||||
|
}
|
||||||
|
|
||||||
return await response.json();
|
return await response.json();
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
|
toast.success("Logged in successfully");
|
||||||
router.refresh();
|
router.refresh();
|
||||||
queryClient.invalidateQueries({ queryKey: ["current"] });
|
queryClient.invalidateQueries({ queryKey: ["current"] });
|
||||||
},
|
},
|
||||||
|
onError: () => {
|
||||||
|
toast.error("Failed to login");
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return mutation;
|
return mutation;
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
import { toast } from "sonner";
|
||||||
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 { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
|
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||||
|
|
||||||
type ResponseType = InferResponseType<(typeof client.api.auth.logout)["$post"]>;
|
type ResponseType = InferResponseType<(typeof client.api.auth.logout)["$post"]>;
|
||||||
|
|
||||||
@ -12,12 +13,21 @@ export const useLogout = () => {
|
|||||||
const mutation = useMutation<ResponseType, Error>({
|
const mutation = useMutation<ResponseType, Error>({
|
||||||
mutationFn: async () => {
|
mutationFn: async () => {
|
||||||
const response = await client.api.auth.logout["$post"]();
|
const response = await client.api.auth.logout["$post"]();
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Failed to logout");
|
||||||
|
}
|
||||||
|
|
||||||
return await response.json();
|
return await response.json();
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
|
toast.success("Logged out successfully");
|
||||||
router.refresh();
|
router.refresh();
|
||||||
queryClient.invalidateQueries({ queryKey: ["current"] });
|
queryClient.invalidateQueries({ queryKey: ["current"] });
|
||||||
},
|
},
|
||||||
|
onError: () => {
|
||||||
|
toast.error("Failed to logout");
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return mutation;
|
return mutation;
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { toast } from "sonner";
|
||||||
import { client } from "@/lib/rpc";
|
import { client } from "@/lib/rpc";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { InferRequestType, InferResponseType } from "hono";
|
import { InferRequestType, InferResponseType } from "hono";
|
||||||
@ -18,12 +19,21 @@ export const useRegister = () => {
|
|||||||
const response = await client.api.auth.register["$post"]({
|
const response = await client.api.auth.register["$post"]({
|
||||||
json,
|
json,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Failed to register");
|
||||||
|
}
|
||||||
|
|
||||||
return await response.json();
|
return await response.json();
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
|
toast.success("Registered successfully");
|
||||||
router.refresh();
|
router.refresh();
|
||||||
queryClient.invalidateQueries({ queryKey: ["current"] });
|
queryClient.invalidateQueries({ queryKey: ["current"] });
|
||||||
},
|
},
|
||||||
|
onError: () => {
|
||||||
|
toast.error("Failed to register");
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return mutation;
|
return mutation;
|
||||||
|
Loading…
Reference in New Issue
Block a user