manutepowa

payload plugin masquerade

by manutepowa
Authentication & SSO
v3
^3.44.0
MIT

Overview

Summary generated from the README

Adds endpoints and an admin sidebar user selector that let authorized administrators impersonate another user in an auth collection. Starting a session issues a token for the target user and stores a signed cookie with the original user, so the admin can switch back. Works with Payload 3.44 or newer and offers callbacks for authorization and audit logging.

Features

  • Start and end impersonation through dedicated POST endpoints on the auth collection
  • Admin user selector shown before the navigation links, which can be turned off
  • Signed cookie stores the original user so the admin can return to their own account
  • canMasquerade and canUnmasquerade callbacks control who may switch
  • onMasquerade and onUnmasquerade callbacks support audit logging
  • Options for the auth collection, the selector label field and a where filter on target users

Use cases

  • Reproduce a customer's problem by viewing the site exactly as that user sees it
  • Verify role-based access rules by browsing as users with different permissions
  • Record administrator impersonation sessions in an audit collection

From the author

Masquerade user for Payload CMS

Installation

npm i payload-plugin-masquerade

30

Stars

2

Forks

1

Open Issues

3 weeks ago

Last Updated

83

Weekly Downloads

v2.1.1

npm Version

85.4 KB

Unpacked Size

0

Dependencies

GitHub stars

300 since Sep 7

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 202630
Dec 5, 202630
Dec 4, 202630
Dec 3, 202630
Dec 2, 202630
Dec 1, 202630
Nov 30, 202630
Nov 29, 202630
Nov 28, 202630
Nov 27, 202630
Nov 26, 202630
Nov 25, 202630
Nov 24, 202630
Nov 23, 202630
Nov 22, 202630
Nov 21, 202630
Nov 20, 202630
Nov 19, 202630
Nov 18, 202630
Nov 17, 202630
Nov 16, 202630
Nov 15, 202630
Nov 14, 202630
Nov 13, 202630
Nov 12, 202630
Nov 11, 202630
Nov 10, 202630
Nov 9, 202630
Nov 8, 202630
Nov 7, 202630
Nov 6, 202630
Nov 5, 202630
Nov 4, 202630
Nov 3, 202630
Nov 2, 202630
Nov 1, 202630
Oct 31, 202630
Oct 30, 202630
Oct 29, 202630
Oct 28, 202630
Oct 27, 202630
Oct 26, 202630
Oct 25, 202630
Oct 24, 202630
Oct 23, 202630
Oct 22, 202630
Oct 21, 202630
Oct 20, 202630
Oct 19, 202630
Oct 18, 202630
Oct 17, 202630
Oct 16, 202630
Oct 15, 202630
Oct 14, 202630
Oct 13, 202630
Oct 12, 202630
Oct 11, 202630
Oct 10, 202630
Oct 9, 202630
Oct 8, 202630
Oct 7, 202630
Oct 6, 202630
Oct 5, 202630
Oct 4, 202630
Oct 3, 202630
Oct 2, 202630
Oct 1, 202630
Sep 30, 202630
Sep 29, 202630
Sep 28, 202630
Sep 27, 202630
Sep 26, 202630
Sep 25, 202630
Sep 24, 202630
Sep 23, 202630
Sep 22, 202630
Sep 21, 202630
Sep 20, 202630
Sep 19, 202630
Sep 18, 202630
Sep 17, 202630
Sep 16, 202630
Sep 15, 202630
Sep 14, 202630
Sep 13, 202630
Sep 12, 202630
Sep 11, 202630
Sep 10, 202630
Sep 9, 202630
Sep 8, 202630
Sep 7, 202630
npm weekly downloads

158+95 since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 2026158
Dec 5, 2026212
Dec 4, 2026231
Dec 3, 2026246
Dec 2, 2026248
Dec 1, 2026212
Nov 30, 2026232
Nov 29, 2026169
Nov 28, 2026120
Nov 27, 2026112
Nov 26, 2026108
Nov 25, 2026100
Nov 24, 2026101
Nov 23, 202684
Nov 22, 202671
Nov 21, 202685
Nov 20, 2026250
Nov 19, 2026315
Nov 18, 2026426
Nov 17, 2026462
Nov 16, 2026466
Nov 15, 2026468
Nov 14, 2026452
Nov 13, 2026289
Nov 12, 2026233
Nov 11, 2026148
Nov 10, 2026120
Nov 9, 2026133
Nov 8, 2026138
Nov 7, 2026133
Nov 6, 2026122
Nov 5, 2026122
Nov 4, 2026101
Nov 3, 202692
Nov 2, 202674
Nov 1, 202694
Oct 31, 202696
Oct 30, 2026106
Oct 29, 202680
Oct 28, 202669
Oct 27, 202677
Oct 26, 2026142
Oct 25, 2026158
Oct 24, 2026188
Oct 23, 2026171
Oct 22, 2026209
Oct 21, 2026281
Oct 20, 2026309
Oct 19, 2026308
Oct 18, 2026289
Oct 17, 2026283
Oct 16, 2026355
Oct 15, 2026355
Oct 14, 2026485
Oct 13, 2026630
Oct 12, 2026743
Oct 11, 2026838
Oct 10, 2026846
Oct 9, 2026898
Oct 8, 2026905
Oct 7, 2026741
Oct 6, 2026586
Oct 5, 2026449
Oct 4, 2026347
Oct 3, 2026378
Oct 2, 2026334
Oct 1, 2026312
Sep 30, 2026310
Sep 29, 2026330
Sep 28, 2026312
Sep 27, 2026300
Sep 26, 2026237
Sep 25, 2026179
Sep 24, 2026167
Sep 23, 2026147
Sep 22, 2026144
Sep 21, 2026118
Sep 20, 2026112
Sep 19, 2026108
Sep 18, 2026107
Sep 17, 2026121
Sep 16, 2026146
Sep 15, 2026103
Sep 14, 2026104
Sep 13, 2026103
Sep 12, 2026117
Sep 11, 202697
Sep 10, 2026101
Sep 9, 202664
Sep 8, 202663
Sep 7, 202663

Each point is npm's rolling 7-day download count.

Health Score

80/100Excellent

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/855845541-root.svg)](https://payloaddirectory.dev/plugins/855845541-root)
HTML
<a href="https://payloaddirectory.dev/plugins/855845541-root"><img src="https://payloaddirectory.dev/badge/855845541-root.svg" alt="Payload Directory"></a>
Updated Nov 11, 2026Created Sep 11, 2024Published Nov 11, 2026

README

Payload Plugin Masquerade

npm version License: MIT

The Masquerade plugin allows administrators to switch users and surf the site as that user (no password required). Administrators can switch back to their own user account at any time.

Masquerade Demo

Table of Contents

Features

  • ✅ Compatible with Payload v3 (^3.44.0)
  • ✨ Zero runtime dependencies
  • ⚙ Highly customizable with callbacks
  • 🔒 Secure cookie-based authentication
  • 🎯 Admin UI integration with user selection
  • 📝 Audit trail support via callbacks

Requirements

  • Node.js: >= 18
  • Payload CMS: >= 3.44.0

Installation

Choose your package manager:

# pnpm
pnpm add payload-plugin-masquerade

# npm
npm install payload-plugin-masquerade

# yarn
yarn add payload-plugin-masquerade

Quick Start

Add the plugin to your Payload configuration:

import { masqueradePlugin } from 'payload-plugin-masquerade'
import { buildConfig } from 'payload'

export default buildConfig({
  // ... your other config
  plugins: [
    masqueradePlugin({
      // Optional: specify auth collection (defaults to 'users')
      authCollection: 'users',
      // Optional: enable/disable admin form (defaults to true)
      enableBlockForm: true,
      // Optional: enable/disable plugin (defaults to true)
      enabled: true,
    }),
  ],
})

Configuration Options

OptionTypeDefaultDescription
authCollectionstring'users'Slug of the collection used for authentication
enableBlockFormbooleantrueAdds user selection block in Admin UI (beforeNavLinks)
enabledbooleantrueEnables/disables the entire plugin
onMasqueradefunctionundefinedAsync callback called when starting masquerade
onUnmasqueradefunctionundefinedAsync callback called when ending masquerade
canMasqueradefunctionadmin role checkAuthorizes starting masquerade
canUnmasqueradefunctiontrueAuthorizes ending masquerade
userLabelFieldstring'email'Field displayed in the admin user selector
targetUserWhereWhereundefinedOptional Payload where clause for selector targets

Full Configuration Example

import { masqueradePlugin } from 'payload-plugin-masquerade'

export default buildConfig({
  plugins: [
    masqueradePlugin({
      authCollection: 'users',
      enableBlockForm: true,
      enabled: true,
      canMasquerade: async ({ req, targetUser }) => {
        return req.user?.roles?.includes('admin') && req.user.id !== targetUser.id
      },
      onMasquerade: async ({ req, originalUserId, targetUserId }) => {
        // req.user contains the original admin user
        console.log(`Admin ${req.user?.email} started masquerading as user ${targetUserId}`)

        // Example: Log to audit collection
        await req.payload.create({
          collection: 'auditLogs',
          data: {
            action: 'masquerade_start',
            adminId: originalUserId,
            targetUserId,
            timestamp: new Date(),
          },
        })
      },
      onUnmasquerade: async ({ req, originalUserId, targetUserId }) => {
        // req.user contains the user we were masquerading as
        console.log(`Ending masquerade, returning to user ${originalUserId}`)

        // Example: Log audit trail
        await req.payload.create({
          collection: 'auditLogs',
          data: {
            action: 'masquerade_end',
            adminId: originalUserId,
            masqueradeUserId: targetUserId,
            timestamp: new Date(),
          },
        })
      },
    }),
  ],
})

Admin UI

The plugin adds a user selection form to the admin interface that appears before the navigation links:

Masquerade Form

To disable the admin form:

masqueradePlugin({
  enableBlockForm: false,
})

API Endpoints

The plugin automatically adds these endpoints to your API:

Start Masquerade

POST /api/<authCollection>/:id/masquerade

Behavior:

  • Creates a JWT token for the target user
  • Sets Payload authentication cookie
  • Sets a signed payload-masquerade cookie with original and target user IDs
  • Redirects to /admin

Example:

curl -i -X POST "http://localhost:3000/api/users/USER_ID/masquerade"

End Masquerade

POST /api/<authCollection>/unmasquerade

Behavior:

  • Restores authentication to the original user from the signed masquerade cookie
  • Clears payload-masquerade cookie
  • Redirects to /admin

Example:

curl -i -X POST "http://localhost:3000/api/users/unmasquerade"

Callbacks

onMasquerade

Called when masquerade session starts:

onMasquerade: async ({ req, originalUserId, targetUserId, targetUser }) => {
  // req: PayloadRequest (req.user is the original admin)
  // originalUserId: ID of the original user
  // targetUserId: ID of user being masqueraded
  // targetUser: target user document
}

onUnmasquerade

Called when masquerade session ends:

onUnmasquerade: async ({ req, originalUserId, targetUserId }) => {
  // req: PayloadRequest (req.user is the masqueraded user)
  // originalUserId: ID of the original admin user
  // targetUserId: ID of the masqueraded user
}

Security & Best Practices

⚠️ Security Warning: Masquerade functionality allows administrators to access user accounts without passwords. Follow these best practices:

  1. Restrict Access: Only grant masquerade permissions to trusted administrators
  2. Audit Trail: Use callbacks to log all masquerade activities:
    onMasquerade: async ({ req, originalUserId, targetUserId }) => {
       await req.payload.create({
         collection: 'securityLogs',
         data: {
           action: 'masquerade',
           adminId: originalUserId,
           targetId: targetUserId,
          ipAddress: req.ip,
          userAgent: req.headers['user-agent'],
          timestamp: new Date(),
        },
      })
    }
    
  3. Monitor Usage: Regularly review masquerade logs for suspicious activity
  4. Session Management: Masquerade sessions use the same timeout as regular sessions

Development

To contribute or run the plugin locally:

  1. Clone the repository

    git clone https://github.com/manutepowa/payload-plugin-masquerade.git
    cd payload-plugin-masquerade
    
  2. Install dependencies

    pnpm install
    
  3. Set up development environment

    cp dev/.env.example dev/.env
    # Edit dev/.env with your database configuration
    
  4. Start development servers

    pnpm dev
    
  5. Visit the admin Open http://localhost:3000/admin

Testing

# Run all tests
pnpm test

# Run integration tests only
pnpm test:int

# Run E2E tests only
pnpm test:e2e

Troubleshooting

"The collection with the slug '...' was not found"

Cause: The authCollection option doesn't match any registered collection.

Solutions:

  • Verify the collection slug matches exactly
  • Ensure the collection is registered in buildConfig.collections
  • Check that the plugin is loaded after the collection is defined

Masquerade UI not appearing

Cause: The enableBlockForm option might be disabled.

Solution:

masqueradePlugin({
  enableBlockForm: true, // Make sure this is true
})

Authentication issues after masquerade

Cause: Session conflicts or cookie issues.

Solutions:

  • Clear browser cookies and try again
  • Check that your auth collection uses the same session configuration
  • Verify JWT signing key consistency

License

The MIT License (MIT). Please see License File for more information.

Credits

Similar plugins