Click here to Skip to main content
15,882,241 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does anyone know the solution for this?

This is the front end that i used to login to the students

When I am entering the respective values and submit it will not redirect to the relevant page. Also when I inspect there is no error in the console.

in user email field use have to enter their email and in the password section they have to enter their password


What I have tried:

import React, { useState } from 'react'
    import axios from "axios";
    import ErrorMessage from './ErrorMessage';
    
    
    const Login = () => {
    
      const [useremail, setUserEmail] = useState("");
      const [userpassword, setUserPassword] = useState("");
      const [error,setError] = useState(false);
      const [message, setMessage] = useState(null);
    
      
      
      const saveData =async (e)=>{
         e.preventDefault();
        const userInfo = localStorage.getItem("usernfo");
        try {
    
          const config = {
            headers: {
              "Content-type": "application/json"
            }
          };
          
          const {data} = await axios.post(
            "http://localhost:5000/user/login",
            {useremail,userpassword},config
          );
          console.log(data);
          localStorage.setItem("userInfo",JSON.stringify(data));
          
        } catch (error) {
           setError(error.message);
        }  
     
        if (userInfo) {
          setMessage("Success");
          window.location.assign("/home");
         
        } else {
          setMessage("Invalid Credentials");     
        }
      }
      return (
        <div>
          <h1>User Login</h1>
          {message && <ErrorMessage variant="danger">{message}</ErrorMessage>}
          {error && <ErrorMessage variant="danger">{error}</ErrorMessage>}
          <form onSubmit={saveData}>
            <label>User Email</label>
            <input
              type="email"
              placeholder="Enter your email"
              value={useremail}
              style={{ margin: 5 }}
              onChange={(e) => setUserEmail(e.target.value)}
            />
            <br></br>
            <label>User Password</label>
            <input
              type="password"
              placeholder="Enter your password"
              value={userpassword}
              style={{ margin: 5 }}
              onChange={(e) => setUserPassword(e.target.value)}
            />
            <br></br>
            <button style={{ margin: 5 }}>Submit</button>
            New user? <a href="/register">Register</a>
          </form>
        </div>
      );
    }
    
    export default Login 
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900