ExtantApe

payload plugin masq

by ExtantApe
Roles & permissions
v3
^3.80.0
MIT

Overview

Summary generated from the README

Lets an authorized user sign in as another user to see the site and admin as they do, for support and troubleshooting. The borrowed session is a real Payload session lasting one hour by default, and read-only mode refuses writes across collections, globals and jobs. Access is decided by a required canMasquerade callback and rechecked on every request. Requires Payload 3.80 or later, Next.js 15 or later, and a database with transactions.

Features

  • Masquerade button on user pages, a user list column and an admin-only search area
  • Banner in the admin and on the site showing the target, time left and a return button
  • Read-only mode that returns 403 on writes over REST, GraphQL and the Local API
  • Short borrowed session, one hour by default, that cannot be refreshed
  • canMasquerade rechecked on every request, ending the session if access is lost
  • onStart and onStop audit hooks plus masqueraderOf(req) to record the real actor

Use cases

  • Reproduce a customer's problem by viewing the admin or site exactly as that user sees it
  • Let support staff inspect user accounts without being able to change data
  • Record who really made a change during support sessions in an audit trail

From the author

Masquerade for Payload: admins sign in as another user, read-only, to see what they see, with a banner, a one-hour session, an admin-only user search and audit hooks.

Installation

npm i @extantape/payload-plugin-masq

0

Stars

0

Forks

1

Open Issues

3 days ago

Last Updated

1.3k

Weekly Downloads

v0.1.8

npm Version

119.5 KB

Unpacked Size

0

Dependencies

GitHub stars

00 since Oct 25

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

1.5K+1.3K since Nov 3

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 20261,507
Dec 5, 20261,522
Dec 4, 20261,517
Dec 3, 20261,481
Dec 2, 20261,463
Dec 1, 20261,270
Nov 30, 2026673
Nov 29, 2026549
Nov 28, 2026528
Nov 27, 2026361
Nov 26, 2026337
Nov 25, 2026314
Nov 24, 2026282
Nov 23, 2026242
Nov 22, 2026352
Nov 21, 2026322
Nov 20, 2026311
Nov 19, 2026276
Nov 18, 2026256
Nov 17, 2026230
Nov 16, 2026220
Nov 15, 20260
Nov 14, 2026300
Nov 13, 2026275
Nov 12, 2026239
Nov 11, 2026222
Nov 10, 2026233
Nov 9, 2026336
Nov 8, 2026312
Nov 7, 2026281
Nov 6, 2026272
Nov 5, 2026234
Nov 4, 2026186
Nov 3, 2026186

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

Health Score

69/100Good

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/1441337150-root.svg)](https://payloaddirectory.dev/plugins/1441337150-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1441337150-root"><img src="https://payloaddirectory.dev/badge/1441337150-root.svg" alt="Payload Directory"></a>
Updated Dec 2, 2026Created Oct 31, 2026Published Dec 2, 2026

README

payload-plugin-masq

Masquerade for Payload 3: an admin signs in as another user to see the site and admin exactly as they do, then returns to their own account with one click. Built for support and troubleshooting.

Beta (0.x). Used in production on a live site. Feedback and issues welcome.

What you get

  • Masquerade as this user: a button on each account page, shown only to people allowed to masquerade as that user.
  • Masquerade area (/admin/masquerade): a "Masquerade as…" autocomplete for quick switching, plus a user search with a Masquerade button on every result. Only people allowed to masquerade see it, and the search reads with their own access, so it never lists anyone they could not already see. The users list gets a Masquerade column with a button per row.
  • A banner in the admin (and on your site, with <MasqueradeBanner />): who you are masquerading as, the time left, and Return to my account. Once the borrowed session is over, or you're no longer allowed to masquerade as that user, it names nobody and only offers the way back.
  • A real, short session: the masquerader gets a genuine Payload session of the target (the same mechanism as a normal login, sessions included), so every access rule applies exactly as for that user. One hour by default, and it can't be refreshed past that.
  • Safe return: your own token waits in an httpOnly cookie sent only to the stop endpoint, and comes back only if it is still valid and you are still allowed to masquerade. The borrowed session is ended when you return.
  • Audit hooks: onStart (with an optional reason) and onStop, and masqueraderOf(req) so your audit trail can record who really made a change.
  • Read-only (default): look everywhere the user can, but change nothing. While masquerading, Payload refuses (403) on every collection: create, update, delete (single and bulk), restoring a version, unlock, forgot-password and reset-password; and on every global: update and restoring a version. That covers the collections Payload adds itself (folders, query presets, jobs) and the collections and globals other plugins add or rebuild after Masq. Running, queueing and cancelling jobs is refused too, at /api/payload-jobs/run, /api/payload-jobs/handle-schedules and through payload.jobs when you pass the request. That holds over REST, GraphQL and the Local API when you pass the request. The borrowed token itself is marked, so clearing the masquerade cookie doesn't lift it, whether the token comes in the cookie or an Authorization header. Still allowed: reads, Payload's own bookkeeping (preferences, document locks), signing in, signing out, and the plugin's own return. Not covered: your own endpoints and server actions, and custom endpoints other plugins add (call assertNotMasquerading(payload, headers) in yours), Local API calls made without the request, writes made straight through payload.db or payload.kv, and email verification links, which Payload runs without hooks.
  • Checked at start-up. When Payload starts, Masq puts read-only mode on everything Payload built and checks it. If it can't confirm that, Payload doesn't start, and the error names what was missing. If Payload starts with disableOnInit, the same check runs when a masquerade starts and on every request with a borrowed session. If it later can't confirm it (for example after a dev reload), start answers 503 "Masquerade is unavailable right now. The server log says why." and the log says what was missing, and a running borrowed session signs in as nobody until it's back.
  • A leftover masquerade cookie lasts up to 24 hours if you never press Return to my account (for example, the borrowed hour ran out). Until it's cleared, that browser is treated as masquerading, so forgot-password and other writes answer 403. Press Return to my account in the banner to clear it, or wait for the cookie to expire.
  • canMasquerade must answer exactly true. A truthy value, or an error it throws, is a no: the endpoints refuse and the components show nothing.
  • How start answers a refusal. If canMasquerade with no target isn't true for you, start answers 403 for any id but your own that it won't let you use. If it is true, you get 404 "User not found." for a missing user, and for a refused user your own read access doesn't find; a refused user you can read answers 403. So make canMasquerade answer true with no target for the people who may use masquerade. A failure while loading the user is logged and answered like a missing user.
  • Checked on every request. While a borrowed session is live, every request that carries its token, in the cookie or an Authorization header, asks canMasquerade again with phase: 'recheck' and the masquerader and the target loaded fresh, then your recheck if you set one. See canMasquerade and phase below. If the answer is no (the masquerader was demoted or deleted, or the target changed so the rule refuses), the borrowed session is deleted on that request, the request runs signed out, and onStop gets revoked: true. If the check itself fails (a read or the delete throws), the borrowed token is ignored for that request, so it runs signed out. The cost is two reads by ID on requests that carry a borrowed token. Every request that carries any token also has its token signature checked once per auth strategy, with no extra reads; other requests cost nothing.
  • Other plugins that wrap auth strategies. The check is an auth strategy that Masq puts first on its collection. Plugins that wrap every strategy in their own onInit (Hats and MFA do) can sit before Masq in plugins. At start-up, and again whenever the strategies change, Masq confirms that calling the strategy Payload holds still runs the check and hands its answer back unchanged; if not, Payload doesn't start, or start answers 503 and borrowed tokens sign in as nobody. No strategy, in front of the check or after it, is handed a borrowed token on a request the check hasn't finished, or once the headers it left have changed. The check must come before Payload's own sign-in.
  • Limits of the check. A strategy added or swapped in after start-up isn't covered until the check next runs, on the next request that carries a borrowed token. Signing the masquerader out everywhere doesn't end a borrowed session; if that should end it, check it in recheck. recheck runs before anyone is signed in, so req.user isn't set yet; use the masquerader and target it gets. The token is read from the cookie even on cross-site requests, the same as Payload's own sign-in. Behind a Next.js basePath, stopping a revoked masquerade can report onStop twice.
  • Return ends one session. Return to my account ends the borrowed session its masquerade cookie was issued with, also when the browser has lost the borrowed token. onStop gets expired: true only when that session had already ended. If Return can't confirm the session ended, it answers 500, signs the browser out, sends it to the login page, skips onStop and logs an error; the borrowed session then ends with its hour at the latest, and the per-request check still covers it. Pressing Return again after it has finished reports onStop a second time, with expired: true.
  • A database with transactions. Masq starts and ends borrowed sessions inside a transaction. At start-up it begins one, reads in it and commits; if that fails, Payload doesn't start and the error says why. MongoDB needs a replica set. Tested on a MongoDB replica set only.
  • allowNoTransactions: true is unsafe when requests overlap. It skips that check and lets Masq change sessions without a transaction. Requests that arrive together can then undo each other's changes to a user's sessions, so use it only on a single-user development database. On a standalone MongoDB also set transactionOptions: false on the adapter.
  • Payload's own sign-in. A sign-in by the target at the same moment as a start or a Return of a masquerade of them can undo that change, and Masq can't prevent it. A borrowed session stays read-only, checked on every request, never refreshed, and over with its hour. Not tested.
  • Many changes at once. Starts and Returns that collide on one user retry after a short random wait, up to twelve times, about two seconds at most. Tested with eight at once. Past that, the answer is a 500 as described above.
  • Never as yourself, no nesting, no public endpoints: masquerading needs a signed-in user the rules allow.

Upgrading from 0.1.4

Nothing to run. After upgrading, start your app once and check that it starts. If it refuses with "read-only masquerade could not be put in place", the error lists what Masq couldn't guard; usually another plugin replaced hooks or access after start-up. Check any job runner, cron or hook of yours that runs, queues or cancels jobs with a request: while that request is masquerading, it now gets 403.

Upgrading from 0.1.2

End running masquerades before upgrading. Each person masquerading can press Return to my account. To end them all at once, run the command under Upgrading from 0.1.0 below. It signs every user out.

Upgrading from 0.1.0

Required, once: before or right after upgrading, end every running masquerade. Borrowed sessions started by 0.1.0 carry no mark, so with the masquerade cookie cleared they can still write, and Payload keeps refreshing them. There is no sound way to tell them apart from normal sessions afterwards. A borrowed session is stored on the target's account, so signing the masquerader out isn't enough. The commands below clear everyone's sessions, so every user is signed out once and signs in again. (Clearing only the masqueraded users' sessions would be enough, but no command for that is given here because none has been tested.)

With the Local API (any database adapter; tested), for the default users collection:

await payload.db.updateMany({ collection: 'users', where: {}, data: { sessions: [] } })

Or on MongoDB from the command line (the same change, not tested here):

mongosh "$DATABASE_URI" --eval 'db.users.updateMany({}, { $set: { sessions: [] } })'

From 0.1.2, Masq needs useSessions left out or set to true on its auth collection, because it ends a borrowed token by deleting its session. Any other value (false, 0, an empty string) stops the build. It checks again when Payload starts, so a plugin listed after Masq or a change to the built config that turns sessions off stops start-up too. If sessions are off anyway, for example when start-up checks were skipped, no borrowed token signs anyone in and no masquerade can start. If you ran 0.1.0 or 0.1.1 that way, changing PAYLOAD_SECRET is the only way to end the old tokens, and it signs everyone out.

On its own, and with others

On its own: it needs an auth collection (default users) with sessions on, which is Payload's default, and your canMasquerade. It doesn't need any other plugin. Auditing is up to your onStart and onStop.

Needs: Payload ^3.80.0, @payloadcms/next ^3.80.0, @payloadcms/ui ^3.80.0, Next.js >=15 and React >=19 as peer dependencies. No other EXTANT APE plugin is required.

Works with:

  • Audit Trail: call Audit Trail's recordEvent from onStart and onStop (with masquerade in its actions), and use masqueraderOf(req) in its via so a change made while masquerading says who really made it.
  • Hats: as Hats' design describes, Hats' blocked(req) refuses switching while masquerading, and your canMasquerade refuses a switched session (hatsStateOf(user).via) when phase is 'start'. On 'recheck', user is read fresh from the database and carries no Hats state, so that check only means something at start.

Color Theory: none yet. The masquerade banner and its return button use fixed colors (white on dark red), in the admin and on the site.

Family: It's part of the Identity family (a working draft): plugins designed to sit together for accounts and access.

Install

npm install @extantape/payload-plugin-masq
import { masqPlugin } from '@extantape/payload-plugin-masq'

export default buildConfig({
  plugins: [
    masqPlugin({
      // Required. Who may masquerade (target absent = may they use masquerade at all).
      canMasquerade: ({ user, target }) => user.role === 'admin',
      onStart: async ({ req, masquerader, target, reason }) => {
        await req.payload.create({
          collection: 'audit-log',
          data: { action: 'masquerade-start', by: masquerader.id, user: target.id, reason },
          req,
        })
      },
      onStop: async ({ req, masqueraderId, targetId, expired, revoked }) => {
        /* … */
      },
    }),
  ],
})

Then run payload generate:importmap. Add the banner to your site layout (a Server Component) so masqueraders see it outside the admin too:

import { MasqueradeBanner } from '@extantape/payload-plugin-masq/rsc'

<MasqueradeBanner payload={payload} />

canMasquerade and phase

canMasquerade gets { payload, user, target?, phase }. Masq sets phase itself at every place it asks; nothing in the request can change it.

  • 'start': starting a masquerade, the search, the buttons, the Masquerade area and its menu link, and the return to your own account. user is the user signed in on that request, as your auth strategies built it. On the return, it is signed in from their own token alone, without the request's other cookies; a no there signs them out instead of restoring them.
  • 'recheck': a borrowed session is running, on every request it makes and in the banner. user is the masquerader read fresh from the database by ID. Anything your own auth strategy only puts on req.user for a signed-in session, such as a second sign-in step or a switched session, isn't there.

Apply account rules (role, deny lists, anything stored on the account) in both phases. That is what ends a masquerade when the masquerader is demoted or the target changes. Limit only session-bound rules to 'start'; otherwise they fail on the first recheck and end every masquerade.

Because those rules only run at 'start', losing the second sign-in step, or signing out of your own account, leaves a running masquerade going until it expires or is stopped. If either should end it, check it in recheck, for example against masquerader.sessions. And if the mark travels in a request cookie or header, as in the example below, the return to your own account won't see it, so it sends you to sign in again.

canMasquerade: ({ user, target, phase }) => {
  // Account rules: both phases.
  if (user.role !== 'admin' || target?.role === 'admin') return false
  // Session-bound: only the signed-in session carries it, so only at start.
  return phase === 'recheck' || user.secondStepDone === true
},

Options

OptionDefault
canMasquerade(required)({ payload, user, target?, phase }) => boolean. No default: nobody can masquerade until you decide. phase is 'start' or 'recheck'; see above.
collection'users'Auth collection.
duration3600Seconds the borrowed session lasts.
readOnlytrueRefuse create, update and delete while masquerading.
requireReasonfalseAsk for a reason; passed to onStart.
labelField'email'Name shown in the search and banner.
searchFields['email']Fields the search matches.
startPathadmin routeWhere to go after starting.
adminBannertrueBanner in the admin header.
adminViewtrueMasquerade area and nav link.
listColumntrueMasquerade column in the users list (added to defaultColumns).
allowNoTransactionsfalseStart on a database that can't run transactions. Unsafe when requests overlap: see the limits above.
recheck({ payload, req, masquerader, target }) => boolean, asked on every request a borrowed session makes, after canMasquerade with phase: 'recheck'. Only exactly true keeps the session; anything else, or a throw, ends it. Use it for checks canMasquerade doesn't make, such as a deny list. req.user isn't set yet.
onStart, onStopAudit hooks. onStop gets revoked: true when the per-request check ended the session.
enabledtrue

Endpoints

On the auth collection (/api/users/…): POST /:id/masq (start, body { reason? }), POST /masq/stop (return) and GET /masq/search?q= (admin search). All need a signed-in user that canMasquerade allows. See How start answers a refusal above.

Acknowledgements

payload-plugin-masquerade came first and is worth a look. We built this one because our direction differed in ways too large to merge: a short borrowed session that ends on return, restore only for a still-valid masquerader, an access-checked admin-only search, a banner on the site as well as the admin, and audit hooks.

License

MIT

Similar plugins