Session/Local Variables for Quasar



  • I am creating a login page for my application, and I was thinking how can I store my username as a session variable such that the username will be display on my pages after I have logged in.

    I have looked at the Local/Session Plugin from https://quasar.dev/quasar-plugins/web-storage#Introduction but was unclear of how I should implement it in my code.

    I have followed and used the code from the documentation in my <script> tag as shown:

    this.$q.localStorage.set(key, value)
    let value = this.$q.localStorage.getItem(key)
    

    This is a variable that I use to store the username from the username input textfield:

    var current_username = this.userToSave.username;
    

    Are there any examples I could reference to for the Local/Session Plugin? And if I used the above code in my login page, how can I access the session variable in my other pages?

    Would appreciate some help on this, thank you!



  • I have figured out how it works!

    At login page:

    this.$q.localStorage.set("username", current_username)
    

    At main page:

    let value = this.$q.localStorage.getItem("username")
    

    If I want to logout from my application, do I do the following?

    this.$q.localStorage.clear()
    


  • If you clear the localStorage, you’ll remove all data, not only the username. It’s safer to use this.$q.localStorage.remove("username").
    I’ll advise you to use vuex instead, keep username in the store and use actions to login/logout/authenticate/etc.
    https://vuex.vuejs.org/



  • @jraez Oh, I see. Will try that, thanks alot for the help! 🙂


Log in to reply