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;