简体   繁体   English

登录后无法立即获取用户,但刷新有效@nuxt-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.之后,我尝试通过在登录组件中使用 $auth.setUser(user) 或 $auth.setUser({user}) 手动设置用户信息,还在 nuxt.config 中使用 property/propertyName : ' '/undefined/false 选项,仍然没有希望。

Basically, can't fetch user(or set user) if I don't refresh the page.基本上,如果我不刷新页面,则无法获取用户(或设置用户)。 http://localhost/api/user response right below: http://localhost/api/user 响应如下:

/api/user response (image) /api/用户响应(图像)

What i am missing right now?我现在缺少什么?

Deleting cookie options from nuxt config solved everything.从 nuxt 配置中删除 cookie 选项解决了所有问题。 Seems like laravel and nuxt putting cookies in same name and causing a collision.似乎 laravel 和 nuxt 将 cookie 放在同名中并导致冲突。

Still I can't understand why auth couldn't fetch user right after login, but needs to refresh the page.我仍然不明白为什么 auth 无法在登录后立即获取用户,但需要刷新页面。

propertyName is for the older version of Auth/Nuxt , use property like below: propertyName用于旧版本的Auth/Nuxt ,使用如下property

auth: {
    strategies: {
        'mg_login': {
            provider: 'laravel/sanctum',
            url: `http://localhost`,
            maxAge: 60 * 60 * 120,
            user: {
                property: false,
            }, 
            endpoints: {
                user: {
                    url: '/api/user',
                    method: 'GET'
                },
                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: '/'
    },
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM