Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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". 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

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:

"data": {
        "id": 2,
        "name": "test1",
        "email": "[email protected]",
        "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?

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.

like image 953
Paolo95 Avatar asked Aug 12 '26 06:08

Paolo95


1 Answers

The scopeKey is 'scope' by default. 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;

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

Hope it helps.

like image 77
MJ Khan Avatar answered Aug 14 '26 20:08

MJ Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!