Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
How to put an error message when putting the credentials wrong in a login

I am doing a login, and I need that when I enter the wrong password or the email I get an error message but I do not know how to do it, I am working with the POST method and also in nextjs

this is my code:

function Login({}) {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  useEffect(() => {
    if (localStorage.getItem("user-info")) {
    }
  }, []);
  async function login() {
    console.warn(email, password);
    let item = { email, password };
    let result = await fetch(
      "https://login-test.repl.co/auth/login",
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(item),
      }
    );
    result = await result.json();
    localStorage.setItem("user-info", JSON.stringify(result));
  }
  return (
    <div className="bg-white w-full rounded-lg shadow p-2 h-screen flex justify-center items-center relative">
      <div className="w-full ">
        <input
          id="inputEmail"
          onChange={(e) => setEmail(e.target.value)}
          type="email"
        />
        <input
          id="inputPassword"
          onChange={(e) => setPassword(e.target.value)}
          type="password"
        />
        <button type="submit" onClick={login}>
          Login
        </button>
        <div className={`${!error ? "hidden" : ""}`}>
          INCORRECT PASSWORD OR EMAIL
        </div>
      </div>
    </div>
  );
}
export default Login;

when I put the wrong password or the mail they send me this:

enter image description here

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

Can you check like this

function Login({}) {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState(null);
  useEffect(() => {
    if (localStorage.getItem("user-info")) {
    }
  }, []);
  async function login() {
   setError(false);
    console.warn(email, password);
    let item = { email, password };
    try {
        let result = await fetch(
          "https://login-test.repl.co/auth/login",
          {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(item),
          }
        );
        result = await result.json();
        localStorage.setItem("user-info", JSON.stringify(result));

    } catch(error) {
        setError(true);
    }
  }
  return (
    <div className="bg-white w-full rounded-lg shadow p-2 h-screen flex justify-center items-center relative">
      <div className="w-full ">
        <input
          id="inputEmail"
          onChange={(e) => setEmail(e.target.value)}
          type="email"
        />
        <input
          id="inputPassword"
          onChange={(e) => setPassword(e.target.value)}
          type="password"
        />
        <button type="submit" onClick={login}>
          Login
        </button>
        <div className={`${!error ? "hidden" : ""}`}>
          INCORRECT PASSWORD OR EMAIL
        </div>
      </div>
    </div>
  );
}
export default Login;
about 4 years ago · Santiago Gelvez Denunciar

0

You can achieve this by checking the status code.

If you are getting status of 200 then you can update the state as follows:

if(result.status === 200)
{
    setEmail("Email Already Exists");
}

You can get more status codes and you can set the errors as you like.

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda