From 29c8453bad28949d95504d57c976090537972fb1 Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Mon, 24 Nov 2025 10:51:51 +0100 Subject: [PATCH] refactor: improve login error handling and user feedback in FormLogin and auth.controller --- apps/infoalloggi/src/forms/FormLogin.tsx | 274 ++++++++++-------- .../src/middlewares/api_middleware.ts | 4 +- .../server/controllers/annunci.controller.ts | 2 +- .../src/server/controllers/auth.controller.ts | 26 +- 4 files changed, 168 insertions(+), 138 deletions(-) diff --git a/apps/infoalloggi/src/forms/FormLogin.tsx b/apps/infoalloggi/src/forms/FormLogin.tsx index a01174c..1273155 100644 --- a/apps/infoalloggi/src/forms/FormLogin.tsx +++ b/apps/infoalloggi/src/forms/FormLogin.tsx @@ -49,15 +49,25 @@ export const FormLogin = () => { onError: ({ message }) => { toast.error(`${t.auth.login.fail}\n${message}`); }, - onSuccess: async () => { - await utils.auth.getSession.invalidate(); - setAlreadyRequested(true); - toast.success(t.auth.login.success); - await router.push(redirect ? (redirect as string) : "/"); - //window.location.replace(redirect ? (redirect as string) : "/"); + onSuccess: async (data) => { + switch (data.status) { + case "not-found": + setUserNotFound(true); + toast.error(data.message); + return; + case "fail": + toast.error(data.message); + return; + case "success": + await utils.auth.getSession.invalidate(); + toast.success(t.auth.login.success); + await router.push(redirect ? (redirect as string) : "/"); + //window.location.replace(redirect ? (redirect as string) : "/"); + return; + } }, }); - const [altreadyRequested, setAlreadyRequested] = useState(false); + const [userNotFound, setUserNotFound] = useState(false); function onSubmit(fields: LoginFormValues) { mutate({ email: fields.email, @@ -89,123 +99,143 @@ export const FormLogin = () => { TEMP DEV LOGIN )} - -
- - ( - -
- {t.auth.login.email}{" "} - -
- - - - -
- )} - /> - ( - -
- -
{t.auth.login.password}
- - {t.auth.login.password_forgotten} - -
- -
- - - - -
- )} - /> - ( - -
- Check Me - -
- - - - -
- )} - /> - - - + + + + ) : ( + <> +
+ + ( + +
+ + {t.auth.login.email} + {" "} + +
-
-
- {t.auth.login.psa_1} - - {t.auth.login.psa_terms} - - {t.auth.login.psa_2} - - {t.auth.login.psa_privacy} - - . -
+ + + +
+ )} + /> + ( + +
+ +
{t.auth.login.password}
+ + {t.auth.login.password_forgotten} + +
+ +
+ + + + +
+ )} + /> + ( + +
+ Check Me + +
+ + + + +
+ )} + /> + + + +
+
+ {t.auth.login.psa_1} + + {t.auth.login.psa_terms} + + {t.auth.login.psa_2} + + {t.auth.login.psa_privacy} + + . +
+ + )} ); diff --git a/apps/infoalloggi/src/middlewares/api_middleware.ts b/apps/infoalloggi/src/middlewares/api_middleware.ts index bf98e7a..9ec3875 100644 --- a/apps/infoalloggi/src/middlewares/api_middleware.ts +++ b/apps/infoalloggi/src/middlewares/api_middleware.ts @@ -27,13 +27,13 @@ export const apisMiddleware = async (req: NextRequest) => { )?.value; if (!accessToken) { - console.log("No access token found"); + console.log("No access token found, path:", pathname); return new NextResponse("Unauthorized", { status: 401 }); } const payload = await verifyToken(accessToken); if (!payload) { - console.log("Invalid token"); + console.log("Invalid token, path:", pathname); return new NextResponse("Unauthorized", { status: 401 }); } } diff --git a/apps/infoalloggi/src/server/controllers/annunci.controller.ts b/apps/infoalloggi/src/server/controllers/annunci.controller.ts index c696ff0..d626725 100644 --- a/apps/infoalloggi/src/server/controllers/annunci.controller.ts +++ b/apps/infoalloggi/src/server/controllers/annunci.controller.ts @@ -110,7 +110,7 @@ export const getAnnunciMetaByCod = async ({ cod }: { cod: string }) => { } const ogImage = annuncio.images && annuncio.images.length > 0 && annuncio.images[0] - ? `${env.BASE_URL}/storage-api/get/${annuncio.images[0]}?image=true` + ? `${env.BASE_URL}/storage-api/get/${annuncio.images[0].img}?image=true` : `${env.BASE_URL}/og.jpg`; return { diff --git a/apps/infoalloggi/src/server/controllers/auth.controller.ts b/apps/infoalloggi/src/server/controllers/auth.controller.ts index d5c6c33..2903b3d 100644 --- a/apps/infoalloggi/src/server/controllers/auth.controller.ts +++ b/apps/infoalloggi/src/server/controllers/auth.controller.ts @@ -84,19 +84,19 @@ export const logIn = async ({ const user = await findUser_byEmail({ email }); if (!user) { - console.error("User not found in login for email:", email); - throw new TRPCError({ - code: "NOT_FOUND", - message: "Errore in Login: Utente non trovato", - }); + return { + status: "not-found" as const, + message: + "Questo utente non esiste. Devi avere un account per effettuare il login.", + }; } //Check if user is blocked or unverified if (user.isBlocked) { - throw new TRPCError({ - code: "FORBIDDEN", - message: "Errore in Login: Utente bloccato", - }); + return { + status: "fail" as const, + message: "Errore in Login, non รจ possibile effettuare il login.", + }; } // If Admin and password is "changeme", skip password check @@ -110,10 +110,10 @@ export const logIn = async ({ }); if (!passworIsMatch) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Errore in Login: Invalid email or password", - }); + return { + status: "fail" as const, + message: "Password errata, riprova.", + }; } }