feat(auth): add toast notifications for login, logout, and registration success/error

This commit is contained in:
ngc2207 2025-02-03 12:07:51 +08:00
parent ca0512ff14
commit 139796c89e
3 changed files with 31 additions and 1 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;