is there a way to get data from local storage using login form in javascript?

You can do something like this:

  • get email and password as an input from the user.

  • retrieve the user by email from the localStorage .

  • if exists, compare password.

// email && password are <input /> fields
const email = document.getElementById("loginEmail");
const password = document.getElementById("loginPassword");

const btnSignin = document.getElementById("btn-signin");

function signIn() { 
    const users = JSON.parse(localStorage.getItem('users'));
    const user = users.find(u => u.email === email.value);
    
    if (user) {
      if (user.password === password.value) {
        alert("Wellcome " + user.firstName);
      } else {
        alert("Wrong password.");
      }
    } else {
      alert("User deos not exist.");
    }
}

btnSignin.addEventListener("click", signIn);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top