+ 2

i want to use local storage to directly go to my account without having to make a new one at <div id='first'>

<div id="first"> <h1>make an account</h1> <form onsubmit="return false"> new Name:<input id='nus' type="text" placeholder="Type your name here" > new password:<input id='npass' type="password" placeholder="Type your password here" > <button onclick="addaccount()" type="submit">add</button> </div> <div id="scnd" style="display: none;"> <p id='txt'></p> <form onsubmit="return false"> Name:<input id='bar' type="text" placeholder="Type your name here" > password:<input id='pass' type="password" placeholder="Type your password here" > <button onclick="insert()" type="submit" id='insbtn'>insert</button> </form> <h1 id="acc" style="display: none;"> example for text <br>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Magni ipsum voluptatibus quis eos laudantium commodi quae pariatur magnam in rem odio, neque officia tempora repellat ea soluta atque eius expedita!</h1> </div> <script> function addaccount(){ var first=document.getElementById('first') const nus=document.getElementById('nus').value; const npass=document.getElementById('npass').value; localStorage.setItem('nus',nus) localStorage.setItem('npass',npass) first.style.display='none' scnd.style.display='block' } function insert(){ var scnd =document.getElementById('scnd') var txt=document.getElementById('txt'); var bar =document.getElementById('bar').value; var pass=document.getElementById('pass').value; var insbtn = document.getElementById('insbtn'); var acc=document.getElementById('acc') localStorage.setItem('name',bar); localStorage.setItem('password',pass); var getus=localStorage.getItem('name') var getpass=localStorage.getItem('password'); if (localStorage.getItem('nus')==bar&&localStorage.getItem('npass')==pass){ acc.style.display='block'; txt.innerHTML='hello'+' '+getus } else{ txt.innerHTML='error not your account' } } </script>

15th Sep 2020, 12:23 AM
Youssef Ashraf
Youssef Ashraf - avatar
2 odpowiedzi
+ 3
Assign `window.onload` to a function that searches in local storage, if the data is right then just login, it no date is found, or is found but it's wrong, do nothing. Like this: `window.onload = (event) => { // Your code }` Just to let you know, storing sensitive information in local storage, session storage or cookies is a bad idea. If this is a serious project, use JWT, hash user's data (with bcrypt) and save it to a database
29th Sep 2020, 3:26 PM
Martín Stanicio
0
Thanks for your help
29th Sep 2020, 3:54 PM
Youssef Ashraf
Youssef Ashraf - avatar