Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda