登录后无法立即获取用户,但刷新有效@nuxt-auth
Can't fetch user right after login, but refresh works @nuxt-auth
登录成功,但无法获取用户信息,直到我刷新页面。
// 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: '/'
},
},
之后,我尝试在登录组件中使用 $auth.setUser(user) 或 $auth.setUser({user}) 手动设置用户信息,也 property/propertyName :' '/undefined/false 中的选项 nuxt.config,仍然没有希望。
基本上,如果我不刷新页面就无法获取用户(或设置用户)。 http:///localhost/api/user 回复在下方:
/api/user response (image)
我现在缺少什么?
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: '/'
},
},
从 nuxt 配置中删除 cookie 选项解决了所有问题。似乎 laravel 和 nuxt 将 cookie 放在同一个名称中并导致冲突。
我还是不明白为什么auth在登录后无法立即获取用户,而是需要刷新页面。
登录成功,但无法获取用户信息,直到我刷新页面。
// 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: '/'
},
},
之后,我尝试在登录组件中使用 $auth.setUser(user) 或 $auth.setUser({user}) 手动设置用户信息,也 property/propertyName :' '/undefined/false 中的选项 nuxt.config,仍然没有希望。
基本上,如果我不刷新页面就无法获取用户(或设置用户)。 http:///localhost/api/user 回复在下方:
/api/user response (image)
我现在缺少什么?
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: '/'
},
},
从 nuxt 配置中删除 cookie 选项解决了所有问题。似乎 laravel 和 nuxt 将 cookie 放在同一个名称中并导致冲突。
我还是不明白为什么auth在登录后无法立即获取用户,而是需要刷新页面。