Saving user information

I managed to login with passport to laravel. I have a sign, great. But I want to save user information, name, avatar, etc.

My login procedure is getting an oauth token. In the dashboard component, I am making an api call for user data.

Should I store them in a global Vue object or use Vuex? It's safe?

+3


source to share


2 answers


Some options you might consider

  • store data in cookies
  • use localStorage
  • keep everything in the root vue instance
  • keep everything in component wrapping vue

My suggestion was to store the authentication token - in fact, this is required to successfully invoke your backend - in a cookie. This will make it easier to access it with every request you send.

To store user information, I would suggest creating a packaging component or using a root vue instance. The next example should clear this up.

wrapper home component (inline template)

data: function() {
  return { userinfo: {} }
},

created: function() {
  // load your user info
}

      

Then use it in your index.html / main view



<body>
  <home inline-template>
    <!-- any stuff here -->

    <!-- pass the user prop to every component that is shown in the userinfo -->
    <router-view :user="userinfo"></router-view>
  </home>
</body>  

      

Your components rendered in the router view can then access the custom declaration

exemplary component

...

props: ['user'],

...

<template>
   <span>{{ user.name }}</span>
</template>

...

      

IMPORTANT: To do this work you will also need to add : true props to your route definition. Explained in detail here: https://router.vuejs.org/en/essentials/passing-props.html

Note. If you don't want to load custom data into your packaging component, you can load it elsewhere and use the event bus to pass the results to your packaging component. However, you should always only have ONE source of truth regarding user information. Store it in one place only.

+2


source


I found the session helper to be very easy to use and it solved the problem of globally using the oauth token:



See: https://laravel.com/docs/5.4/session , title: Global Session Helper

+1


source







All Articles