Auth-module: $auth not defined in plugin & resetOnError option is ignored...

Created on 22 Sep 2018  路  12Comments  路  Source: nuxt-community/auth-module

Version

v4.5.1

Reproduction link

http://prntscr.com/kxf2s5

Steps to reproduce

Follow the steps in the small detailed guide...

What is expected ?

  • To have the $auth inside the plugin to use .onError
  • To log out the user if the request fails (resetOnError is true)

What is actually happening?

  • $auth is undefined
  • The user is not logged out, so I must find alternatives like using $auth in the plugin but I'm unable to...

Example failed request because bad token: http://prntscr.com/kxf48b
Result in the local storage: http://prntscr.com/kxf4i6

Additional comments?

nuxt.config.js:

    auth: {
        // See https://auth.nuxtjs.org/getting-starterd/options
        redirect: {
            login: '/',
            logout: '/',
            user: '/dashboard'
        },
        strategies: {
            local: {
                endpoints: {
                    login: { url: '/login', method: 'post', propertyName: 'token' },
                    logout: { url: '/logout', method: 'post' },
                    user: { url: '/user', method: 'get', propertyName: 'user' }
                },
            }
        },
        resetOnError: true
    },

Also, just as a side note, the login redirect is not working either... Login works tho.

This bug report is available on Nuxt community (#c203)
bug

Most helpful comment

Move your plugs' declaration into auth

module.exports = {
    plugins: [
        <some plugins without auth>
    ],
    ...
    auth: {
        redirect: {
        ...
        },
        strategies: {
        ...
        },
        ...
        plugins: [
             '~/plugins/your-plugin-with-auth.js'
        ]
    },

All 12 comments

I'm hitting the same problem: my token to the server is expired, and so I want to logout and force getting a new token. Unfortunately resetOnError is not resetting the token, and my plugin (which is defined in the auth/plugins property as defined here rather than at the top level sees this.$auth is undefined.

Also, just as a side note, the login redirect is not working either... Login works tho.

See #134 for a workaround.

Any fixes?

I am also running into this issue. Any update?

Move your plugs' declaration into auth

module.exports = {
    plugins: [
        <some plugins without auth>
    ],
    ...
    auth: {
        redirect: {
        ...
        },
        strategies: {
        ...
        },
        ...
        plugins: [
             '~/plugins/your-plugin-with-auth.js'
        ]
    },

@themondays - Thank you. That does indeed work - missed this in the documentation.

I think this can be closed as resolved @pi0

Should definitely be more clear in the documentation

The problem with this method is that inject does not work inside a auth-plugin; so you'll need to include your plugin both places.

@themondays Thanks! Been wasting a couple of hours on this one... :(

Move your plugs' declaration into auth

module.exports = {
    plugins: [
        <some plugins without auth>
    ],
    ...
    auth: {
        redirect: {
        ...
        },
        strategies: {
        ...
        },
        ...
        plugins: [
             '~/plugins/your-plugin-with-auth.js'
        ]
    },

How does this solve making resetOnError: true work? Not really getting it.

For some reason you need to use ~ and @ syntax is not working like nuxt-config.js plugins.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

essamamdani picture essamamdani  路  3Comments

ishitatsuyuki picture ishitatsuyuki  路  4Comments

nikkanetiya picture nikkanetiya  路  3Comments

eatyrghost picture eatyrghost  路  3Comments

yuwacker picture yuwacker  路  3Comments