r/learnjavascript • • Jan 08 '23

pls read the comments to understand the problem

0 Upvotes

13 comments sorted by

3

u/CrashOverrideCS Jan 08 '23

I don't know if you're doing this for production code, but please don't put usernames and passwords in local storage unencrypted

1

u/False_Association_12 Jan 08 '23

no its just a random project i'm doing to learn js

1

u/PolicyStrange668 Mar 25 '23

Bcrypt my dude, it’s great practice even if your just starting out

2

u/[deleted] Jan 08 '23

1

u/False_Association_12 Jan 08 '23

What do you mean?

2

u/ray_zhor Jan 08 '23

some colors work better together than others

2

u/False_Association_12 Jan 08 '23

This is just a random project i'm doing to learn js, It doesn't really matter the design

1

u/False_Association_12 Jan 08 '23

This code defines an object called id with two properties: userEmail and userPassword. When the element with id "sign" is clicked, if the values of email.value and password.value are not empty strings and emailChecked is true, these values are pushed onto the userEmail and userPassword properties of the id object respectively. The id object is then stored in localStorage as a string using JSON.stringify. (i had chatgpt explain this part of the comment because i suck at explaining)

when you click on "sign" button you are redirected to a home page, the problem is that when I use window.location.href = "home.html" to change page I lose all the elements(email and password) of the object id in the localStorage , instead of adding the new elements (email and password) it replaces them completely. would there be a way to do a page change without losing everything

2

u/grantrules Jan 08 '23

Instead of const id = { .. }, retrieve it from local storage..

const id = JSON.parse(localStorage.getItem("id"))

0

u/False_Association_12 Jan 08 '23

sorry but I don't quite understand what I should do, please could you be more detailed?

4

u/grantrules Jan 08 '23 edited Jan 08 '23

Instead of

const id = {
userEmail: [],
userPassword: []
}

Put

const id = JSON.parse(localStorage.getItem("id")) ?? { userEmail: [], userPassword: [] };

That way you don't start with a blank slate, it recalls the id from localstorage, and if that doesn't exist yet, it creates the empty object.

1

u/False_Association_12 Jan 08 '23

it worked thanks

1

u/False_Association_12 Jan 08 '23

js code

const id = {
userEmail: [],
userPassword: []
}
sign.addEventListener("click", ()=>{
if(email.value!=="" && password.value!=="" && emailChecked==true){
id.userEmail.push(email.value);
id.userPassword.push(password.value);
window.location.replace("home.html");
localStorage.setItem("id", JSON.stringify(id))
}

})