Pao*_*o95 3 javascript vue.js vuex nuxt.js
最近我开始学习如何使用 Nuxtjs,在学习如何使用其 Auth 模块时我遇到了一个问题。
我能够登录,并且想检查帐户的范围,我尝试使用“Auth”的“scopeKey”属性来执行此操作。从后端我从数据库获取“范围”,它可以是“用户”或“管理员”。
我尝试设置范围
scopeKey: 'scope'
Run Code Online (Sandbox Code Playgroud)
但我在检查时发现范围是“未定义”/“空”
this.$auth.hasScope('admin') / this.$auth.hasScope('user')
Run Code Online (Sandbox Code Playgroud)
或“this.$auth.hasScope(admin)”将“scopeKey”设置为时返回空值
scopeKey: 'data.scope'
Run Code Online (Sandbox Code Playgroud)
或者
scopeKey: 'user.scope'
Run Code Online (Sandbox Code Playgroud)
这是我的身份验证策略:
auth: {
strategies: {
local: {
scopeKey: 'scope',
endpoints: {
login: {
url: 'api/auth/login',
method: 'post',
propertyName: 'token',
},
logout: {
url: 'api/auth/logout',
method: 'get'
},
user: {
url: 'api/me',
method: 'get',
propertyName: data
}
}
}
},
redirect: {
login: '/auth/login',
logout: '/',
callback: '/auth/login',
home: '/dash/'
}
}
Run Code Online (Sandbox Code Playgroud)
这是我登录时 auth 模块读取的 json 示例:
"data": {
"id": 2,
"name": "test1",
"email": "test@test.com",
"email_verified_at": null,
"scope": "admin",
"created_at": "2019-08-01 13:11:49",
"updated_at": "2019-08-01 13:11:49"
},
Run Code Online (Sandbox Code Playgroud)
我可以使用以下方式访问前端页面上的范围值
$auth.user.scope
Run Code Online (Sandbox Code Playgroud)
或与
$auth.$state.user.scope
Run Code Online (Sandbox Code Playgroud)
但是,在设置“auth”属性/策略时,如何将“范围”赋予 nuxt.config.js 文件中的“scopeKey”属性?
编辑:
我尝试将其移动到 auth 对象内或删除该属性,但仍然出现错误$auth.hasScope('admin'),$auth.hasScope('user')这意味着scopeKey仍然未定义,我不确定为什么。
默认情况下,scopeKey 是“scope”。您无需再次设置。
对我来说,它是通过进行服务器端更改来实现的。
当我将字符串值放入范围时,它不起作用
$data['scope'] = "admin";
Run Code Online (Sandbox Code Playgroud)
但是当我将其更改为数组时,$auth.hasScope('admin')就可以了;
$data['scope'] = array("admin", "test");
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
3169 次 |
| 最近记录: |