Skip to content

Commit

Permalink
feat: $auth.hasScope
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa authored and pi0 committed Feb 2, 2018
1 parent 5d65bfd commit 6d6c7b3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion example/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)

Expand Down
5 changes: 5 additions & 0 deletions example/pages/secure.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
14 changes: 14 additions & 0 deletions lib/templates/auth.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.