Failed to fetch after restarting server

Viewed 181

So I'm making a website and I made a login function, For some reason when I make an account and try to login it works, but when I restart the server it says "failed to fetch" I don't know what could be changing

function validation

function validation(email, password) {
    let data = `{
"email": "${email}",
"password": "${password}"
}`;
alert(data)
    fetch("https://domain/api/user/get", {
            "method": "POST",
            "headers": {
                "Content-Type": "application/json"
            },
            "body": data
        })
        .then(async response => {
            response.json().then(json => {
               if (json.error.isError == true) {
                    document.getElementById('invalid').style.borderColor='red'
                    document.getElementById('invalid').style.visibility = 'visible'
                    document.getElementById('invalid').style.opacity = '1'
                    return document.getElementById('invalid').innerText = json.error.message
                    // return false;
                }
                location.href='/dashboard'
            });
        }).catch(e => {
          alert(e)
          return false;
          })
}

function login

function login() {
  try {
    let email = document.getElementById('nemail').value;
    let password = document.getElementById('npassword').value;
    var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (!emailReg.test(email)) {
        return false;
    }
    validation(email, password);
  } catch(e) {
    console.log(e);
    return false;
  }
}

app post

app.post("/api/user/get", async (req, res) => {
  try {
    let {email, password} = req.body

    if (!email) {
      eValid = '{"error": {"isError": true, "message": "No Email Provided"}}'
        return res.send(eValid)
    }

    if (!password) {
      eValid = '{"error": {"isError": true, "message": "No password provided"}}'
      return res.send(eValid)
    }
  if(email) {
      var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
      if(!emailReg.test(email)) {
        eValid = '{"error": {"isError": true, "message": "Invalid Email"}}'
      return res.send(eValid)
      } 
    }
    const userData = await userdb.findOne({email: email});

    if (!userData) {
      eValid = '{"error": {"isError": true, "message": "Account Not found. Is the Email correct?"}}'
        return res.send(eValid)
    }
  let hashMatch = await bcrypt.compare(password, userData.password)
    if(!hashMatch) {
      eValid = '{"error": {"isError": true, "message": "Password is Incorrect. Please Try Again."}}'
      return res.send(eValid)
    }
  eValid = '{"error": {"isError": false, "message": null}}'
  req.session.isAuth = true;
  console.log("it gets here at least")
  return res.send(eValid)
  } catch(e) {
    console.log(e)
  }
})

I don't know what I might be doing wrong D: If you guys could help that would be nice, I've tried console logging where it gets and trying to log it in but I don't know why it's throwing that error or why it works when I sign up for the first time. And sorry if the code Is a bit messy I know people get mad about that

0 Answers
Related