问题描述
我正在尝试使用 nuxt-auth 模块,我对此模块的设置是
auth: {
cookie: false,plugins: ['~/plugins/api.js'],redirect: {
logout: '/login',login: '/',home: false
},strategies: {
local: {
scheme: 'refresh',token: {
property: 'token',maxAge: 3600
},refreshToken: {
property: 'refresh_token',data: 'refresh_token',maxAge: 60 * 60 * 24 * 30
},user: {
property: 'userDetail'
},endpoints: {
login: { url: 'http://localhost:8085/api/login_check',method: 'post',propertyName: 'token' },refresh: { url: 'http://localhost:8085/api/token/refresh',propertyName: 'refresh_token' },logout: false,user: { url: 'http://localhost:8085/api/user/fetchactive',method: 'get' }
},tokenrequired: true
}
}
}
我的“fetchactive”API 返回一个包含属性“userDetail”的 JSON,它是一个包含电子邮件地址的字符串(我也尝试将 userDetail 设为一个对象,但没有成功)。
例如
{"userDetail":{"email":"my@email.test"}}
Nuxt auth 一直告诉我“用户数据响应不包含字段 userDetail”。
我也尝试将“property”设置为 false,但在这种情况下 Nuxt auth 会查找名为“false”的字段... 我就是无法让它工作。
有人可以帮忙吗?
解决方法
发生这种情况是因为 auth.user 端点默认在响应中搜索“数据”对象,但我在那里不存在,因为您有自己的响应对象键,不包含在“数据:{.. .}
尝试在 auth.user 中添加示例中的 propertyName
endpoints: {
user: { url: 'http://localhost:8085/api/user/fetchactive',method: 'get',propertyName: '' }
}
}
,
在 nuxt.config.js 文件中使用此配置对我有用
auth: {
strategies: {
local: {
user: {
property: ''
},//other configs
}
}
//other configs
}