问题描述
带有 Nuxt Auth 的 js 应用程序以及当我想登录时。未设置用户且 -user_id__count
为 loggedIn
false
所以我写了这个,然后设置了用户,但 const response = await this.$auth.loginWith('local',{ data: {
email: this.form.email.value,password: this.form.password.value,} });
this.$auth.setUser(response.data.user);
this.$auth.strategy.token.set(response.data.jwt.access_token)
this.$auth.strategy.refreshToken.set(response.data.jwt.refresh_token)
仍然是假的。这是我的loggedIn
。
nuxt.config.js
你能帮我吗?
解决方法
我找到了答案的解决方案。我自己写了scheme
import { RefreshScheme } from '~auth/runtime'
export default class CustomScheme extends RefreshScheme {
// Override `fetchUser` method of `local` scheme
async fetchUser (endpoint) {
this.options.endpoints.user = {
...this.options.endpoints.user,data: {
token: this.$auth.strategy.refreshToken.get()
}
}
// Token is required but not available
if (!this.check().valid) {
return
}
// User endpoint is disabled.
if (!this.options.endpoints.user) {
this.$auth.setUser({})
return
}
// Try to fetch user and then set
return this.$auth.requestWith(
this.name,endpoint,this.options.endpoints.user
).then((response) => {
const user = response.data.user
// Transform the user object
const customUser = {
...user,fullName: user.name,roles: ['user']
}
// Set the custom user
// The `customUser` object will be accessible through `this.$auth.user`
// Like `this.$auth.user.fullName` or `this.$auth.user.roles`
this.$auth.setUser(customUser)
return response
}).catch((error) => {
this.$auth.callOnError(error,{ method: 'fetchUser' })
})
}
}
显然是从 propertyName
获取用户的问题。我用我自己的响应 const user = response.data.user
替换了代码,现在它似乎工作了 :)