0

I have a problem with the authenticate with my Nuxt application (static SSR).
I'm using Sanctum. But the $auth variable is emptied after a refresh. So the user is disconnected.

login.vue

await axios.get(`${process.env.apiUrl}/sanctum/csrf-cookie`)

const res = await this.$auth.loginWith('laravelSanctum', {
   data: {
      email: this.userLogin.login.email,
      password: this.userLogin.login.password,
   }
})
.catch((error) => {
      this.userLogin.messageError = 'Wrong credentials'
      [ ... stop process code ...]
})

await this.$auth.$storage.setUniversal('_auth.user', JSON.stringify(res.data.user))
await this.$auth.setUser(res.data.user)

store/index.js

export const actions = {
  async nuxtServerInit({ commit, dispatch }) {
    const user = this.$auth.$storage.getUniversal('_auth.user')
    if (user) {
      await this.$auth.setUser(user)
    }
  }
}

nuxt.config.js

auth: {
  strategies: {
    laravelSanctum: {
      provider: 'laravel/sanctum',
      url: process.env.apiUrl,
      endpoints: {
        csrf: { url: '/sanctum/csrf-cookie', methods: 'GET' },
        login: { url: '/api/login', method: 'POST' },
        logout: { url: '/api/logout', method: 'POST' },
        user: false
      },
      user: {
        property: false,
        autoFetch: false
      },
      cookie: false

    }
  },
  redirect: {
    login: '/mon-compte/login',
    logout: '/mon-compte/login',
    //home: '/mon-compte/mon-espace',
    register: '/mon-compte/register'
  }
}
kissu
  • 40,416
  • 14
  • 65
  • 133
Vin Parker
  • 93
  • 9
  • So `static SSR` is `target: 'static'` + `ssr: true`? Also, is your `$auth` state getting `loggedIn` [properly set to `true`](https://stackoverflow.com/a/68081536/8816585)? Your cookie is also deleted? Seems strange, let's maybe focus on that one first. What is `await axios.get('${process.env.apiUrl}/sanctum/csrf-cookie')` for btw? Do you see the expect result from the backend in your network tab? – kissu Oct 06 '22 at 03:49
  • @kissu ! hi! Yes indeed (static SSR). When i refresh the page, $auth.loggedIn remains at true but $auth.user is emptied. And i removed the csrf-cookie call, it's changing nothing :) – Vin Parker Oct 06 '22 at 13:29

0 Answers0