Answer the question
In order to leave comments, you need to log in
How to check if a user is logged in to a Vue app?
Guys, hello everyone.
I am making an application and faced the following task:
Let's say there is a menu item "Profile"
, there are two views of this page:
{ path: '/profile', component: ProfileViewsPage, children: [
{ path: '', component: LoginPage },
{ path: '', component: ProfilePage }
Answer the question
In order to leave comments, you need to log in
It seems to me that you have not quite the right approach, what if you have many paths that require authorization? One of the options to add information to the router requires this authorization page or not:
{ path: '/profile', component: ProfilePage requiresAuth: true,
{ path: '/login', component: LoginPageб requiresAuth: false }
I did a bit unscientific: just globally v-if="auth" shows the content, and v-if="!auth" shows the login form. Then, when clicking on a link (for example, sent via Skype) to a specific closed page, the user receives an authorization window, and upon successful login, he immediately gets to the desired page, and there is no need to remember anywhere where to redirect. Naturally, on the server side it also checks whether the user is logged in or not.
Start a state , when loading the page, write the user data there (immediately when the page is returned or through a separate request) and then check wherever necessary in any way.
or
Use api token when making requests to the backend. And also, if necessary, get a full profile from the backend (access rights, etc.) and further as in the first option.
Well, ideally, both options should be periodically checked through the backend for rottenness.
In general, as you like within the application, one option is not correct.
Here is an example with "global service" authorization
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question