简体   繁体   中英

Nuxt.js trigger middleware after store dispatch

I am trying to create a simple app with authentication using Nuxt.js . I have an auth middleware which redirects me to login page if i am not logged in. The problem is in logout function which should check if there is an auth middleware on this page and redirect me to login page if it is. I was trying to write

methods: {
    async logout() {
        try {
            await this.$store.dispatch('auth/logout');
            this.$router.push(this.$route.fullPath);
        } catch (e) {
            console.log(e);
        }
    }
}

but router.push doesn't trigger middleware. What can I do with logout function to make it work?

(The main question is how to manually rerun middleware on the page without a hard refresh to check if the page requires authentication).

The problem is, basically, that if logout is called on a page that needs authentication, after 'pushing' current path to router the middleware on the current page need to be run, so the user is redirected to login page instead of staying on the page that needs user to be authenticated.

$router.push doesn't refresh page on the same path. You can try $router.go() .

If you call go() without an argument, the current page is refreshed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM