From d4d384ba2bc0eb1900c61a0cc25057e5698d8753 Mon Sep 17 00:00:00 2001
From: juan
Date: Mon, 11 Aug 2025 14:06:13 +0200
Subject: [PATCH] fix: update validation logic in useValidation hook and
improve error handling in useLogin hook
---
src/lib/hooks/useValidation.tsx | 5 +++--
src/lib/hooks/user/useLogin.tsx | 15 ++++++++++-----
src/lib/validation/user.ts | 4 ++--
src/routes/__root.tsx | 2 +-
src/routes/_authed.tsx | 7 +++----
src/routes/login.tsx | 1 -
6 files changed, 19 insertions(+), 15 deletions(-)
diff --git a/src/lib/hooks/useValidation.tsx b/src/lib/hooks/useValidation.tsx
index f21010d..108f996 100644
--- a/src/lib/hooks/useValidation.tsx
+++ b/src/lib/hooks/useValidation.tsx
@@ -1,5 +1,5 @@
import { useState } from "react"
-import type { z } from "zod"
+import { z } from "zod"
type FormDataValidation = Record
@@ -26,7 +26,8 @@ export const useValidation = ({
if (!result.success) {
//FIXME: Flatten errors, new in zod v4
- setErrors(result.error.flatten().fieldErrors as T)
+ // setErrors(result.error.flatten().fieldErrors as T)
+ setErrors(z.flattenError(result.error).fieldErrors as T)
return false
}
diff --git a/src/lib/hooks/user/useLogin.tsx b/src/lib/hooks/user/useLogin.tsx
index d2d6412..fe6e47d 100644
--- a/src/lib/hooks/user/useLogin.tsx
+++ b/src/lib/hooks/user/useLogin.tsx
@@ -15,10 +15,15 @@ export const useLogin = () => {
})
const loginMutation = useMutation({
mutationKey: ["login"],
- mutationFn: async (data: TLoginForm) =>
- loginUser({
+ mutationFn: async (data: TLoginForm) => {
+ const response = await loginUser({
data
- }),
+ })
+
+ if (response.error) {
+ throw new Error(response.message)
+ }
+ },
onMutate: () => {
toast.loading("Logging in...", { id: "login" })
},
@@ -28,8 +33,8 @@ export const useLogin = () => {
to: "/post"
})
},
- onError: () => {
- toast.error("Failed to log in.", { id: "login" })
+ onError: (error) => {
+ toast.error(error.message, { id: "login" })
}
})
diff --git a/src/lib/validation/user.ts b/src/lib/validation/user.ts
index a9c8259..bf73223 100644
--- a/src/lib/validation/user.ts
+++ b/src/lib/validation/user.ts
@@ -1,12 +1,12 @@
import z from "zod"
export const loginFormSchema = z.object({
- email: z.string("Invalid email address"),
+ email: z.email("Invalid email address"),
password: z.string().min(1, "Password must be at least 1 character long")
})
export const signupFormSchema = z.object({
- email: z.string("Invalid email address"),
+ email: z.email("Invalid email address"),
password: z.string().min(6, "Password must be at least 6 characters long"),
redirectUrl: z.string().optional()
})
diff --git a/src/routes/__root.tsx b/src/routes/__root.tsx
index e424124..900fe74 100644
--- a/src/routes/__root.tsx
+++ b/src/routes/__root.tsx
@@ -19,7 +19,7 @@ export const Route = createRootRouteWithContext()({
beforeLoad: async () => {
const user = await getUser()
return {
- user
+ ...user
}
},
head: () => ({
diff --git a/src/routes/_authed.tsx b/src/routes/_authed.tsx
index 4580cfd..ec1f096 100644
--- a/src/routes/_authed.tsx
+++ b/src/routes/_authed.tsx
@@ -1,10 +1,10 @@
-import { createFileRoute } from "@tanstack/react-router"
+import { createFileRoute, redirect } from "@tanstack/react-router"
export const Route = createFileRoute("/_authed")({
beforeLoad: ({ context }) => {
- console.log("contextw", context)
- if (!context?.user) {
+ if (context.error) {
throw new Error("Not authenticated")
+ // TODO: Redirect to login page
}
},
errorComponent: ({ error }) => {
@@ -15,7 +15,6 @@ export const Route = createFileRoute("/_authed")({
)
}
-
throw error
}
})
diff --git a/src/routes/login.tsx b/src/routes/login.tsx
index a506496..e24c272 100644
--- a/src/routes/login.tsx
+++ b/src/routes/login.tsx
@@ -12,7 +12,6 @@ function LoginComp() {
const handleSubmit = (e: FormEvent) => {
e.preventDefault()
-
const formData = new FormData(e.currentTarget)
login({