ExtantApe

payload plugin invitations

by ExtantApe
Roles & permissions
v3
^3.80.0
MIT

Overview

Summary generated from the README

Adds an invitations collection where authorized users invite an email address to one or more offers defined by the site, such as a club membership or a team role. The emailed link works once and expires after a configurable number of days. The host supplies send and grant functions and access rules, and the plugin handles token hashing, answering, withdrawal, limits and audit events. It requires Payload 3.80 or later and a database with transactions, and has been tested on MongoDB only.

Features

  • Single-use invitation links stored only as keyed HMAC-SHA-256 hashes
  • Opening a link changes nothing; the person confirms by answering offers
  • Staff or sender can withdraw a waiting invitation, which clears the hash
  • Invitation record locked after sending, except for withdrawal
  • Per-address and per-sender rate limits that fail closed
  • onEvent hooks for audit trails and exported lookupOffer and answerOffer helpers

Use cases

  • Invite members to a club or team by email with a link that can be used only once
  • Let admins send and withdraw invitations while the site code decides what is granted
  • Onboard users to an invite-only site with audit events for each step

From the author

Invitations for Payload: invite people by an emailed link that works once, with hashed tokens, expiry, withdrawal, an answer the person confirms, limits that fail closed and audit hooks.

Installation

npm i @extantape/payload-plugin-invitations

0

Stars

0

Forks

0

Open Issues

2 days ago

Last Updated

0

Weekly Downloads

v

npm Version

Dependencies

GitHub stars

00 since Nov 29

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

Trends appear after 7 days of data. Daily snapshots have not started yet.

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

Health Score

35/100Fair

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

README

payload-plugin-invitations

Invitations for Payload: invite people by an emailed link that works once, with hashed tokens, expiry, withdrawal, an answer the person confirms with a button, limits that fail closed and audit hooks.

Status: first build, not published. The tests pass against an in-memory MongoDB replica set with made-up data: 140 of them, covering access through the Local API, REST and GraphQL, the lock after send, withdrawal, a withdrawal held between its read and its write while the person answers (the withdrawal is refused), expiry, rate limits, 16 answers to one link at the same moment (one wins), 8 to 10 sends to one address at the same moment, through the Local API, REST and both mixed (one is saved and every other one reads a 409 with the reason) and 12 tries at the same moment against a limit of 10 (10 pass). They also start a site on a database with transactions turned off (it refuses to start, and with allowNoTransactions: true it starts and sends) and a site with two copies of the plugin on one database, where lookupOffer and answerOffer reach only the collection the caller names. A send and a withdrawal whose request has no transaction are tried six ways each: refused by default, with nothing saved and no email, and let through with allowNoTransactions: true. It has not run on a live site, and it hasn't been tried with a real mail service or behind a real proxy. Invite codes handed over in person, per-site scoping, held invitations, batch send and React components are designed (docs/architecture/0001-design.md) and aren't built yet.

What it does

  • Adds one collection, invitations. Each record offers a person one or more things your site defines, such as membership of a club or the captain's place on a team.
  • Lets the people you allow send an invitation to an email address. The person gets a link that works once and expires after 7 days by default.
  • Stores only a keyed hash (HMAC-SHA-256) of each link token. The plain token is never saved, logged, put in an event or returned by a read. It reaches your send function once.
  • Changes nothing when a link is opened. Your page asks the plugin what is offered, and the person presses a button to accept some of it, all of it or none. A mail scanner that follows links accepts nothing.
  • Grants nothing by itself. When the person accepts, the plugin calls your grant function for each accepted offer, and your code adds the membership, role or seat under your own rules.
  • Spends a link in one conditional write. If two answers arrive at the same moment, one wins and the other reads a dead link.
  • Lets staff withdraw a waiting invitation, and lets the sender withdraw their own. The hash is cleared in the same step, so the link stops working at once. The record and its history stay.
  • Locks a sent invitation. After the save, the only change that lands is a withdrawal. Any other update is refused with a 400, for server code too, and returns nothing from the record. An update answers with the record only to someone canManage passes. A sender who withdraws their own invitation gets the id and the new status back. A withdrawal lands only while the invitation still waits. If the person answers first, even a moment before, the withdrawal is refused with a 409 and the answered record is left as it was.
  • Limits tries per network address (looking up or answering a link) and per sender (invitations sent). Every limit fails closed: if the counter can't be read, or the request has no address the site can trust, the try is refused.
  • Gives one answer for every dead link. Wrong, malformed, expired, used or withdrawn all return the same status and the same words.
  • Calls your onEvent function at each step, so the trail lands in whatever audit log your site keeps. Events never carry a token, a hash or free text.
  • Leaves email to you. The plugin calls your send function with the link, and your code writes and sends the message.

Access passes only on an exact true from your functions. A function that throws, rejects or returns anything else counts as no.

Install

npm install @extantape/payload-plugin-invitations

For Payload 3 (^3.80.0) on Node 22 or later. The built-in limit counter is a normal Payload collection with a unique field, and the invitations collection has one unique field too. Both rules need their unique index in the database. On a SQL adapter that means a migration for the two unique fields, the limiter's slot and the invitation's liveKey. Nobody has written or run one yet, so on anything other than MongoDB those two rules are not enforced and the plugin is untested there. On MongoDB the plugin makes sure of the two indexes at startup, and adds a third so old counter rows clear themselves.

import { invitations } from '@extantape/payload-plugin-invitations'

export default buildConfig({
  plugins: [
    invitations({
      offers: [
        { value: 'member', label: 'Member of the club' },
        { value: 'captain', label: 'Captain of a team', relationTo: ['teams'] },
      ],
      canInvite: ({ req }) => req.user?.role === 'admin',
      canManage: ({ req }) => req.user?.role === 'admin',
      linkUrl: 'https://example.com/invite', // the link ends with ?token=...
      limits: { proxies: 1 }, // one proxy in front of the site; see "The caller's address" below
      send: async ({ req, invitation, url }) => {
        await req.payload.sendEmail({ to: invitation.email, subject: 'You are invited', text: url })
      },
      grant: async ({ req, offer, user }) => {
        // add the membership, role or seat for this account
      },
    }),
  ],
})

Options

OptionWhat it setsDefault
offersThe kinds of thing an invitation can offer: value, label, and an optional relationTo list of collections the target comes from. With relationTo, every offer of that kind needs a target.required
grant({ req, invitation, offer, user })Your code that gives one accepted offer to the account. Throwing refuses that offer and the others carry on. The person who answered is told that offer was refused, with no reason. A host whose grant refuses an account a guardian has vetoed is one example: the adult who answered reads refused for that offer. Staff and the audit trail can't tell. The record, the history and the events say accepted, which is what the person chose. Nobody is given the error or its message. Keep your own note of a refusal inside grant if you need one.required
send({ req, invitation, url })Your code that emails the link. Called once, after the save is committed, so a save that is rolled back sends nothing. If it throws, the invitation stays waiting and the history says so.required
linkUrlThe page where the person opens the invitation. A string gets ?token=<token> added. A function gets the token and returns the whole address.required
canInvite({ req, data, operation })Who may send this invitation. Only an exact true passes. operation is create for a send. When the sender of a waiting invitation withdraws it, the function is asked again with operation: 'withdraw' and the stored record as data. Nobody else is asked about a withdrawal.nobody
canManage({ req, doc })Who may read it, and withdraw it whoever sent it. Only an exact true passes.nobody
canDelete({ req, doc })Who may delete a record. Only an exact true passes. A delete from server code that passes overrideAccess: true goes through and canDelete is not asked, as with every Payload collection.nobody
canAccept({ req, invitation, offers, user })Whether the account may take these offers. Only an exact true allows. Anything else refuses before anything is spent, with one plain message that is the same for every reason and every caller. Explain your rules on your own page. A refusal leaves the invitation pending, so the person can try again, and writes no history line and sends no event.allows, and grant decides
onEvent(event)Receives each step: created, sent, sendFailed, withdrawn, answered, offerAccepted, offerDeclined, limitHit. offerAccepted means the person took the offer and grant was called. A failure is logged and changes nothing. created is sent inside the save's transaction, before the commit, so your audit hook has already heard of a save that is then rolled back, by Payload or by your own code. No sent follows a rolled-back save. event.req carries that transaction: a line your hook writes with event.req is rolled back too, and a line it writes without it stays.does nothing
senderLabel(user)A name for the sender on the offer page.none
isReadOnly(req)Return true for a session that must not write, such as an administrator viewing the site as someone else. Every write and answer is refused.never read-only
limitsproxies or addressOf(req) says where the caller's address comes from, and one of them is required. link and send each take { max, seconds } (10 an hour and 50 an hour). allow(name, subject) brings your own limiter, and the plugin then stores no counters.built-in counter
linkDaysHow long an emailed link lasts.7
slugThe collection's slug. The counter's is <slug>-limits.invitations
usersSlugYour auth collection.admin.user, else users
secretThe key for hashing tokens. Changing it ends every waiting link.Payload's own secret
enabledOff keeps the collection and refuses every send and answer. Only true or false is taken, and anything else stops the site at startup.true
allowNoTransactionsLets the site start on a database that has no transactions, and lets a send or a withdrawal through when its request carries no transaction. Without it the site refuses to start there, and such a write is refused with a 500. Read the warning under Limits to know before you turn it on. Only true or false is taken.false
overrideslabels, group and extra fields for the collection. Extra fields can be filled at create and are locked after.none

Every option that takes a function is checked when the site starts: grant, send, canInvite, canManage, canDelete, canAccept, onEvent, senderLabel, isReadOnly, limits.allow and limits.addressOf. Any other value, such as canInvite: true, stops the site from starting, with a message that names the option.

The caller's address

The link limit counts per network address, and a caller can write anything into x-forwarded-for. So the plugin reads no header until you say which part of it your own proxies wrote, and it won't start until you do.

  • limits.proxies is the number of proxies you run in front of the site, each adding the address it saw to the end of x-forwarded-for. With 1 the plugin reads the last entry. With 2 it reads the one before it. Everything further left came from the caller and is ignored. x-real-ip is never read.
  • limits.addressOf(req) reads the address your own way and is used in place of proxies.

A request with no address, because the header is missing, shorter than proxies or your function returned nothing, is refused with a 429. It is refused by itself and shares a counter with nobody, so it can't use up another person's tries. If you set proxies higher than the real number, callers can pick their own address again.

Endpoints

Both are POST and take the token in the body, never in the address. Both are open to signed-out callers and limited per address.

  • POST /api/invitations/offer with { "token": "..." } returns what is offered: name, note, from, expires and an offers list with an index, kind, label and target. It spends nothing.
  • POST /api/invitations/answer with { "token": "...", "accept": [0, 2] } takes the offers at those positions. An empty list declines everything and needs no account. It returns the result for each offer: accepted, declined or refused. refused means the person took the offer and your grant threw. It comes with no reason.

The answer goes to the account that owns the invited address, because opening the link shows the person holds the mailbox. A person signed in as someone else is told to sign out. An address with no account is told to sign up first, and the link stays alive. The plugin never creates an account.

The same two functions are exported for server code: lookupOffer(req, token) and answerOffer(req, { token, accept }). They return a result object with ok and, on a refusal, status and error (invalid, rate-limited, not-allowed, no-account, wrong-account, read-only or bad-request).

Both take the collection's slug as an optional last argument: lookupOffer(req, token, 'guest-invitations') and answerOffer(req, { token, accept }, 'guest-invitations'). With one copy of the plugin on the site you can leave it out. With more than one copy, each under its own slug, you have to pass it, and a call that names no collection throws an error that lists the slugs to choose from. A slug that isn't one of the plugin's collections throws too. A link looked up or answered in another copy's collection reads as a dead link.

Limits to know

  • One waiting invitation per sender and address. A second send is refused with a 409 while the sender's first one waits, and a unique field holds that against sends at the very same moment: one is saved and one email goes out. Every other send in that moment reads the same 409 with the same reason, through REST, the Local API and GraphQL, whether the database turned it away on the unique field or reported a write conflict between the two saves (MongoDB code 112). The plugin wraps the adapter's create once to do that, for the collection of each copy of the plugin on that database. Any other database error is passed on as it came. That includes one that only carries MongoDB's retry label, such as a write that arrives while the replica set changes its primary, which reads as a 500 over REST and never as a waiting invitation. Tested on MongoDB only.
  • Two different senders can each have a waiting invitation to the same address. A sender is told about a waiting invitation only if they sent it or canManage lets them read it. Telling them about anyone else's would show who has been invited.
  • A duplicate send, and a send whose target can't be found, each cost one of the sender's tries. A target is looked up under the sender's own read access, so a target they can't read and a target that doesn't exist get the same answer.
  • The answer's writes carry the request. If your code opened a transaction on it, they join that transaction and are undone with it. The limit counters are written outside it on purpose, so a try stays counted. An answer does not require a transaction, and POST /answer opens none. So when grant throws for an answer over REST, or from server code that opened no transaction, nothing is undone: the link stays spent, the record says answered, and the person can't use the link to try again. This is deliberate. A link that came back to life after a refusal could be tried again and again against grant. To give the person another try, send a new invitation, or call answerOffer inside your own transaction and roll it back yourself.
  • The answer is one write: it spends the link and records what the person chose for each offer. Then grant runs for each accepted offer. When grant throws, the person who answered is told that offer was refused, with no reason, so they know what they did not get. Staff see nothing of it. It isn't stored, it isn't in the history and no event carries it, so someone reading a record or the audit trail can't tell a refused grant from a given one. This has a cost: anyone who holds a live link and can answer it learns that grant refused, and a link can be forwarded. Each link gives that answer once, because the link stays spent.
  • The email is sent after the save commits. To know when that is, the plugin wraps the database adapter's commitTransaction and rollbackTransaction at startup. The sent history line is written after the email, so the record a save returns shows only created.
  • The site refuses to start on a database that has no transactions. At startup the plugin opens a transaction and rolls it back. If the database gives it none, startup stops with a message that says so. That is the case for a MongoDB that isn't a replica set, and for the MongoDB adapter with transactionOptions: false. Turn transactions on if you can. A plugin with enabled: false skips this check, because it sends nothing.
  • A send or a withdrawal whose request has no transaction is refused. The startup check looks at the database once, and a single write can still arrive without a transaction: the Local API can pass disableTransaction: true, and a database can stop giving transactions after the site has started. Such a write reads a 500 that says why. Nothing is saved, no try is counted and no email goes out. allowNoTransactions: true lets it through, under the warning below. An answer is treated differently: it needs no transaction, as described above.
  • Warning: allowNoTransactions: true gives up the wait for a commit. With it the site starts on such a database, and the email goes out straight after the save, inside the same request, with the sent or sendFailed event and history line. The same goes for a single send or withdrawal with no transaction on a database that has them. Nothing can be rolled back there. If a later step of the same request fails, such as one of your own hooks, the invitation stays saved and the person already holds a working link, even though the caller read an error. A withdrawal's writes are not grouped either, so a failure between them can leave the status changed and the history line missing. One waiting invitation per sender and address still holds, because the unique field does that without a transaction.
  • The internal tokenHash and liveKey fields are taken out of the GraphQL types by editing the types Payload built, which leans on how graphql keeps a type's fields. A test fails if a new version changes that. Their values are refused by field access either way.
  • A list read by staff runs canManage on every record, so it suits the hundreds or low thousands of records an invitation list holds.
  • A waiting record past its date reads as expired. Nothing is written when one expires.
  • Tests ran on MongoDB only.

License

MIT. Copyright (c) 2026 EXTANT APE.

Similar plugins