From 139796c89e2b8d5967165f8b63c62230b993bb4b Mon Sep 17 00:00:00 2001 From: ngc2207 Date: Mon, 3 Feb 2025 12:07:51 +0800 Subject: [PATCH] feat(auth): add toast notifications for login, logout, and registration success/error --- src/features/auth/api/use-login.ts | 10 ++++++++++ src/features/auth/api/use-logout.ts | 12 +++++++++++- src/features/auth/api/use-register.ts | 10 ++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/features/auth/api/use-login.ts b/src/features/auth/api/use-login.ts index 3825105..c987548 100644 --- a/src/features/auth/api/use-login.ts +++ b/src/features/auth/api/use-login.ts @@ -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; diff --git a/src/features/auth/api/use-logout.ts b/src/features/auth/api/use-logout.ts index 9d13910..dc7268c 100644 --- a/src/features/auth/api/use-logout.ts +++ b/src/features/auth/api/use-logout.ts @@ -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({ 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; diff --git a/src/features/auth/api/use-register.ts b/src/features/auth/api/use-register.ts index b9cfc0a..09d8ead 100644 --- a/src/features/auth/api/use-register.ts +++ b/src/features/auth/api/use-register.ts @@ -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;