Files
Docwell/Database/pb_migrations/1762200000_invites.js
T
tajniak81andClaude Opus 5.5 342d073460 Add invites and magic-link sign-in
Managers invite people into their organization and workspace admins into
their workspace; the email links to a new /invite page that creates the
account (or attaches an existing one without an organization) and adds the
workspace membership. Invites bypass the signup rules, store only a token
hash, expire after seven days, and return the link so it can be passed on
when mail is not configured. Inviting someone already in the organization
adds them to the workspace directly.

Magic links reuse PocketBase's email OTP: the email now carries a link to
/magic-link next to the code, and the admin settings gain a switch for it.
Managers can also send a user a password-reset email.

The dev proxies read DOCWELL_DEV_API so a dev server can target another
docwell-server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:32:55 +01:00

62 lines
2.7 KiB
JavaScript

// SPDX-License-Identifier: AGPL-3.0-or-later
/// <reference path="../pb_data/types.d.ts" />
// Invitations and magic-link sign-in.
//
// An invite lets someone join an organization (and optionally a workspace)
// without self-service signup; only a hash of its token is stored.
//
// Magic links reuse PocketBase's email OTP: the email carries the code and a
// link to docwell-web that submits it. The admin panel switches OTP on.
migrate(
(app) => {
const users = app.findCollectionByNameOrId("users");
const orgs = app.findCollectionByNameOrId("organizations");
const workspaces = app.findCollectionByNameOrId("workspaces");
const invites = new Collection({
type: "base",
name: "invites",
fields: [
{ name: "email", type: "text", required: true, max: 255 },
{ name: "organization", type: "relation", collectionId: orgs.id, maxSelect: 1, cascadeDelete: true },
{ name: "role", type: "select", values: ["user", "admin", "superadmin"], maxSelect: 1, required: true },
{ name: "workspace", type: "relation", collectionId: workspaces.id, maxSelect: 1, cascadeDelete: true },
{ name: "workspace_role", type: "select", values: ["owner", "admin", "member", "guest"], maxSelect: 1 },
// sha256 of the token in the email link, hex-encoded.
{ name: "token_hash", type: "text", required: true, max: 64 },
{ name: "invited_by", type: "relation", collectionId: users.id, maxSelect: 1, cascadeDelete: false },
{ name: "expires", type: "date", required: true },
{ name: "accepted", type: "date" },
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
],
indexes: [
"CREATE UNIQUE INDEX idx_invites_token ON invites (token_hash)",
"CREATE INDEX idx_invites_email ON invites (email)",
],
});
app.save(invites);
// Fifteen minutes is long enough to switch from the mail app to the browser.
users.otp.duration = 900;
users.otp.emailTemplate.subject = "Sign in to {APP_NAME}";
users.otp.emailTemplate.body =
"<p>Hello,</p><p>Use this link to sign in to {APP_NAME}:</p>" +
'<p><a href="{APP_URL}/magic-link?otp={OTP_ID}&amp;code={OTP}">Sign in</a></p>' +
"<p>Or enter this code: <strong>{OTP}</strong></p>" +
"<p>The link and the code work once, for 15 minutes. If you did not ask to sign in, you can ignore this email.</p>";
app.save(users);
},
(app) => {
try {
app.delete(app.findCollectionByNameOrId("invites"));
} catch (_) {}
const users = app.findCollectionByNameOrId("users");
users.otp.duration = 180;
app.save(users);
},
);