0

i am creating a simple login form using frontend react backend node js.while attempt to login login failure. if we type wrong email and password alert msg displays login success. i don't know why.my backend i tested through postman it is work well.check through react had a problem.what i tried so far i attached below.

import axios from "axios";
import {  useState } from "react";
import { useNavigate } from 'react-router-dom';

function Login() {

    const [email, setEmail] = useState("");
    const [password, setPassword] = useState("");
    const navigate = useNavigate();
    


    async function login(event) {
        event.preventDefault();
        try {
          await axios.post("http://localhost:9992/student/login", {

             email: email,
            password: password,
           
          });
         
          alert("Login Successfully"); 
         
              
        //  navigate('/home');
         
        
        } catch (err) {
          alert("ogin Failed");
        }
      }



    return (
     <div>
            <div class="container">
            <div class="row">
                <h2>Login</h2>
             <hr/>
             </div>

             <div class="row">
             <div class="col-sm-6">
 
             <form>
             <div class="form-group">
          <label>email</label>
          <input type="email"  class="form-control" id="email" placeholder="Enter Name"
          
          value={email}
          onChange={(event) => {
            setEmail(event.target.value);
          }}
          
          />
          
          
          
        </div>

        <div class="form-group">
            <label>password</label>
            <input type="password"  class="form-control" id="password" placeholder="Enter Fee"
            
            value={password}
            onChange={(event) => {
              setPassword(event.target.value);
            }}
            
            />
          </div>
                  <button type="submit" class="btn btn-primary" onClick={login} >Login</button>
              </form>

            </div>
            </div>
            </div>

     </div>
    );



  
4
  • Your current logic isn't checking for login validation? Inside the try block you can assign the axios.post call's results to a variable data and based on that data you then use condition checker statement like if...else and respond accordingly. Commented Jan 30, 2023 at 9:34
  • { "status": false, "message": "Student Error Detailssss" } Commented Jan 30, 2023 at 9:35
  • back end "status": true, means login success Commented Jan 30, 2023 at 9:36
  • how to write this sir Commented Jan 30, 2023 at 9:36

1 Answer 1

1

I am using the data value based on your comment.

Modify your login function as following:

async function login(event) {
event.preventDefault();
 try {
  const res = await axios.post("http://localhost:9992/student/login", {
  email: email,
  password: password,
  });
  const data = res.data;

  if (data.status === true) {
  alert("Login Successfully"); 
  } else {
  alert("Login failed")
  }

 } catch (err) {
  alert("Error, please try again");
 }
}
Sign up to request clarification or add additional context in comments.

2 Comments

i got the error error res.data . i attached the node js above
Login failed displayed i tried above. if i enter the correct data also faild

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.