1

I have the following inside my nuxt.config.js file:

auth: {
  strategies: {
    local: {
      scheme: 'local',
      token: {
        property: 'meta.token',
        global: true,
      },
      user: {
        property: 'data',
      },
      endpoints: {
        login: { url: '/auth/login', method: 'post' },
        logout: { url: '/auth/logout', method: 'post' },
        user: { url: '/auth/user', method: 'get' }
      }
    },
  }
},

After a short (but still unknown amount of time for me), user is logged out. Also, after closing and opening the browser, user is always logged out. How could I persist the logged in state even after closing the browser? Also, why could be the reason for users be logged out after a short amount of time?

Inspecting cookies I have in my bearer token:

Expires / Max-Age:"Session"

And I have in my local storage:

auth._token_expiration.local:"1656703495434"
kissu
  • 40,416
  • 14
  • 65
  • 133
Kos-Mos
  • 415
  • 1
  • 5
  • 15
  • 1
    Did you tried this one? https://stackoverflow.com/a/66872372/8816585 – kissu Jul 01 '22 at 19:51
  • Sorry. it's not clear for me how this answers could be related. I guess the hint is that the problem is at vuex level? If so how could vuex get the bearer from the storage when coming back to the application after browser close/start? – Kos-Mos Jul 01 '22 at 19:57
  • 1
    You need some `localStorage` and a middleware to test it, see if the user was previously logged in. If he does, auth him again, otherwise ask for an authentication. – kissu Jul 01 '22 at 20:55
  • Your answer was actually correct. The problem was in vuex. Thank you! Solved by using nuxt auth's this.$auth.$storage.setUniversal('user', response.data, true) – Kos-Mos Jul 01 '22 at 21:09

1 Answers1

1

Using this.$auth.$storage.setUniversal('user', response.data, true) was enough to solve OP's issue.

As shown here: https://auth.nuxtjs.org/api/storage/#universal-storage

kissu
  • 40,416
  • 14
  • 65
  • 133