Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
extraer datos de la matriz en localStorage

Tengo una función que toma datos de una entrada y los guarda como datos en una matriz durante el registro. Ahora quiero otra función para verificar durante el inicio de sesión si los datos existen y si coinciden. ¿Cómo puedo hacer esto usando solo javascript? En resumen, necesito una función que verifique si los datos ingresados por el usuario existen y, de ser así, inicie sesión.

 function saveData() { let name, email, password; name = document.getElementById("username").value; email = document.getElementById("email").value; password = document.getElementById("password").value; let user_records = new Array(); user_records = JSON.parse(localStorage.getItem("users")) ? JSON.parse(localStorage.getItem("users")) : []; if ( user_records.some((v) => { return v.email == email; }) ) { alert("Email wykorzystany"); } else { user_records.push({ name: name, email: email, password: password, }); localStorage.setItem("users", JSON.stringify(user_records)); } }

Sé que no es así como se debe hacer el registro. Solo lo hago para aprender cosas nuevas.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

function saveAndTestUser() { // here use const as they are not updated const name = document.getElementById("username").value; const email = document.getElementById("email").value; const password = document.getElementById("password").value; // Get all the records of the users // prefer camelcase : not a rule though but it's better this way const storedUsers = localStorage.getItem('users') // if there are no stored users then assign empty array // so that we don't get unexpected errors const userRecords = storedUsers ? JSON.parse(storedUsers): [] // Checking if email already exists if(userRecords.some(user => user.email === email)){ alert("user already exists") return false; // stops the function execution } // If email doesn't exists then the code below will be executed // Similar to if-else // Add current record to existing records const newRecords = [...storedUsers, {name, email, password}] // Set the new record to storage localStorage.setItem("users", JSON.stringify(newRecords)); }
about 4 years ago · Juan Pablo Isaza Report

0

este es un inicio de sesión básico, cuando verifique que el correo electrónico y la contraseña sean correctos, puede hacer lo que quiera

 function checkData() { const name = document.getElementById('username').value; const password = document.getElementById('password').value; let user_records = JSON.parse(localStorage.getItem('users')) || []; if ( user_records.find((user) => { return user.name == name && user.password == password; }) ) { alert('Logged in'); // do your things here } else { alert('wrong email or password'); // do your things here } }
 <input id="username" /> <input id="password" /> <input type="submit" value="submit" onClick="checkData()" />

Extra: Esto es algo que puede hacer solo con fines de aprendizaje, que es agregar una clave al almacenamiento local, por ejemplo: localStorage.setItem('loggedIn', true) y establecer en cada página una verificación de este valor, si es verdadero mostrar la página, si es falso redirigir para iniciar sesión. En el mundo real, usamos tokens JWT que contienen toda la información sobre el usuario, etc. Puede buscar autenticación de token JWT en Google y aprender eso, lo cual es realmente útil en el mundo front-end.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!