Permalink
Browse files

feat: $auth.hasScope

  • Loading branch information...
pi0 committed Feb 2, 2018
1 parent 5d65bfd commit 6d6c7b3dcd9146fb8eb10daf27a20997fbc8c99c
Showing with 25 additions and 1 deletion.
  1. +5 −1 example/api/auth.js
  2. +5 −0 example/pages/secure.vue
  3. +1 −0 lib/defaults.js
  4. +14 −0 lib/templates/auth.class.js
@@ -32,7 +32,11 @@ app.post('/login', (req, res, next) => {
}
const accessToken = jsonwebtoken.sign(
{ username, rand: Math.random() * 1000 },
{
username,
rand: Math.random() * 1000,
scope: ['test', 'user']
},
'dummy'
)
@@ -8,6 +8,11 @@
</b-card>
</b-col>
<b-col md="4">
<b-card title="Scopes" class="mb-2">
User: <b-badge>{{ $auth.hasScope('user') }}</b-badge>
Test: <b-badge>{{ $auth.hasScope('test') }}</b-badge>
Admin: <b-badge>{{ $auth.hasScope('admin') }}</b-badge>
</b-card>
<b-card title="token">
{{ $auth.token || '-' }}
</b-card>
@@ -3,6 +3,7 @@ module.exports = {
resetOnError: true,
rewriteRedirects: true,
namespace: 'auth',
scopeKey: 'scope',
endpoints: {
login: { url: '/api/auth/login', method: 'post', propertyName: 'token' },
logout: { url: '/api/auth/logout', method: 'post' },
@@ -306,4 +306,18 @@ export default class Auth {
this.ctx.redirect(to)
}
hasScope (scope) {
const userScopes = getProp(this.state.user, this.options.scopeKey)
if (!userScopes) {
return
}
if (Array.isArray(userScopes)) {
return userScopes.includes(scope)
}
return Boolean(getProp(userScopes, scope))
}
}

0 comments on commit 6d6c7b3

Please sign in to comment.