2026-07-05 11:43:13 +00:00
|
|
|
import { useHookForm } from "@erp/core/hooks";
|
|
|
|
|
|
|
|
|
|
import { useIdentityAuthSession } from "../../context";
|
|
|
|
|
import type { IdentityAuthError } from "../../shared/entities";
|
|
|
|
|
import { type LoginForm, LoginFormSchema } from "../entities";
|
|
|
|
|
|
|
|
|
|
function getLoginErrorMessage(error: unknown) {
|
|
|
|
|
const status = (error as IdentityAuthError | undefined)?.status;
|
|
|
|
|
|
|
|
|
|
if (status === 401) {
|
|
|
|
|
return "Email o contraseña no válidos.";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status === 400) {
|
|
|
|
|
return "Revisa los datos introducidos.";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "No se ha podido iniciar sesión. Inténtalo de nuevo.";
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 10:57:33 +00:00
|
|
|
export function useLoginController() {
|
2026-07-05 11:43:13 +00:00
|
|
|
const { login } = useIdentityAuthSession();
|
|
|
|
|
|
|
|
|
|
const form = useHookForm<LoginForm>({
|
|
|
|
|
resolverSchema: LoginFormSchema,
|
|
|
|
|
initialValues: {
|
2026-07-10 11:59:44 +00:00
|
|
|
email: "admin@local.test",
|
|
|
|
|
password: "password",
|
2026-07-05 11:43:13 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const handleSubmit = form.handleSubmit(async (values) => {
|
|
|
|
|
form.clearErrors("root");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await login(values);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
form.setError("root", {
|
|
|
|
|
message: getLoginErrorMessage(error),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
form,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
};
|
|
|
|
|
}
|