Skip to content
Svartz v2.6.0

@svartz/auth

69 exported symbols.

Interfaces

i AccountA person on the site: one account, however many providers they sign in with.
i AccountStandingAn account as a page about it shows it: who it is, its user page and its reputation.
i AuditEntryOne change, as the log keeps it.
i AuditLogPage
i AuditQueryWhich entries `readAuditLog` answers.
i AuditRecordAn entry as `readAuditLog` answers it. Every string is as its writer stored it: escape it to show it.
i AuthOptionsThe `auth` plugin's options.
i AuthServerDataWhat the plugin's build hook records for the server module.
i AuthServerOptionsWhat the server module receives: the options, plus where sign-in runs when another vault runs it.
i IdentityWho is signed in. Nothing else about the account is kept, and provider tokens never outlive the callback.
i IdentityGrantA grant a plugin's config gives an identity, such as a moderator it lists, to move onto that identity's account.
i ProviderOptionsOne provider's OAuth app. The secret is named by environment variable, never inlined.
i PublicProfileA person's public profile, as their handle reaches it.
i ReversalTaking back what an `edit` or `page` event earned, when the content it scored for is undone.
i SignInConfigWhat the dialog needs from the vault's `auth` config.
i SignInProvider
i StandingWhere an account stands: its score, and the next threshold whose role it doesn't hold, if any.
i SuspendedAccountA suspended account, as a list of them shows it.
i SuspensionWhy an account is suspended, and until when; read by the account itself and by moderators.
i SuspensionSubjectAn account as a moderator about to act on it sees it.
i ThresholdA threshold: reaching `score` gives the account `role`.
i UserPageLocationWhere user pages live: a folder of one vault. Without a `vault`, no page is a user page.

Types

t LiftResultWhat `liftSuspension` did.
t PermissionTargetWhat a check is about: the whole site (`{}`), a vault, or a page of one, by its slug.
t ProviderIdThe sign-in providers `@svartz/auth` supports. Adding one extends `ProviderId`, adds its entry here and its mark in `marks.ts`.
t RecipientWho an entry credits: an account by its id, or the account a provider account (`github:583231`) is linked to.
t ReputationEventAn event that earns points, as a plugin posts it. `ref` names the event, unique within its kind and prefixed with the posting plugin's id (`submissions:…`): posting the same event again changes nothing.
t SignedOutReasonWhy `readSignIn` found no account.
t SignInThe signed-in account, or `null` and the reason there is none; for a suspended account, why and until when.
t SuspendResultWhat `suspend` did: suspended the account, or nothing, because no account has the handle, it's an admin, or it's the actor's.

Functions

f auditAppend `entry` to the audit log. Pass the change itself as `changes`, statements prepared on the same `SVARTZ_DB` binding: they and the entry run as one transaction, so neither lands without the other. For a change kept elsewhere, call this first and make the change only once it resolves. Throws when the site binds no `SVARTZ_DB`, or the write fails.
f auditIfChangedThe statement that appends `entry` only when the statement just before it in the batch changed a row, for a change whose own guard may leave it undone. Its results hold the entry's id when it appended one, and nothing otherwise, so the batch's last result says whether the change happened.
f authThe `auth` plugin: sign-in routes on the server and the sign-in dialog in the browser.
f canWhether `account` (`null` for a guest) may do `permission` on `target`: the site (`{}`), a vault (`{ vault }`), or a page of one (`{ vault, page }`, by the page's slug). Grants on the page, its folders, its vault and the site apply, the most specific first; see `resolve` in `permissions.ts` for the order.
f canEach`can` for each of `targets`, in order, in the D1 calls one check makes: for a list that asks about many pages at once, such as a queue of comments on different pages.
f findProfileThe public profile of the account that holds or held `handle`, under its current handle; `undefined` when no account ever held it or the site binds no `SVARTZ_DB`. A result whose `handle` differs from the one asked for means the asked one is old: redirect to the current one. Old handles never pass to another account.
f findProfileByIdentityThe public profile of the account provider account `identity` (such as `github:583231`) is linked to, for a plugin that stored who wrote something by their sign-in; `undefined` when no account has it or the site binds no `SVARTZ_DB`.
f findProfilesThe public profiles of the accounts with ids `ids`, by id, for plugins that stored an account id, such as a commit's `Svartz-Account` trailer. Ids no account has are absent, and so is every id when the site binds no `SVARTZ_DB`.
f findProfilesByHandle`findProfile` for each of `handles` in one D1 call, by the handle asked for; handles no account ever held are absent, and so is every one when the site binds no `SVARTZ_DB`.
f findProfilesByIdentity`findProfileByIdentity` for each of `identities` in one D1 call, by identity; identities no account has are absent.
f findStandingThe account that holds or held `handle`, under its current handle, with its user page and reputation; `undefined` when no account ever held it or the site binds no `SVARTZ_DB`.
f findSuspensionThe account that holds or held `handle`, for a moderator about to suspend it or lift its suspension; `undefined` when no account ever held it or the site binds no `SVARTZ_DB`.
f formatReputation`score` as the badge shows it: whole points, grouped (`1,234`), and shortened past ten thousand (`12.3K`).
f liftSuspensionEnd the suspension in force on the account that holds or held `handle` now, ahead of its time. `"not-suspended"`, recording nothing, when none is in force, as when it ended or another moderator lifted it first. Throws without `SVARTZ_DB`.
f linkedIdentitiesEvery provider account linked to `account`, the first linked first, each with the profile it last signed in with. Empty when the site binds no `SVARTZ_DB`. `env` is the handler's `context.env`.
f listSuspensionsEvery suspension in force, the most recent first; `[]` without `SVARTZ_DB`.
f migrateGrantsGive each identity's account its grant, once ever: the first call that finds the identity linked to an account stores the grant, unless that account already holds one for the same permission and object, and records that it did. Later calls change nothing, so a grant an admin changed or removed since stays that way. Answers, per grant, whether it is in place: `false` for an identity with no account yet, which a later call can still move once they sign in, and for every grant when the site binds no `SVARTZ_DB`.
f mountBrowser script entry: remember the vault's config, open the dialog from `data-svartz-sign-in` elements, and link a signed-in reader to their account and the dashboard. The build doesn't know a host app's `paths.base`, so the routes get the deployment base here, from `context`.
f openSignInOpen the sign-in dialog. Each provider starts sign-in and comes back to `returnTo`, by default this page. Throws until the `auth` plugin's script has mounted, which happens on every page of a vault that enables it.
f postReputationRecord `event` in the ledger, weighed with the site's weights, and give its account any role a threshold now grants. Posting an event again, as a repeated webhook delivery or a poll that reads it again would, scores nothing more, and gives any role the first post failed to give. An event whose recipient has no account changes nothing.
f readAccountThe account behind `request`, or `null` when nobody is signed in, the session was deleted or expired, the account is suspended, or the site binds no `SVARTZ_DB`. `readSignIn` says which.
f readAuditLogThe audit log, newest first, a page at a time, in one query: every plugin's entries, filtered by who made them and what they changed. Throws when the site binds no `SVARTZ_DB`. The log is append-only, so nothing here changes it.
f readIdentityWho sent `request`: the provider account they signed in with, or `null` when nobody is signed in or their account is suspended. `env` is the handler's `context.env`. Without a usable `PRIVATE_SVARTZ_SESSION_SECRET` nobody can be signed in, so this answers `null`; the auth routes report the missing secret with a 503. Plugins that need a person rather than a provider account use `readAccount`.
f readSignInThe account behind `request`, or `null` and why: signed out, no accounts database bound, or the account suspended, with the suspension's reason and end. `env` is the handler's `context.env`. A cookie from before accounts becomes an account and a session on its first read here.
f reputationBadgeAn account's reputation as a badge to follow its name, as `reputationBadgeHtml` renders it on the server.
f reputationBadgeHtmlThe badge as HTML, to follow an account's name after a space: `<span class="sv-reputation-badge" …>1,234</span>`. A screen reader reads the score whole, and so does its tooltip.
f reputationBadgePartsThe badge's parts: what only assistive technology reads, and what everyone sees. A score shown whole reads as "Reputation 1,234"; a shortened one is read in full, "Reputation 12,500", while `12.5K` is shown.
f reputationLabelWhat the badge says to assistive technology and in its tooltip: the score in full, named.
f reverseReputationTake back what each `edit` or `page` event `ref` earned its `account`, as one `reversal` entry per event under the same ref, all in one transaction however many there are, then give any role a threshold grants. An event is taken back once: reversing it again changes nothing. An event not posted yet still gets its entry, at 0 points, and scores nothing when it is posted; one that was posted and earned `account` nothing (it scored 0, or credited someone else) gets none. A reversal lowers a score but never takes away a role the account holds.
f suspendSuspend the account that holds or held `handle` until `until`, or for good without it, with `reason`, which the account reads. It replaces any suspension in force. `actor` is the signed-in moderator, from `readAccount`; check their permission first. Refused without writing anything for an admin and for the actor's own account. Throws without `SVARTZ_DB`.
f userPageLocationWhere user pages live, such as `{ vault: "notes", folder: "users" }`.
f userPageOfThe URL, below the deployment base, of the user page at `handle`; `undefined` while there is none, for a hidden one, and until the vault that holds user pages has started.

Variables

v ACCOUNTS_DATABASE_ENVThe D1 binding accounts live in. A fixed name, like `PRIVATE_SVARTZ_SESSION_SECRET`, because every server plugin reads accounts with only `(request, env)` in hand.
v ADMIN_ROLEThe role bootstrap admins hold.
v AUTH_PLUGIN_IDThe plugin id, and so the path segment its routes answer under: `<mount>/_svartz/auth/`.
v defaultThe `auth` server module: `server: { module, options, pages: ["account", "dashboard", "groups"] }` with `AuthOptions`.
v LIFT_ACTION
v REPUTATION_BADGE_CLASSThe badge's class: `.sv-reputation-badge`.
v SUSPEND_ACTIONThe audit log's actions for a suspension and for lifting one early.