N
N
nuffyweb2021-01-22 16:50:36
Vue.js
nuffyweb, 2021-01-22 16:50:36

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>

there
computed: {
            ...mapState({
                currentUser: (state) => state.auth.currentUser,
            }),
        },

2. in App.vue the request is made:
created() {
            this.$store.dispatch('auth/getCurrentUser');
        },

And it turns out that when the user is logged in and we reload the page, the first menu is shown first, and after a few seconds the one you need, how can this be fixed?
You can see and feel the whole code here

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Sarvarov, 2021-01-22
@megakor

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 question

Ask a Question

731 491 924 answers to any question