I have a project task to create a user registration and login form that utilizes local storage to store data. The functionality is working correctly when a new user registers by saving their email address and password in localStorage.
However, I am facing a challenge in comparing data when an unregistered user attempts to log in compared to a registered user trying to log in.
When a registered user logs in, the input values should be compared with the stored data in localStorage, and if they match, it should display "login Successful"; otherwise, it should prompt "Not a registered user."
My implementation involves using JavaScript for this purpose. Any assistance on this matter would be greatly appreciated.
Below is the code snippet:
var userData;
var usersr;
var loginData;
var usersl;
// User Registration
const registerBtn = document.getElementById('register-btn')
function UserRegistration() {
userData = [{
email: document.getElementById('register-email').value
}, {
password: document.getElementById('register-pass').value
}];
usersr = JSON.parse(localStorage.getItem('Users')) || [];
usersr.push(userData);
localStorage.setItem('Users', JSON.stringify(usersr));
location.reload();
console.log(userData);
}
// User Login
const loginBtn = document.getElementById('login-btn')
function loginUser() {
usersl = JSON.parse(localStorage.getItem('UsersLogin')) || [];
loginData = [{
loginEmail: document.getElementById('login-email').value
}, {
loginPass: document.getElementById('login-pass').value
}];
usersl.push(loginData);
localStorage.setItem('UsersLogin', JSON.stringify(usersl));
console.log(usersl);
location.reload();
}