Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module storage works incorrect with vuex and localstorage #490

Closed
maddocnc opened this issue Nov 27, 2019 · 1 comment
Closed

module storage works incorrect with vuex and localstorage #490

maddocnc opened this issue Nov 27, 2019 · 1 comment
Labels

Comments

@maddocnc
Copy link

maddocnc commented Nov 27, 2019

Version

v4.8.4

Reproduction link

http://later-if-need

Steps to reproduce

  1. Install auth-module in Nuxt project with vue, use following config:
auth: {
    watchLoggedIn: true,
    resetOnError: true,
    cookie: false, // <--- no cookie, only localStorage, '/personal' section of site wich need authorization runs with serverMiddleware setting mode: 'spa'
    redirect: false,
    strategies: {
      local: {
        endpoints: {
          login: { url: '/api/login/', method: 'post', propertyName: 'token' },
          logout: false,
          user: {
            url: '/api/user/',
            method: 'get',
            propertyName: 'user',
          }
        },
        tokenRequired: true,
        tokenName: 'Authorization',
        tokenType: 'Bearer'
      }
    }
  },

note that in documentation: vuex.namespace option is set to 'auth' by default...

  1. run $auth.login() then do $router.push('/personal')
  2. in /pages/personal/index.vue mounted we have:
console.log('USER', this.$auth.user, this.$auth.loggedIn, this.$store.state.auth.user, this.$store.state.auth.loggedIn)

What is expected ?

this.$auth.user, this.$auth.loggedIn to show real value equal to this.$store.state.auth.loggedIn, this.$store.state.auth.user

What is actually happening?

token is used, /api/user/ and all other endpoints with checks for token is returning data correctly.
this.$user.getToken() also seems to work.
however console.log('USER', this.$auth.user, this.$auth.loggedIn, this.$store.state.auth.user, this.$store.state.auth.loggedIn)

gives me:
USER null false {…} true

adding

async asyncData({ $axios, store, redirect, $auth }) {
      await $auth.fetchUserOnce()
}

shows correct request but nothing changed.

Additional comments?

Looks like storage engine (lib/core/storage.js) works incorrect. Stores data to my vuex store, but can't read from state directly.
While token seems to be stored using setUniversal/getUniversal, and is correctly setted in nuxt-axios instance headers.

if I add console log in lib/core/auth.js setUser and get user () I see following after call login:

> setUser false
> setUser {_id: "5dd65ff098711f313c3be6e6", email: "...",…} // several times in between goes my route push

> get user() this.$state 
loggedIn: false
strategy: "local"
user: null

> get user () this.$storage._useVuex.state.auth // storage.js this._useVuex = this.options.vuex && this.ctx.store leaves store there....
busy: false
loggedIn: true
strategy: "local"
user: Object

after refreshing page Cmd+R data reveals correctly.

This bug report is available on Nuxt community (#c448)
@ghost ghost added the cmty:bug-report label Nov 27, 2019
@JoaoPedroAS51
Copy link
Collaborator

Closing here, as this is an old issue and should be fixed in v5.
It's now recommended to use v5 instead of v4. See status and #893

P.S. I tried to reproduce the issue in v5, but it's working properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants