refactor: improve login error handling and user feedback in FormLogin and auth.controller
This commit is contained in:
parent
70e24b5abe
commit
29c8453bad
4 changed files with 168 additions and 138 deletions
|
|
@ -49,15 +49,25 @@ export const FormLogin = () => {
|
||||||
onError: ({ message }) => {
|
onError: ({ message }) => {
|
||||||
toast.error(`${t.auth.login.fail}\n${message}`);
|
toast.error(`${t.auth.login.fail}\n${message}`);
|
||||||
},
|
},
|
||||||
onSuccess: async () => {
|
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();
|
await utils.auth.getSession.invalidate();
|
||||||
setAlreadyRequested(true);
|
|
||||||
toast.success(t.auth.login.success);
|
toast.success(t.auth.login.success);
|
||||||
await router.push(redirect ? (redirect as string) : "/");
|
await router.push(redirect ? (redirect as string) : "/");
|
||||||
//window.location.replace(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) {
|
function onSubmit(fields: LoginFormValues) {
|
||||||
mutate({
|
mutate({
|
||||||
email: fields.email,
|
email: fields.email,
|
||||||
|
|
@ -89,7 +99,24 @@ export const FormLogin = () => {
|
||||||
TEMP DEV LOGIN
|
TEMP DEV LOGIN
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
|
{userNotFound ? (
|
||||||
|
<div className="rounded-md bg-red-50 p-4">
|
||||||
|
<div className="flex">
|
||||||
|
<div className="ml-3">
|
||||||
|
<h3 className="font-medium text-lg text-red-800">
|
||||||
|
Account non trovato
|
||||||
|
</h3>
|
||||||
|
<div className="mt-2 text-lg text-red-700">
|
||||||
|
<p>
|
||||||
|
Devi avere un account per effettuare il login. Se non hai un
|
||||||
|
account, contattaci per accedere al servizio.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form
|
<form
|
||||||
autoComplete="on"
|
autoComplete="on"
|
||||||
|
|
@ -102,7 +129,9 @@ export const FormLogin = () => {
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<div className="flex flex-wrap items-center gap-x-2">
|
<div className="flex flex-wrap items-center gap-x-2">
|
||||||
<FormLabel htmlFor="email">{t.auth.login.email}</FormLabel>{" "}
|
<FormLabel htmlFor="email">
|
||||||
|
{t.auth.login.email}
|
||||||
|
</FormLabel>{" "}
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -174,7 +203,7 @@ export const FormLogin = () => {
|
||||||
/>
|
/>
|
||||||
<Button
|
<Button
|
||||||
className="w-full rounded-lg bg-indigo-600 px-4 py-2 font-medium text-white duration-150 hover:bg-indigo-500 active:bg-indigo-600"
|
className="w-full rounded-lg bg-indigo-600 px-4 py-2 font-medium text-white duration-150 hover:bg-indigo-500 active:bg-indigo-600"
|
||||||
disabled={isPending || altreadyRequested}
|
disabled={isPending}
|
||||||
type="submit"
|
type="submit"
|
||||||
>
|
>
|
||||||
{isPending ? (
|
{isPending ? (
|
||||||
|
|
@ -187,7 +216,6 @@ export const FormLogin = () => {
|
||||||
</Button>
|
</Button>
|
||||||
</form>
|
</form>
|
||||||
</Form>
|
</Form>
|
||||||
|
|
||||||
<br />
|
<br />
|
||||||
<div className="px-2 pt-2 text-left text-sm">
|
<div className="px-2 pt-2 text-left text-sm">
|
||||||
<span>{t.auth.login.psa_1}</span>
|
<span>{t.auth.login.psa_1}</span>
|
||||||
|
|
@ -206,6 +234,8 @@ export const FormLogin = () => {
|
||||||
</Link>
|
</Link>
|
||||||
.
|
.
|
||||||
</div>
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,13 @@ export const apisMiddleware = async (req: NextRequest) => {
|
||||||
)?.value;
|
)?.value;
|
||||||
|
|
||||||
if (!accessToken) {
|
if (!accessToken) {
|
||||||
console.log("No access token found");
|
console.log("No access token found, path:", pathname);
|
||||||
return new NextResponse("Unauthorized", { status: 401 });
|
return new NextResponse("Unauthorized", { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = await verifyToken(accessToken);
|
const payload = await verifyToken(accessToken);
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
console.log("Invalid token");
|
console.log("Invalid token, path:", pathname);
|
||||||
return new NextResponse("Unauthorized", { status: 401 });
|
return new NextResponse("Unauthorized", { status: 401 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,7 @@ export const getAnnunciMetaByCod = async ({ cod }: { cod: string }) => {
|
||||||
}
|
}
|
||||||
const ogImage =
|
const ogImage =
|
||||||
annuncio.images && annuncio.images.length > 0 && annuncio.images[0]
|
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`;
|
: `${env.BASE_URL}/og.jpg`;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -84,19 +84,19 @@ export const logIn = async ({
|
||||||
const user = await findUser_byEmail({ email });
|
const user = await findUser_byEmail({ email });
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
console.error("User not found in login for email:", email);
|
return {
|
||||||
throw new TRPCError({
|
status: "not-found" as const,
|
||||||
code: "NOT_FOUND",
|
message:
|
||||||
message: "Errore in Login: Utente non trovato",
|
"Questo utente non esiste. Devi avere un account per effettuare il login.",
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
//Check if user is blocked or unverified
|
//Check if user is blocked or unverified
|
||||||
|
|
||||||
if (user.isBlocked) {
|
if (user.isBlocked) {
|
||||||
throw new TRPCError({
|
return {
|
||||||
code: "FORBIDDEN",
|
status: "fail" as const,
|
||||||
message: "Errore in Login: Utente bloccato",
|
message: "Errore in Login, non è possibile effettuare il login.",
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// If Admin and password is "changeme", skip password check
|
// If Admin and password is "changeme", skip password check
|
||||||
|
|
@ -110,10 +110,10 @@ export const logIn = async ({
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!passworIsMatch) {
|
if (!passworIsMatch) {
|
||||||
throw new TRPCError({
|
return {
|
||||||
code: "BAD_REQUEST",
|
status: "fail" as const,
|
||||||
message: "Errore in Login: Invalid email or password",
|
message: "Password errata, riprova.",
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue