Search code examples
nuxt.jsnuxt-auth

Can't fetch user right after login, but refresh works @nuxt-auth


Login is successful, but can't fetch user infos until i refresh the page.

// Login Component
  try {
    await this.$auth.loginWith("mg_login", {
      data: this.user,
    });
  } catch (e) {
    this.$toast.error("Error!", { duration: 1500 });
  }


// Nuxt config
    auth: {
    strategies: {
        'mg_login': {
            provider: 'laravel/sanctum',
            url: `http://localhost`,
            maxAge: 60 * 60 * 120,
            endpoints: {
                user: {
                    url: '/api/user',
                    method: 'GET',
                    propertyName: false
                },
                login: {
                    url: '/login',
                    method: 'POST'
                },
                csrf: {
                    url: '/sanctum/csrf-cookie',
                    method: 'GET'
                },
            },
            cookie: {
                name: 'mg_session',
                options: {
                    path: '/',
                    sameSite: 'none',
                    maxAge: 60 * 60 * 120,
                }
            },
        },
    },
    redirect: {
        home: '/'
    },
},

After that I tried setting user infos manually by using $auth.setUser(user) or $auth.setUser({user}) in login component, also property/propertyName : ' '/undefined/false options in nuxt.config, still no hope.

Basically, can't fetch user(or set user) if I don't refresh the page. http://localhost/api/user response right below:

/api/user response (image)

What i am missing right now?


Solution

  • Deleting cookie options from nuxt config solved everything. Seems like laravel and nuxt putting cookies in same name and causing a collision.

    Still I can't understand why auth couldn't fetch user right after login, but needs to refresh the page.