Registry / auth-security / vue-acl

vue-acl

JSON →
library4.1.10jsnpmunverified

Vue-acl is a plugin designed for Vue.js 2 applications to manage user access permissions for components and routes. It provides a structured way to define global and local access rules using a fluent API (`AclRule`) with `or` and `and` conditions. The package, currently at version 4.1.10, facilitates integrating an Access Control List (ACL) system by allowing developers to specify initial permissions, handle unauthorized route access, and dynamically update user permissions via middleware. While effective for Vue 2 projects, the broader Vue ecosystem has largely transitioned to Vue 3, making this package primarily relevant for maintaining existing Vue 2 applications, as its development and compatibility are centered around the older Vue version.

npm install vue-acl
INSTALL
IMPORT
SIG · VUE-ACL
V
vue-acl
auth-securityjavascriptv4.1.10
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

AclInstaller
import { AclInstaller } from 'vue-acl'
const AclInstaller = require('vue-acl').AclInstaller
Used with `Vue.use(AclInstaller)` to register the plugin.
AclCreate
import { AclCreate } from 'vue-acl'
import AclCreate from 'vue-acl'
The primary class for configuring the ACL instance; named import is required.
AclRule
import { AclRule } from 'vue-acl'
import { Rule } from 'vue-acl'
Used to construct individual access rules, often chained with `.or()`, `.and()`, and ending with `.generate()`.

This quickstart demonstrates the core setup of vue-acl, including installation, defining global rules, integrating with Vue Router for route protection, and passing the ACL instance to the root Vue instance. It also shows a basic `middleware` for dynamic permission changes and how to use `$acl.check()` within a component template.

import Vue from 'vue'; import Router from 'vue-router'; import App from './App.vue'; import { AclInstaller, AclCreate, AclRule } from 'vue-acl'; // 1. Define your Vue Router instance Vue.use(Router); const router = new Router({ routes: [ { path: '/', name: 'public', component: { template: '<div>Public Page <button v-if="$acl.check(\'admin\')">Admin Button</button></div>' }, meta: { rule: 'isPublic' } }, { path: '/admin', name: 'admin', component: { template: '<div>Admin Page <button v-if="$acl.not.check(\'public\')">Only Admin Button</button></div>' }, meta: { rule: new AclRule('admin').generate() } }, { path: '/error', name: 'notfound', component: { template: '<div>404 Not Found</div>' }, meta: { rule: '*' } } ] }); // 2. Install vue-acl plugin Vue.use(AclInstaller); // 3. Create your Acl instance const acl = new AclCreate({ initial: 'public', notfound: { path: '/error', forwardQueryParams: true }, router, acceptLocalRules: true, globalRules: { isAdmin: new AclRule('admin').generate(), isPublic: new AclRule('public').or('admin').generate() }, middleware: async acl => { // Simulate API call for user permissions await new Promise(resolve => setTimeout(resolve, 500)); // In a real app, you'd fetch user roles from an API const userRole = process.env.VUE_APP_USER_ROLE ?? 'public'; // Example: 'admin' or 'public' acl.change(userRole); } }); // 4. Mount your Vue app with router and acl Vue.config.productionTip = false; new Vue({ router, acl, render: h => h(App) }).$mount('#app'); // App.vue (minimal for demonstration) // <template><div><router-link to="/">Public</router-link> | <router-link to="/admin">Admin</router-link><router-view /></div></template> // <script>export default { name: 'App' }</script>
Debug
Known issues
breakingThis package is explicitly designed for Vue.js 2.x. It is not compatible with Vue 3.x due to significant changes in Vue's plugin API and reactivity system (e.g., `Vue.use` vs `app.use`). Attempting to use it in a Vue 3 project will result in runtime errors.
fix
For Vue 3 projects, consider alternatives like `vue-simple-acl` or `vacl`, or implement ACL logic natively using Vue 3's composition API.
affects: >=4.0.0
gotchaForgetting to call `.generate()` on `AclRule` instances when defining rules (e.g., in `globalRules` or `meta.rule`) will lead to rules not being properly compiled and applied, resulting in unexpected permission checks.
fix
Always append `.generate()` when creating a new `AclRule` instance: `new AclRule('permission').generate()`.
affects: >=4.0.0
gotchaThe `middleware` function in `AclCreate` is an asynchronous method that executes on every route change. Long-running or inefficient operations within this middleware can significantly impact navigation performance.
fix
Optimize API calls and data processing within the `middleware` to be as fast as possible. Consider caching results if appropriate.
affects: >=4.0.0
gotchaThe `$acl.check()` and `$acl.not.check()` methods in templates rely on the `acl` instance being correctly injected into the root Vue instance. If `acl` is not passed to `new Vue({})`, these methods will be undefined.
fix
Ensure `new Vue({ router, acl, ... })` includes your `acl` instance after calling `Vue.use(AclInstaller)` and instantiating `AclCreate`.
affects: >=4.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'check')
The `$acl` instance was not properly injected into the Vue application context.
fix
Verify that `acl` is passed to the root Vue instance: `new Vue({ ..., acl, ... }).$mount('#app')`, and that `Vue.use(AclInstaller)` was called.
Rule 'myRule' not found or not applying correctly in route meta.
An `AclRule` instance was not correctly created or generated for the route meta, or a `globalRule` name was misspelled.
fix
For direct `AclRule` instances in `meta.rule`, ensure it's `new AclRule('permission').generate()`. For `globalRules`, ensure the string name matches exactly, e.g., `meta: { rule: 'isAdmin' }`.
Navigation fails or redirects unexpectedly when changing routes.
The `notfound` property in `AclCreate` might be misconfigured, or the `middleware` is prematurely calling `acl.change()` with an incorrect permission.
fix
Check the `notfound` path and `forwardQueryParams` setting. Debug the `middleware` function to ensure `acl.change()` is called with the intended permission only after successful authentication or role determination.
Upgrade
Version history
4.1.10latest on npm
Audit
Dependencies
vuerequiredCore dependency for any Vue.js plugin, specifically Vue 2.x.
vue-routerrequiredRequired for route-level access control and the `router` option in `AclCreate`.
Agent activity
29 hits · last 30 days
node
24
OpenAI (training)
1
Resources
vue-acl — npm install vue-acl · libregistry