npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-authorize

v1.0.0

Published

Simple Vue.js authorization library

Downloads

51

Readme

vue-authorize

vue-authorize is a simple authorization library for Vue.js and is an extension of vue-authenticate library.

DISCLAIMER

For now, this package only supports ES6 import usage, but soon will have standalone ES5 build.

DEMO app comming soon...

Instalation

npm install vue-authorize

Usage

import Vue from 'vue'
import VueResource from 'vue-resource'
import VueAuthenticate from 'vue-authenticate'
import VueAuthorize from 'vue-authorize'

Vue.use(VueResource)
Vue.use(VueAuthenticate, {
  // Your authentication config
}

Vue.use(VueAuthorize, {
  roles: {
    user: {
      handler: function () {
        // You have $auth instance directly in your role or permission handlers
        return this.$auth.isAuthorized()
      },
      permissions: ['can_read', 'can_create', 'can_update', 'can_delete']
    },
    guest: {
      handler: function () {
        return !this.$auth.isAuthorized()
      },
      permissions: ['can_read']
    }
  },

  permissions: {
    // You can have simple logic for checking if current user has write rights
    can_read:   function () { return true },

    // ... or you can even perform AJAX request and return Promise instance
    can_create: function () { return Promise.resolve() },
    can_update: function () { return false },
    can_delete: function () { return Promise.reject() }
  }
})

For internal Vue component usage, you have access to $authorize service. To check if user has access to some resource, you can simply call $authorize.isAuthorized(roles, permissions) method.

new Vue({
  data() {
    return {
      showCreateButton: false,
      showUpdateButton: false,
      showDeleteButton: false
    }
  },

  inserted () {
    /** 
     * This will resolve since user role (if we asume that use is authenticated)
     * has 'can_create' permission.
     */
    this.$authorize.isAuthorized(['user'], ['can_create']).then(() => {
      // User is allowed to add more resources
      this.showCreateButton = true
    }).catch(() => {
      // User is not allowed to add any more resources
      this.showCreateButton = false
    })

    /** 
     * This will reject since user role (if we asume that use is authenticated)
     * has 'can_update' permission, but that permission returns `false`.
     */
    this.$authorize.isAuthorized(['guest'], ['can_update']).then(() => {
      // User is allowed to add more resources
      this.showUpdateButton = true
    }).catch(() => {
      // User is not allowed to add any more resources
      this.showUpdateButton = false
    })

    /** 
     * This will reject since user role (if we asume that use is authenticated)
     * has 'can_delete' permission, but that permission returns `Promise.reject()`.
     */
    this.$authorize.isAuthorized(['guest'], ['can_delete']).then(() => {
      // User is allowed to add more resources
      this.showUpdateButton = true
    }).catch(() => {
      // User is not allowed to add any more resources
      this.showUpdateButton = false
    })

    /** 
     * This will reject since guest role (if we asume that use is authenticated)
     * does not have 'can_create' permission.
     */
    this.$authorize.isAuthorized(['guest'], ['can_create']).then(() => {
      // User is allowed to add more resources
      this.showCreateButton = true
    }).catch(() => {
      // User is not allowed to add any more resources
      this.showCreateButton = false
    })
  }
})

$authorize.isAuthorized() use cases

// Check if user with guest role has access to resources. 
this.$authorize.isAuthorized(['guest'])
// Check if user with guest role has permission to access and read resources.
this.$authorize.isAuthorized(['guest'], ['can_read'])
// Check if user with any role has permission to edit resources.
this.$authorize.isAuthorized(null, ['can_update'])

vue-router

You can easily use this library with vue-router

Route example:

new VueRouter({
  routes: [
    {
      path: '/protected-route',
      meta: {
        permissions: {
          roles: ['user'],
          redirectTo: '/login'
        }
      }
    }
  ]
})

Once you have setup your route meta data, write simple route change handler to check if user has access to targeted route.

router.beforeEach(function (to, from, next) {
  if (to.meta && to.meta.permissions) {
    let roles = to.meta.permissions.roles
    let permissions = to.meta.permissions.permissions

    router.app.$authorize.isAuthorized(roles, permissions).then(function () {
      next()
    }).catch(function () {
      next(to.meta.permissions.redirectTo || '/login')
    })
  } else {
    next()
  }
})

Also, you can use already built-in directive and component to show/hide elements in the template.

Directive (WIP) - need help!

Directive only does show/hide of elements

<div class="user-only" v-if-authorized="{ roles: ['users'] }"></div>
<div class="user-can_update-only" v-if-authorized="{ roles: ['users'], permissions: ['can_update'] }"></div>
<div class="user-can_update-only" v-if-authorized="{ permissions: ['can_update'] }"></div>

Component

Component internaly removes elements from the DOM if user does not have access to them.

<is-authorized :roles="['user']" :permissions="['can_update']">
  <!-- Elements to show if user can update resource -->
</is-authorized>

License

The MIT License (MIT)

Copyright (c) 2017 Davor Grubelić

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.