websanova / vue-auth

A simple light-weight authentication library for Vue.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

vue-auth not redirecting after axios received 401 error

ddzobov opened this issue · comments

package.json:

"@websanova/vue-auth": "^4.1.1",
"axios": "^0.21.1",
"vue": "^2.6.12",
"vue-axios": "^2.1.5",
"vue-router": "^3.5.1",

vue-auth config:

Vue.use(require('@websanova/vue-auth/dist/v2/vue-auth.common.js'), {
    plugins: {
        http: Vue.axios,
        router: Vue.router,
    },
    drivers: {
        auth: require('@websanova/vue-auth/dist/drivers/auth/bearer.js'),
        http: require('@websanova/vue-auth/dist/drivers/http/axios.1.x.js'),
        router: require('@websanova/vue-auth/dist/drivers/router/vue-router.2.x.js'),
    },
    options: {
        authRedirect: '/login',
        logoutData: {
            redirect: '/login',
            makeRequest: true,
        },
        parseUserData: function (data) {
            return data.response.user || {};
        },
        rolesKey: 'permissions',
    }
});

i've added some debug to drivers/router/vue-router.2.x.js:

      routerGo: function (data) {
        var router = this.plugins.router;
        console.log('process redirect', data);
        (router.push || router.go).call(router, data).catch(function (err) { console.log(err); });
      }

And i see this in my console (Safari latest, MacOS):

[Error] Failed to load resource: the server responded with a status of 401 (Unauthorized) (table, line 0)
[Log] process redirect – {redirect: {path: "/login", name: "login"}}
[Log] NavigationDuplicated: Avoided redundant navigation to current location: "/systems". <---- this is my current location

Hmm, so ya, it's not logging you out, and doing a redirect loop back to systems...

You may need to investigate the http driver...

https://github.com/websanova/vue-auth/blob/master/src/drivers/http/axios.1.x.js#L39

Double check if that is indeed returning true for invalid token...which should auto trigger logout...

https://github.com/websanova/vue-auth/blob/master/src/auth.js#L229

Yes, all of this works.

This is final step - vue auth making bad redirect (or vue router buggy?)

Do you have any auth setup in the routes, also what is your authRedirect set to?

https://github.com/websanova/vue-auth/blob/master/src/auth.js#L28

The problem is indeed with the vue-router plugin.
In version 3.x the router.push(obj) method accepts objects like {path: '/login'}, but the driver passes this object wrapped like {redirect: {path: '/login'}}

Quick fix is to override the existing router driver, and simply change this line to:
(router.push || router.go).call(router, data.redirect).catch(function (err) {});

Better solution is to implement the whole 3.x router driver.

Hmm, should work, redirect and push are not the same thing.

Should be fixed by #673