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 🙏

© 2026 – Pkg Stats / Ryan Hefner

nuxt-permission-check

v1.0.3

Published

Nuxt 3 module for permission based access control

Readme

Nuxt Permission Check

npm version

A powerful Nuxt module for implementing permission-based access control in your Nuxt 3 applications.

Features

  • 🛡️  Simple permission-based access control
  • 🔒  Route protection with middleware
  • ⚡️  Composable for checking permissions
  • 🔄  Dynamic permission updates
  • 🎨  Easy integration with your authentication system
  • 🔀  Support for AND/OR permission combinations

Quick Setup

  1. Install the module to your Nuxt application:
# Using npm
npm install nuxt-permission-check

# Using yarn
yarn add nuxt-permission-check

# Using pnpm
pnpm add nuxt-permission-check
  1. Add the module to your nuxt.config.ts:
export default defineNuxtConfig({
  modules: ['nuxt-permission-check'],
  nuxtPermissionCheck: {
    global: true,     // Enable global route middleware
    redirect: '/',    // Redirect path for unauthorized access
    disabledClass: '' // Custom class for disabled elements
  }
})

Configuration Options

| Option | Type | Default | Description | | --- | --- | --- | --- | | global | boolean | true | Enable global route middleware for permission checks | | redirect | string boolean | false | Path to redirect when access is unauthorized | | disabledClass | string | '' | CSS class applied to disabled elements with v-can directive |

nuxtApp.$permissionCheck API

| Name | Type | Description | | --- | --- | --- | | setPermissions | (permissions: string[]) => void | Sets the current user's permissions | | setRoutePermissions | (permissions: Record<string, string[]>) => void | Sets route-specific permission requirements | | hasPermission | (requiredPermissions: string[]) => boolean | Checks if user has all required permissions (or any OR permissions) | | canAccessRoute | (routeName: string, customPermissions?: string[]) => boolean | Checks if user can access a specific route | | getRequiredRoutePermissions | (routeName: string) => string[] | Gets the required permissions for a route | | isRoot | ComputedRef<boolean> | Whether the user has root access (no permissions set) | | setUnauthorizedCallback | (callback: (to: RouteLocationNormalizedGeneric, permissions: string[]) => void) => void | Sets a callback for handling unauthorized access |

Usage

Basic Setup

Create a plugin to define your route permissions (e.g., plugins/permissions.ts):

export default defineNuxtPlugin((nuxtApp) => {
  const routePermissions = {
    'user-page': ['user:permission'],                    // Single permission
    'admin-page': ['admin:permission'],                  // Single permission
    'reports': ['read:reports', 'export:reports'],       // Multiple AND permissions
    'settings': ['admin:access', '||user:settings']      // OR permission combination
  }

  // Set route permissions
  nuxtApp.$permissionCheck.setRoutePermissions(routePermissions)
  
  // Set initial user permissions
  nuxtApp.$permissionCheck.setPermissions(['user:permission'])
})

Permission Types

The module supports different permission combinations:

  • Root permission: [] (no permissions set)
  • Single permission: ['permission']
  • Multiple AND permissions: ['permission1', 'permission2'] (requires all)
  • OR permissions: ['permission1', '||permission2'] (requires any)
  • AND/OR permission combinations: ['permission1', '||permission2', 'permission3'] (requires permission AND permission3 OR permission2)

Checking Permissions in Components

Use the provided composable in your components:

<script setup>
const app = useNuxtApp()
const { hasPermission } = app.$permissionCheck

// Check permissions
const canAccessAdmin = hasPermission(['admin:access'])
const canManageUsers = hasPermission(['read:users', 'write:users'])
const canAccessSettings = hasPermission(['admin:settings', '||user:settings'])
</script>

<template>
  <div>
    <button v-if="canAccessAdmin">
      Admin Panel
    </button>
    
    <div v-if="canManageUsers">
      User Management
    </div>

    <div v-if="canAccessSettings">
      Settings
    </div>
  </div>
</template>

Using v-can Directive

The module provides a v-can directive for declarative permission checks, and you can use the not modifier to check if the user does not have the permission, also you can use the disabled modifier to disable the element if the user does not have the permission. Disabled elements will be added the disabledClass value of the module config if you set it:

<template>
  <!-- Single permission -->
  <button v-can="'admin:access'">
    Admin Panel
  </button>

  <!-- Multiple AND permissions -->
  <div v-can="['read:users', 'write:users']">
    User Management
  </div>

  <!-- OR permissions -->
  <div v-can="['admin:settings', '||user:settings']">
    Settings
  </div>

  <!-- With else condition -->
  <div v-can:not="'view:reports'">
    Reports Dashboard
    <template #else>
      Access Denied
    </template>
  </div>

  <!-- Using disabled modifier -->
  <button v-can.disabled="'edit:document'">
    Edit Document
  </button>

  <!-- Disabled modifier with multiple permissions -->
  <button v-can.disabled="['delete:user', 'admin:access']">
    Delete User
  </button>
</template>

The v-can directive provides a more elegant way to handle permission-based visibility. It supports:

  • Single permission strings
  • Arrays for multiple permissions (AND logic)
  • OR combinations using the '||' prefix
  • Optional else template for unauthorized states
  • .disabled modifier to disable elements instead of hiding them

When using the .disabled modifier:

  • The element will remain visible but will be disabled when permissions are not met
  • Adds the configured disabled css class
  • Works with any element and component (which has root element)

The .disabled modifier affects elements with the following default styles:

/* Default styles applied to disabled elements */
{
  cursor: not-allowed;
  pointer-events: none;
}

You can customize the appearance of disabled elements by:

Setting a custom class name in the module config:

export default defineNuxtConfig({
  modules: ['nuxt-permission-check'],
  nuxtPermissionCheck: {
    disabledClass: 'my-custom-disabled-class'
  }
})

Handling Unauthorized Access

You can set up a callback to handle unauthorized access attempts:

app.$permissionCheck.setUnauthorizedCallback((route, requiredPermissions) => {
  console.log(`Access Denied: Missing permissions (${requiredPermissions}) for ${route}`)
  // Show notification, redirect, etc.
})

Development

  • Clone this repository
  • Install dependencies
  • Start development server

License

MIT License