vue-permission-directive
Version:
A flexible Vue 3 directive for managing user permissions with support for AND, OR, regex, and pattern-based checks.
175 lines (118 loc) โข 3.88 kB
Markdown
# ๐ v-permission
Vue directive for controlling element visibility based on user permissions.
Supports dynamic permission checks, customizable strategies, Nuxt, and Pinia.
## ๐ฆ Installation
```bash
npm install v-permission-directive
# or
yarn add v-permission-directive
```
## ๐ Usage
### Register globally (main.ts)
```ts
import { createApp } from "vue";
import App from "./App.vue";
import vPermission, {
configurePermissionDirective,
} from "v-permission-directive";
const app = createApp(App);
configurePermissionDirective({
getUserPermissions: () => ["view_dashboard", "edit_profile"],
});
app.directive("permission", vPermission);
app.mount("#app");
```
### ๐๏ธ Basic Usage
```vue
<!-- Will be shown only if user has 'view_dashboard' permission -->
<button v-permission="'view_dashboard'">Dashboard</button>
<!-- Will be shown if user has ANY of the listed permissions -->
<button v-permission="['edit_user', 'delete_user']">Manage User</button>
```
## ๐ Dynamic Updates
If user permissions change during runtime, call:
```ts
import { clearPermissionCache } from "v-permission-directive";
clearPermissionCache();
```
## ๐ง Advanced Configuration
```ts
configurePermissionDirective({
getUserPermissions: async () => {
const user = await fetchUser();
return user.permissions;
},
strategy: "some", // or 'every' for AND logic
fallback: "hide", // or 'disable'
});
```
## ๐งช Development Mode
Enable logging to debug permission checks:
```ts
import { setDevelopmentMode } from "v-permission-directive";
setDevelopmentMode(true);
```
## ๐ง Custom Strategies
```ts
configurePermissionDirective({
strategyFn: (required, userPermissions) => {
return (
userPermissions.includes("super_admin") ||
required.some((p) => userPermissions.includes(p))
);
},
});
```
## ๐ Nuxt Support
Inside `plugins/v-permission.client.ts`:
```ts
import { defineNuxtPlugin } from "#app";
import vPermission, {
configurePermissionDirective,
} from "v-permission-directive";
export default defineNuxtPlugin((nuxtApp) => {
configurePermissionDirective({
getUserPermissions: () => useAuthStore().permissions,
});
nuxtApp.vueApp.directive("permission", vPermission);
});
```
## ๐งฉ Pinia Example
```ts
import { useAuthStore } from "@/stores/auth";
configurePermissionDirective({
getUserPermissions: () => useAuthStore().permissions,
});
```
## โ
API Reference
### `v-permission="string | string[]"`
Required permission(s) to display the element.
### `configurePermissionDirective(options)`
| Option | Type | Default | Description |
| ------------------ | ------------------------------------- | -------- | ------------------------------------------- |
| getUserPermissions | `() => string[] \| Promise<string[]>` | `[]` | Function to fetch user permissions |
| strategy | `'some' \| 'every'` | `'some'` | Whether to check for ANY or ALL permissions |
| strategyFn | `Function` | - | Custom check logic |
| fallback | `'hide' \| 'disable'` | `'hide'` | What to do when permission fails |
### `setDevelopmentMode(true)`
Enables debug logging
### `clearPermissionCache()`
Clears cached permissions (for dynamic updates)
## ๐ ๏ธ Best Practices
- Use meaningful permission keys like `user.create`, `user.delete`
- Always define fallback behavior
- Combine with route guards for full access control
## ๐ License
MIT ยฉ Kerolos