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