E
E
EgoriusRus2019-12-15 18:38:28
RESTful API
EgoriusRus, 2019-12-15 18:38:28

Maintaining a session in Vuejs. How right?

Need advice from more experienced guys
What's the point. I am developing an application with fronted on vue and back on yii2
I figured out the process of authorization and login, and everything seems to be simple
. But I ran into such a problem. We need an analogue of the session to store information about the user in the application.
It is also necessary for requests, I need to know the id of the current user, and for rendering - the full name and position of the user are registered in the header.
I do this (I don’t know how correctly), when beforeCreate() the application, I throw a token into the backend, and I get all the user data and shove it into the store.
But I ran into such problems
. First, until the request was completed, naturally, undefined is displayed in the name, which not very pretty
The second, and main, when creating a component that takes all the records associated with a given user, I create a back request, inside which I pass the user id like this.
But in a situation where I have not received the user yet, and the request to receive records has already been sent, I get GET of the form
?id=undefined, which is not so hot at
all

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2019-12-15
@EgoriusRus

1. State without data
2. Sending login -> server gives token + userInfo (1 or 2 requests doesn't matter)
3. Save userInfo in store, token is in cookies
4. When reloading, load userInfo store from localStorage and start process token validation (back request)
5. initialize placeholders with empty strings, you can also hang the computed property to display the v-show login block

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question