-3

I have a problem with my username and password for "login successfully" which is not connecting to database i dont know how to code it.. like getting username and password from database for the login authentication.. Here is my javascript code .....

var attempt = 3; 
function userLogin(){
    var username = document.getElementById("username").value;
    var password = document.getElementById("password1").value;
    if(username=="" || password==""){
        alert ("Please complete the required field!");
    }if(username==username || password==password){
        alert ("Login Successfully");
    }else{
            attempt --;
            document.getElementById("msg").innerHTML="<center class='text-danger'>Invalid username or password</center>";
            alert("You have left "+attempt+" login attempt;");
            if(attempt == 0){
                document.getElementById("username").disabled=true;
                document.getElementById("password1").disabled=true;
                document.getElementById("login").disabled=true;
            }
        }
        
    }

I want to get the username and password from the Mysql database for my login (login successfully).. Like for example.. entering the username and password from database then if correct it will alert login successfully then after going to the landing page.. else if not from the database it will attempt 3 times just like my code here..

Shadow
  • 33,525
  • 10
  • 51
  • 64
Christlyn
  • 3
  • 2
  • What a bad format code,and `username==username || password==password` needs to be `username==username && password==password` – flyingfox Nov 20 '22 at 09:43
  • Welcome on Stack Overflow, your code doesn't show how the code access to the database. You need a back-end layer between your JS and your database, so that the password of the database is not accessible to any visitor. – A.L Nov 20 '22 at 09:46

1 Answers1

0

I think you're trying to authenticate users here. Handling authentication on the client-side is a bad idea as it lets anybody see what the credentials from database are. You want to learn a backend like Node or PHP to do what you are trying to do. I suggest you take a look at something like Node.JS that will allow you to connect to database and make database queries. I would suggest starting out with Node + JWT if you are new to this. I am sharing some Node+Mongoose code as an example here (login-post controller).

const login_post = async (req, res) => {
//we take the user-entered data from the request
  const { email, password, username } = req.body;

  try {
//the 'User' here is a mongoose model and lets use run queries against MongoDB database but you could use an ORM for your SQL database here instead
    const user = await User.login(email, password, username);
    const token = createToken(user._id);
    res.cookie('jwt', token, { httpOnly: true, maxAge: maxAge * 1000 });
    req.body.userId = user._id;
    // console.log(req.body.userId);
    res.status(200).json({ user: user._id });
  } 
  catch (err) {
    const errors = handleErrors(err);
    res.status(400).json({ errors });
  }

}

This might seem confusing but all you need to do is- have the client side js make API calls to a backend. I believe you could use the Google Firebase database if you don't want to do that part by yourself.

Anshu
  • 80
  • 6