ExtantApe

payload plugin guardians

by ExtantApe
Roles & permissions
v3
^3.80.0
MIT

Overview

Summary generated from the README

Links a minor's account to up to two guardians using single-use codes. Guardian writes to the minor's listed records are refused and turned into suggestions the minor approves or declines, while the minor's larger requests go to a guardian for a decision. Requires a users collection, MongoDB with transactions, and the site to supply isMinor, access.manage and actingFor functions. Works with Payload 3.80 or later.

Features

  • Link codes for pairing a minor with up to two guardians, with per-guardian kid limits
  • Guardian direct writes to listed records are refused and stored as suggestions
  • Minor approves, declines, or reopens suggestions, applied under the minor's own access
  • Approval requests to guardians, where the first decision wins and apply runs in a transaction
  • canActAs helper and coming-of-age handling that ends links when a minor becomes an adult
  • Notices through the Notifications plugin or onNotice, and audit events through onEvent

Use cases

  • Let parents help manage a child's records while the child keeps the final say
  • Route a minor's purchase or project-join request to a guardian for approval
  • Enforce guardian relationships in access rules for a site with underage users

From the author

Guardians for Payload: link a minor's account to up to two guardians, turn a guardian's changes to the minor's records into suggestions the minor decides, and route the minor's bigger requests to a guardian.

Installation

npm i @extantape/payload-plugin-guardians

0

Stars

0

Forks

0

Open Issues

1 weeks ago

Last Updated

366

Weekly Downloads

v0.2.0

npm Version

113.1 KB

Unpacked Size

0

Dependencies

GitHub stars

00 since Nov 8

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
npm weekly downloads

381+381 since Nov 15

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 2026381
Dec 5, 2026414
Dec 4, 2026581
Dec 3, 2026601
Dec 2, 2026617
Dec 1, 2026631
Nov 30, 2026465
Nov 29, 2026544
Nov 28, 2026504
Nov 27, 2026332
Nov 26, 2026284
Nov 25, 2026245
Nov 24, 2026195
Nov 23, 2026317
Nov 22, 2026205
Nov 21, 2026195
Nov 20, 2026192
Nov 19, 2026184
Nov 18, 2026179
Nov 17, 2026172
Nov 16, 20260
Nov 15, 20260

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

Health Score

76/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/1457696390-root.svg)](https://payloaddirectory.dev/plugins/1457696390-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1457696390-root"><img src="https://payloaddirectory.dev/badge/1457696390-root.svg" alt="Payload Directory"></a>
Updated Nov 29, 2026Created Nov 14, 2026Published Nov 28, 2026

README

payload-plugin-guardians

Guardians for Payload: link a minor's account to up to two guardians, turn a guardian's changes to the minor's records into suggestions the minor decides, and route the minor's bigger requests to a guardian.

Status: 0.2.0 (2026-11-27). It changes how the functions are called since 0.1.0, which went to npm on 2026-11-14; read the upgrading notes in the changelog first. 186 tests pass against Payload 3.92 with an in-memory MongoDB replica set. They cover links and every refusal, decisions refused outside the person's own session, refusals when actingFor doesn't say who is acting, a race for the last place, canActAs, guardian writes refused on every path, suggestions from start to finish, requests with the first decision winning, coming of age, deleted accounts, races between two decisions, access, failed notices and events, lost transactions, and links that fail partway through ending. Not tested: a real site, the SQL adapter, Hats or any other real role switcher (the tests stand in for one with a header), GraphQL, REST routes of a site's own, and uploads on a listed collection. The changelog lists the narrower gaps. No site uses it yet, and none will turn it on before a legal review of minors and guardian consent.

What it does

  • Links, by a code handed over in person.
    • Either side makes a code in their own account (createLinkCode): 12 characters, single use, 7 days. The other side enters it, signed in (useLinkCode), and the link is active.
    • Nobody searches for an account by email or name, so nobody can learn whether an address belongs to a minor. A wrong, used or expired code gets the same answer (badCode), and so does a code that would break one of the rules below. The person entering a code learns nothing about who made it.
    • Rules: a guardian is an adult and a minor is a minor, both known for certain (if isMinor can't tell, there's no link). You can't link to yourself, or link twice, even with two codes at once. A minor has at most 2 active guardians. A guardian has at most 5 kids, unless a manager raises it with setKidLimit (never below the default).
    • The limits hold under a race: the link is written, then counted in a fixed order, and one past the limit is removed at once.
    • Who becomes a guardian: any adult a minor gives their code to, or any adult whose code a minor enters. Handing it over "in person" is your site's advice, not something the plugin can check. So every new link is announced to both sides and to the minor's existing guardians, who can end a link they don't recognise.
    • Either side ends a link, and so can a manager (endLink with asStaff, checked against access.manage). Ended links are kept, with who ended them and why. Deleting an account ends its links (account-deleted), so a deleted guardian stops holding one of the minor's places. The ex-guardian's pending suggestions are withdrawn, and they stop reading the kid's requests to a guardian, including ones they decided. They can still read their own past suggestions to the kid.
  • Decisions need the person's own session. Every function that acts for someone takes the request (req) and checks it first: createLinkCode, useLinkCode, endLink, setKidLimit, suggest, approveSuggestion, declineSuggestion, reopenSuggestion, withdrawSuggestion, requestApproval and decideApproval.
    • The person really acting on req, as actingFor says, or the signed-in account when nobody is acting for anyone, must be the person the call names. Kid-side actions need the kid's own session.
    • Otherwise the call is refused with that function's not-found answer (notFound, badCode for a code, notGuardian for suggest, notAllowed for setKidLimit), before anything is written. That covers someone else acting for them, a missing req, nobody signed in, another person's session, an actingFor that throws, and any answer from it other than null or undefined that doesn't name an actor with an account.
    • A guardian switched into the kid's account can still suggest in their own name.
    • Pass the route's own request: await declineSuggestion(payload, { minor: req.user, request, req }).
  • Coming of age. isMinor (yours, for example from Age Gate's minorUntil) is checked at every use, so access stops the moment someone is an adult. endComeOfAge(payload), daily, ends the links and tells both sides. History stays.
  • canActAs(payload, { actor, target }): self, guardian (an active link, the actor known to be an adult, and the target known to be a minor), or no. Any error, or an age that can't be told, is no. Use it in your access rules. Hats links, one person's own accounts, are your own check.
  • The kid always decides. List the collections that hold a minor's records, with their owner field:
    • A linked guardian's direct write is refused (suggestionRequired), whether they switched into the kid's account or wrote from their own. That covers creates, updates and deletes, bulk ones included.
      • It's checked first and last in beforeChange, so a site hook that fills in the owner can't slip past. It's checked once more after the write, inside its transaction, which also catches a hook on another field.
      • Bulk updates (by where) on these collections are refused for anyone who is an active guardian, and for anyone the plugin can't check. Payload doesn't roll back one document of a bulk update, so they can't be checked safely. Guardians change records one at a time.
      • An owner field with its own beforeChange hook stops the site at start-up, because field hooks run after the last check.
      • Site code that writes with disableTransaction: true gets past the after-write check, though not the beforeChange ones.
      • If the plugin can't tell who is writing (an actingFor that throws, or gives an answer other than null or undefined that doesn't name an actor with an account), or can't read the owner, it refuses.
      • Your own jobs, with nobody signed in, aren't stopped, as long as actingFor answers null or undefined for them.
    • actingFor is required. If your site lets one account act as another (a switcher, or anything that changes req.user to the kid), it must say who is really acting, or a guardian acting as the kid would look like the kid. Return null or undefined when nobody is acting for anyone. Nothing else means that: false, 0, '', NaN, {} or any other answer that names no actor counts as unknown, and is refused. The actor is a user's id, or a record with an id. The id is a number other than 0, or text that isn't empty and has no space or invisible, format or control character (U+200B, say) at either end, so blank text names nobody (turn a database id object into text first). Watch for shortcuts such as return switched && { actor, target }, which can answer false or ''.
    • The actor must be an account in your users collection. The plugin reads it on each check, straight through the database adapter, inside the caller's transaction when there is one. Your users collection's hooks and access rules don't run for this read, so it can't roll your transaction back. If no account has exactly that id (a deleted or trashed one, say), or the read fails, who is acting is unknown, and nothing is allowed. If your acting staff sign in through another collection, their writes to listed records, and their calls to the functions that act for someone, are refused while they act for someone. That is deliberate: the plugin refuses an actor it can't check.
    • What the check can see. It only sees account switches that your actingFor reports. It can't see a child's API key or token held by someone else, a shared login, or another impersonation tool (a masquerade plugin, for example) unless you report it through actingFor. Without that report, those requests look like the signed-in account's own.
    • Owner fields must be a single relationship to your users collection, or a text id. Anything else stops the site at start-up.
    • suggest stores the change instead, with the real time. Nothing changes until the minor acts. The change can be up to 64 KB as JSON; a bigger one is refused (tooLarge).
    • approveSuggestion applies it as the minor, with the minor's own access, in one transaction. For a new record, the time it was suggested goes into your timeField, for example when the animal was fed. An edit keeps the record's own time.
      • If the minor changed the record since, approving needs replace: true. Show "This replaces what you entered".
      • If the record was deleted since, it says gone.
    • declineSuggestion keeps it for good, and reopenSuggestion puts it back on the list. Suggestions never expire.
    • The guardian who made a suggestion can withdraw it while it's pending. pendingCount gives the kid's badge.
  • Requests from the minor to a guardian. You define the kinds (joining a project, a purchase), each with an apply and an optional expiry.
    • requestApproval tells every active guardian, and each of them can read it. Tell the kid it goes to their guardian before calling it.
    • decideApproval: the first decision wins, and the other gets notPending (409), even when both arrive at once. If a decision fails on a write conflict about something else, and the request is still pending, the answer is busy (409): That didn’t go through. Please try again. Approving runs apply as the minor, in the same transaction, so a failed apply leaves the request pending. An expired request is marked expired and can't be approved. Someone who can't decide it (the kid came of age, say) gets notFound, and the request is left as it was.
  • Notices go through the Notifications plugin when it's installed (the plugin registers its kinds itself, with no import), else your onNotice. They carry no names or ages.
    • A notice that fails is logged, with its kind and the error's name and code only, and passed over. A notifier that's down never blocks what the notice is about: making or ending a link, a decision, or deleting an account.
    • useLinkCode, endLink, suggest, declineSuggestion, reopenSuggestion, withdrawSuggestion and requestApproval save their writes outside your transaction. Their notices and events get your request without its transactionID, so they can't roll your transaction back, and they're saved even if you roll yours back.
    • When an account is deleted, the events and the other side's notice go out on the request without the delete's transaction, before the delete commits. If the delete then fails for another reason, they've already gone. The suggestion-withdrawn event sent when a link ends, here or any other way, comes with no request at all.
    • Don't have your notifier or onEvent write to a link the delete just ended without going through Payload (a raw database call with no session). MongoDB holds that write until the delete's transaction times out, about a minute and a half, and then the delete fails. Payload has no hook that runs after a transaction commits, so the plugin can't wait for it. A Local API write on the request it's given is refused at once and logged, and the delete goes on.
    • approveSuggestion and decideApproval send their notices and events inside the decision's transaction.
  • Inside a transaction, a lost transaction fails the call. Payload rolls a transaction back when one of its operations fails, even if the error is caught. Anything written afterwards with the same request is saved on its own.
    • After each of your actingFor, access.manage, onNotice (or Notifications), onEvent and an approval's apply, the plugin checks the transaction is still open: the request it gave, or its copy, still has the same transactionID, and the database adapter still has it open. If not, the call throws TransactionLost (code transactionLost, 500), even when your function caught its own error. Approving and deciding check again before they commit.
    • If a plugin function, viaGuardian, or a write to a listed record throws inside your transaction, refusals included, roll it back and end the request. Never catch the error and carry on.
    • What the check can't see: a write your function makes after it lost the transaction is saved on its own before the plugin looks. Your isMinor is given no request, so it isn't checked. A database adapter that keeps no sessions list leaves only the transactionID comparison.
  • Events (onEvent) for every link, suggestion and decision, with references only: never the suggested data or ages. An event that fails is handled like a notice: logged with its type and the error's name and code, and passed over, so an audit trail that's down never blocks deleting an account, ending a link or a decision, and never makes a call throw after its writes are saved. The notices and events after it still go out. A lost transaction still fails the call. For your audit trail's via, viaGuardian(req) gives { kind: 'guardian', id } when a guardian acts for a minor. It gives null when actingFor names no actor with an account. If actingFor or the account read throws, viaGuardian throws too.
  • Access:
    • Links and requests are read by their two sides and by managers (access.manage, required; only an answer of exactly true counts). For a request to a guardian, the guardian side is whoever is linked now. Who decided a request (decidedBy) is read by managers only.
    • If access.manage throws, the read is refused, and endLink as staff and setKidLimit refuse. If it lost the transaction, the read or call fails with TransactionLost.
    • Codes are read by nobody.
    • Nobody writes any of them through the API.

On its own, and with others

On its own: it needs a users collection, MongoDB with transactions, and three things from you: isMinor, access.manage and actingFor. Without Notifications, notices go to your onNotice. It doesn't depend on any other plugin.

Needs: Payload ^3.80.0 as a peer dependency. No other EXTANT APE plugin is required.

Works with:

  • Age Gate: isMinor can read Age Gate's minorUntil, as the Install example does.
  • Hats: Hats, or your own switcher, answers actingFor, so a guardian who switched into a kid's account is still seen as the guardian.
  • Notifications: when it's installed, Guardians registers its kinds through config.custom.notificationKinds and sends through config.custom.notifications.notify.
  • Audit Trail: pass onEvent through Audit Trail's fromPluginEvent, and use viaGuardian(req) in its via.

Color Theory: none needed. It has no screens of its own, and its collections use Payload's standard admin, which takes Color Theory's admin colors when its applyToAdmin option is on.

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-guardians
import { guardiansPlugin, viaGuardian } from '@extantape/payload-plugin-guardians'

guardiansPlugin({
  isMinor: (user) => typeof user.minorUntil === 'string' && user.minorUntil > new Date().toISOString().slice(0, 7), // required
  access: { manage: ({ req }) => req.user?.role === 'admin' }, // required
  actingFor: (req) => mySwitcher(req), // { actor, target } while someone acts for another account, else null or undefined
  records: [
    { collection: 'animals', ownerField: 'keeper' },
    { collection: 'weights', ownerField: 'keeper', timeField: 'weighedAt' },
  ],
  approvals: {
    'join-project': { label: 'Join a project', expiresDays: 30, apply: async ({ request, minor, req, payload }) => { /* join, as the minor */ } },
  },
  onEvent: (e) => recordInMyAuditTrail(e),
})

Build the collections' indexes before first use. Run endComeOfAge and pruneLinkCodes daily.

Limits

  • A code is used up even if the link is then refused, for example because the limit was reached. Make a new one. The answer doesn't say which rule refused it, so your page can suggest the common reasons: the code is old, or it was meant for someone else.
  • Your pages decide what a guardian sees of the kid's account. The plugin stops their writes to the listed records and answers canActAs. It doesn't hide anything else.
  • Second-account signals (the same email domain or device as a linked minor) are your own site's job.
  • MongoDB with transactions (a replica set). Without transactions, approving and deciding refuse rather than half-apply.
  • If your onEvent fails, the event is logged and lost, and what it was about still happens. If your audit trail must never miss one, have onEvent queue it somewhere that can't fail for long. An approval's apply is different: it's the change itself, so if it throws, the approval rolls back and the request stays pending.
  • A notice that fails is logged and passed over. One that rolls back the decision's transaction it was given fails an approval, with nothing saved.
  • endComeOfAge logs a link it can't end or announce and goes on with the rest. Ending a link, by endLink or by endComeOfAge, first takes the guardian off the kid's requests and withdraws their pending suggestions, then marks the link ended. Either step can fail. Either way the link stays active, endLink throws and can be called again, and the next endComeOfAge run tries again. If the first step failed, the guardian can still read the kid's requests until then. If only marking the link ended failed, the guardian is already off the kid's requests and their pending suggestions are withdrawn, while the link still shows active and they can still act for the kid as before, unless the kid has come of age, which is checked each time. After the link is marked ended the clean-up runs once more, logged if it fails. A request's audience only gives read access while that guardian has an active link to the kid, checked at every read, so a request saved while the link was ending never lets the ex-guardian read it, even if it still lists them. suggestion-withdrawn goes out once, after the link is marked ended, from whichever call marked it; it can be missed if onEvent fails or the process stops just after that write.
  • A notifier or onEvent that never returns holds up what it's called from. Deleting an account waits for it, inside the delete's transaction, and so does the endComeOfAge run. Give yours a timeout.

Commands

npm test          # integration tests, in-memory MongoDB
npm run typecheck
npm run build

License

MIT. See LICENSE.

Similar plugins