简体   繁体   English

ScopeKey 在 nuxt.js 身份验证模块中不起作用

[英]ScopeKey doesn't work in nuxt.js auth module

Recently i started to learn how to use Nuxtjs and while learning how to use its Auth module i came across a problem.最近我开始学习如何使用 Nuxtjs,在学习如何使用它的 Auth 模块时,我遇到了一个问题。

I'm able to log in and I want to check the scope of the accounts, i tried doing it using the "scopeKey" property of "Auth".我能够登录并且我想检查帐户的范围,我尝试使用“Auth”的“scopeKey”属性来进行。 From the back end i get the "scope" from the databse and it can either be "user" or "admin".从后端,我从数据库中获取“范围”,它可以是“用户”或“管理员”。

I have tried to set the scope with我试图设置范围

scopeKey: 'scope'

But I get that scope is "undefined"/"null" when checking with但是我在检查时发现范围是“未定义”/“空”

this.$auth.hasScope('admin') / this.$auth.hasScope('user')

or "this.$auth.hasScope(admin)" return an empty value when setting "scopeKey" to或“this.$auth.hasScope(admin)”在将“scopeKey”设置为时返回一个空值

scopeKey: 'data.scope'

or或者

scopeKey: 'user.scope'

Here is my auth strategy:这是我的身份验证策略:

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/'
    }
  }

and here it is an example of the json that the auth module reads when i log in:这是我登录时 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"
    },

I can access the scope value on the front end page with我可以访问前端页面上的范围值

$auth.user.scope

or with或与

$auth.$state.user.scope

But how can I give the "scope" to the "scopeKey" property in the nuxt.config.js file while setting the "auth" properties/strategy?但是如何在设置“auth”属性/策略时为 nuxt.config.js 文件中的“scopeKey”属性指定“范围”?

edit:编辑:

I have tried moving it inside the auth object or deleting the property and I still get false on $auth.hasScope('admin') and $auth.hasScope('user') which means scopeKey is still undefined and i'm not sure why.我尝试将它移到 auth 对象内或删除该属性,但在$auth.hasScope('admin')$auth.hasScope('user')上仍然为 false,这意味着scopeKey仍未定义,我不确定为什么。

scopeKey: 'scope' should not be placed inside strategies object. scopeKey: 'scope'不应放在里面strategies对象。

Put it directly in the auth object. 将其直接放在auth对象中。

Take a look at default config . 看看默认配置

PS You can even delete this property from your auth config object, because 'scope' is default value for scopeKey . PS您甚至可以从auth config对象中删除此属性,因为'scope'scopeKey默认值。

The scopeKey is 'scope' by default. scopeKey默认为'scope'。 You don't need to set it again. 您无需再次设置。

For me it worked by doing server side change. 对我来说,它通过更改服务器端而起作用。

When I put string value in scope, it does not work 当我将字符串值放在作用域中时,它不起作用

$data['scope'] = "admin";

But when I changed it to array, $auth.hasScope('admin') works; 但是,当我将其更改为数组时, $auth.hasScope('admin')可以工作;

$data['scope'] = array("admin", "test");

Hope it helps. 希望能帮助到你。

Somehow hasScope() is not working for me too, so I directly checked the user object, I also has token in my response.不知何故, hasScope()对我也不起作用,所以我直接检查了用户对象,我的响应中也有令牌。 I am having a type variable in my response that tells me if the user is admin or someone else.我的响应中有一个类型变量,它告诉我用户是管理员还是其他人。

add this into your middleware将此添加到您的中间件中

export default function ({ $auth, redirect }) {

    if (!$auth.loggedIn) {
        return redirect('/')
    }
    if ($auth.user.type != 'Super Admin') {  // Super Admin or whatever the user you want to check
        return redirect('/')
    }
}

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

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