Answer the question
In order to leave comments, you need to log in
How and where is it correct to check whether the user is logged in and store his data?
Good afternoon!
1. There are two menus in the component 'TheNavbar'
: the first with links to registration / authorization, the second when the user is logged in
<template v-if="$currentUser">
<li class="nav-item">
<router-link
:to="{ name: 'TheSignin' }"
class="nav-link"
>
New Article
</router-link>
</li>
<li class="nav-item">
<router-link
:to="{ name: 'TheSignup' }"
class="nav-link"
>
Settings
</router-link>
</li>
<li class="nav-item">
<router-link
:to="{ name: 'TheSignup' }"
class="nav-link"
>{{ currentUser.username }}</router-link
>
</li>
</template>
<template v-else>
<li class="nav-item">
<router-link
:to="{ name: 'TheSignin' }"
class="nav-link"
>Sign in</router-link
>
</li>
<li class="nav-item">
<router-link
:to="{ name: 'TheSignup' }"
class="nav-link"
>Sign up</router-link
>
</li>
</template>
computed: {
...mapState({
currentUser: (state) => state.auth.currentUser,
}),
},
created() {
this.$store.dispatch('auth/getCurrentUser');
},
Answer the question
In order to leave comments, you need to log in
1. Make the loading state property in Vuex in the auth module.
Set to false after loading the event.
Do not display the menu yet .
2. You can also do that if there is some kind of token in the cookies, then display the menu of the authorized user.
If such a menu is displayed and the token is invalid, then it resets the menu or redirects to the authorization page. $store.state.loading === true
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question