PricingBlog

Objects saved in Local Storage

  • nocodeable-1174778460309303397

    NoCodeable

    2 years ago

    I understand that local storage items are stored as strings.

    If I wanted to store a user object that contained a name, email, and role into a single local storage item, I believe I have to stringify it when I save it then parse it when I retrieve the data (hopefully this is correct).

    How would this be done in a toddle formula? I see things like Parse JSON and String. I haven't been able to get it to behave the way I would like.

    And lastly, would this be secure? Again, I don't intend to store sensitive data I just don't want to have to poll an API ever time I change pages.
  • lucasg-1174782399385706598

    Lucas G

    2 years ago

    It's not secure. Careful when saving client data in local storage
  • But it's a standard key value store so it should work
  • lucasg-1174782952337584178

    Lucas G

    2 years ago

    Also, users can technically access and modify their local storage
  • erikbeus-1174782981072752670

    Erik Beuschau

    2 years ago

    We just released formulas/actions for working with local and session storage 😊 https://discord.com/channels/972416966683926538/972416966683926541/1174257744031780914
    And they’re already stringifying/parsing when saving/reading so you can save objects and arrays. Reg. security, I would recommend saving something like non sensitive user preferences/selected filter values etc. But if you have public data that you want to persist across pages, that’s also a good use case 👍
    👍1
  • nocodeable-1174784225816363028

    NoCodeable

    2 years ago

    Thanks for the responses. Regarding the local storage already stringifying and parsing, does that mean if I save a localstorage item with key of currentUser and value of {name: Mike, email: mike@email.com} that I would access the name value as currentUser.name?
  • erikbeus-1174797760592093324

    Erik Beuschau

    2 years ago

    That is correct. In that case, when you read it it will be JSON parsed and return a regular object
  • nocodeable-1174799985963974656

    NoCodeable

    2 years ago

    Thanks @Erik Beuschau - and one last question sort of off topic.. Instead of storing user data in local or session storage, it seems better to store it in a variable. I know there can be variables set on components and pages. Is there a concept of app variables that can be set once and used throughout the application?
  • Tod-1174799987499081778

    Tod

    2 years ago

    Great energy @NoCodeable! Your continuous contribution to the toddle Community just made you advance to Community Level 1!
  • erikbeus-1174813512153960519

    Erik Beuschau

    2 years ago

    No. Right now I would recommend sessions storage or storing a variable on the page that you pass down to child components as attributes.
  • nocodeable-1174838204009042040

    NoCodeable

    2 years ago

    So is session storage more secure than local storage?
  • lucasg-1174845078771073139

    Lucas G

    2 years ago

    No, there’s just no global variable functionality right now within toddle.
    👍1