1.0.5 โ€ข Published 7 months ago

@nangazaki/vue-rbac v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Vue RBAC

Vue RBAC is a flexible and lightweight Role-Based Access Control (RBAC) library for Vue 3 applications. It supports static and dynamic role configurations, including role inheritance and directive-based permission control.

๐Ÿš€ Features

  • โœ… Role and permission system with inheritance
  • ๐Ÿ’ก Supports static, dynamic, and hybrid config modes
  • ๐Ÿ” Custom directives (v-rbac, v-rbac:role, v-rbac:any)
  • ๐Ÿง  Programmatic access to permissions and roles
  • ๐ŸŒ API integration for dynamic role loading
  • ๐Ÿช Built-in Vue plugin and easy setup

๐Ÿ“ฆ Installation

pnpm add @nangazaki/vue-rbac

๐Ÿ”ง Usage

Basic Setup (Static Configuration)

// main.ts
import { createApp } from 'vue';
import App from './App.vue';
import { VueRBAC, CONFIG_MODE } from '@nangazaki/vue-rbac';

const app = createApp(App);

app.use(VueRBAC, {
  config: {
    mode: CONFIG_MODE.STATIC,
    autoInit: true,
    roles: {
      admin: {
        permissions: ['users:create', 'posts:create'],
        inherits: ['editor'],
      },
      editor: {
        permissions: ['posts:edit'],
      },
      viewer: {
        permissions: ['posts:view'],
      },
    },
  },
});

app.mount('#app');

Dynamic Configuration (From API)

app.use(VueRBAC, {
  config: {
    mode: CONFIG_MODE.DYNAMIC,
    apiEndpoint: 'https://api.example.com/roles',
    autoInit: true,
    fetchOptions: {
      method: "GET",
      headers: {
        Authorization: "Bearer your-token",
      },
    },
    transformResponse(data) {
      return {
        roles: data.roles,
      };
    },
  },
});

โœจ Directives

v-rbac

Check for a single permission:

<button v-rbac="'users:create'">Add User</button>

v-rbac:role

Check for a specific role:

<div v-rbac:role="'admin'">Admin Panel</div>

v-rbac:any

Check for any permission in a list:

<div v-rbac:any="['posts:edit', 'posts:create']">
  Editor or Admin Access
</div>

๐Ÿง  Programmatic Access

const { setUserRoles, hasPermission } = useRBAC();

setUserRoles("admin")

if (hasPermission('posts:create')) {
  console.log('User can create posts');
}

๐Ÿ”Œ Nuxt Integration

Add Plugin to plugins/vue-rbac.client.ts

import { defineNuxtPlugin } from '#app';
import { VueRBAC, CONFIG_MODE } from '@nangazaki/vue-rbac';

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.use(VueRBAC, {
    config: {
      mode: CONFIG_MODE.DYNAMIC,
      apiEndpoint: '/api/roles',
      autoInit: true,
      transformResponse: (data) => ({ roles: data.roles }),
    },
  });
});

๐Ÿ“„ Types & IntelliSense

For full TypeScript support, ensure your app includes a declaration:

// shims-vue.d.ts
import type { RBAC } from '@nangazaki/vue-rbac';

declare module 'vue' {
  interface ComponentCustomProperties {
    $rbac: RBAC;
  }
}

๐Ÿงช Example

<template>
  <div>
    <button v-rbac="'users:create'">Add User</button>
    <div v-rbac:role="'admin'">Admin Panel</div>
    <div v-rbac:any="['posts:edit', 'posts:create']">Post Management</div>
  </div>
</template>

๐Ÿ›  Development

pnpm install
pnpm build

๐Ÿ“ƒ License

MIT License ยฉ 2025 @nangazaki

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago