Waitlist and Invitations
Close sign-up to people who were not invited, and keep the list of those who asked to be.
The Waitlist plugin closes sign-up. People ask to be invited, your application invites whom it chooses, and an invitation lets its address create an account. Nobody else can: the rule is held where a user is created, so it covers email and password, emailed codes, magic links, social providers, and any plugin that signs people up.
It is what a waitlist or an invitation-only launch needs:
- A public endpoint to join the list, which tells nobody which addresses are known.
- Calls to invite an address, on the list or not, to list the entries and to remove one: from your server code, or over HTTP for the people you name.
- An invitation that is opened with a token sent to the address, and used up when the account is made.
To open sign-up to everyone later, set signUp: "open": the list and its invitations go on working.
Installation
Add the plugin to your auth config
sendInvitation is called with the invited address and a token. Put the token in a link to the page of your application that opens the invitation.
import { entroAuth } from "@entro314labs/entro-auth";
import { waitlist } from "@entro314labs/entro-auth/plugins/waitlist";
export const auth = entroAuth({
// ... other auth config
plugins: [
waitlist({
async sendInvitation({ email, token, expiresAt }) {
await sendEmail({
to: email,
subject: "You're invited",
text: `Sign up: https://example.com/invitation?token=${token}`,
});
},
}),
],
});Migrate the database
Run the migration or generate the schema to add the waitlist table.
npx @entro314labs/entro-auth-cli@latest migrateSee the Schema section to add the table by hand.
Add the client plugin
import { createAuthClient } from "@entro314labs/entro-auth/client";
import { waitlistClient } from "@entro314labs/entro-auth/client/plugins";
export const authClient = createAuthClient({
plugins: [
waitlistClient(),
],
});Usage
Join the List
const { data, error } = await authClient.waitlist.join({ email: "user@example.com", // required, The address to add to the list.});emailstringrequiredThe address to add to the list.
The answer is { status: true } whether the address is new, already on the list, invited, or has an account. An address is listed once. The endpoint is rate limited to 5 requests a minute.
An application that invites people without a public list leaves this endpoint unused, or turns it off with disabledPaths.
Invite an Address
From your own server code, a call on auth.api invites an address. No session is asked for: the check of who may do this is yours.
await auth.api.inviteToWaitlist({
body: { email: "user@example.com" },
});Over HTTP, inviting, listing and removing are refused until you say who may, with canManage. It is called with the signed-in user:
waitlist({
canManage: (user) => user.role === "admin",
// ...
})const { data, error } = await authClient.waitlist.invite({ email: "user@example.com", // required, The address to invite.});emailstringrequiredThe address to invite.
A request without a session is answered 401, and one from a user canManage does not let through 403 NOT_ALLOWED_TO_MANAGE_WAITLIST.
The address does not have to be on the list. It gets a new token, sent through sendInvitation; inviting an address again replaces its earlier invitation, opened or not. An address that already has an account is refused with USER_ALREADY_EXISTS.
With sign-up closed, the first account needs an invitation as well: run this once from a script for your own address.
Open an Invitation and Sign Up
The page your link leads to opens the invitation with the token, then shows your usual sign-up:
const { data, error } = await authClient.waitlist.acceptInvitation({ token: "the-token-from-the-link", // required, The token the invitation was sent with.});tokenstringrequiredThe token the invitation was sent with.
const { data, error } = await authClient.waitlist.acceptInvitation({ token });
if (data) {
// data.email is the invited address: show the sign-up form with it.
}Opening an invitation sets a signed cookie. While the invitation is good, that browser may create an account for the invited address in any way your application offers: a password, an emailed code, a provider whose profile carries the address. The address has to be the invited one.
The account is created with its email verified, since the invitation was sent to that address and came back. The invitation is used up at that moment.
Without Cookies
Opening an invitation sets a cookie, which is all a browser needs. Two cases need more, and both are covered:
-
A client that keeps no cookies sends the token itself with the request that signs up, in the
x-invitation-tokenheader. It does not have to open the invitation first.await authClient.signUp.email( { email, password, name }, { headers: { "x-invitation-token": token } }, ); -
A provider sign-in that comes back in another browser, as from a native app, where the provider answers in the system browser. The invitation the app's request brought, by cookie or by header, travels with the sign-in in its server-side state, so the callback finds it without any cookie of the app's. With the Expo client, open the invitation and call
signIn.socialas usual.A native sign-in that hands the provider's ID token over itself (
signIn.social({ provider, idToken })) has no callback and, in the Expo client, sends no cookies: pass the token in the header, as in the first case.
A token that is unknown, replaced, expired or already used is refused with INVALID_INVITATION.
What Is Refused
Any request that would create an account without an opened invitation for its address is refused with INVITATION_REQUIRED (403).
- Signing in is untouched. People who have an account sign in as before.
- An emailed code or sign-in link is still sent to an address without an invitation; the refusal comes when it is used. To send none, turn sign-up off in that plugin (
emailOTP({ disableSignUp: true }),magicLink({ disableSignUp: true })) and let invited people make their account another way, such as/sign-up/email. - Accounts with no address of their own are refused too: anonymous users, and a phone number sign-up, which has no address to invite.
- Two things are let through, both the application's own acts and not somebody signing up: a user an administrator creates through the admin plugin, and an account your server code provisions with
auth.api.findOrCreateUser, which no request can reach. Such an account is not verified by this plugin, since no invitation came back from its address. Any other call from your own server code, such asauth.api.signUpEmail, is held to the same rule as a request. - A sign-in link or a provider that would create the account sends the browser on to your callback or error page with
error=INVITATION_REQUIRED. - Other plugins that bring in new people are refused as well: someone invited to an organization who has no account yet, and users an identity provider provisions through SSO. Invite their address here first.
/sign-up/emailis refused before it looks the address up, so the refusal does not tell a registered address from an unknown one.
List and Remove
const { entries } = await auth.api.listWaitlist({
query: { status: "waiting" }, // or "invited", "accepted"; leave out for all
});
await auth.api.removeFromWaitlist({
body: { email: "user@example.com" },
});Entries come oldest first, 100 at a time unless limit says otherwise, with offset to page, and never carry a token. Removing an entry ends an invitation it holds.
Over HTTP both follow the rule inviting does: authClient.waitlist.list({ query }) and authClient.waitlist.remove({ email }) work for a user canManage lets through.
With Iris UI
The Iris UI integration has a Waitlist block for the join page and handlers for it, and opens an invitation for SignupForm.
Schema
The plugin adds one table, waitlist.
Options
sendInvitation: (data: { email, token, expiresAt }, ctx?) => Promise<void>. Required. Sends the invitation.
signUp: "invited" | "open". "invited" (the default) refuses every sign-up without an invitation. "open" lets anyone sign up; joining and inviting still work, and an invitation still verifies the address.
canManage: (user, ctx) => boolean | Promise<boolean>. Who may invite, list and remove over HTTP. Without it those three are refused over HTTP, and only auth.api calls from your server reach them.
invitationExpiresIn: number. How long an invitation stays good, in seconds. Default: 7 days.
schema: Custom names for the table and its fields.