Summer Sale! All accounts are 50% off this week.

Ajvanho's avatar
Level 14

Clear local storage - Vue SPA

I try to clear storage on browser close, but this function clear storage and in the case when refresh page...

mounted() {
            window.onbeforeunload = function() {
                localStorage.clear();
            };   
        },
0 likes
7 replies
Ajvanho's avatar
Level 14

To clear a cart or auth state...

bugsysha's avatar
bugsysha
Best Answer
Level 61

What do you store regarding auth state? You probably already know that can be dangerous.

If there is nothing harmful, then you can use sessionStorage as suggested and you do not have to worry about clearing anything.

1 like
Ajvanho's avatar
Level 14

Regarding authorization, I only keep a state of whether the user is logged in.

key: isLogIn value: false/true.

Regarding cart, I keep everything :) Is that allowed?

{"items":[{"quantity":2,"master":{"id":12,"name":"S","price":11000,"stock_count":15,"in_stock":true,"product":{"photo":"photos/coat.jpg","name":"Coat"}},"slug":"coat"},{"quantity":1,master":{"i ...
bugsysha's avatar

Regarding authorization, I only keep a state of whether the user is logged in.

Not that useful isn't it?

Regarding cart, I keep everything :) Is that allowed?

Yeah that is cool, but why don't you just keep the identifiers or something like that?

1 like
Ajvanho's avatar
Level 14

I am using isLogIn info to keep user logged in, if refresh page for example.

For SSR app-s I used session_id for the cart. Now I have decided for this approach, but next time I will like SSR.

Please or to participate in this conversation.