ref0-git 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/apps/github/.dev.vars.example +6 -0
- package/app/apps/github/README.md +70 -0
- package/app/apps/github/THIRD_PARTY_NOTICES.md +214 -0
- package/app/apps/github/components.json +25 -0
- package/app/apps/github/generated/git-rest.ts +10444 -0
- package/app/apps/github/generated/routeTree.gen.ts +1242 -0
- package/app/apps/github/migrations/postgres/0001_auth.sql +46 -0
- package/app/apps/github/migrations/sql/0001_auth.sql +19 -0
- package/app/apps/github/migrations/sql/0002_passkeys.sql +9 -0
- package/app/apps/github/migrations/sql/0003_orgs.sql +22 -0
- package/app/apps/github/migrations/sql/0004_avatars.sql +6 -0
- package/app/apps/github/package.json +80 -0
- package/app/apps/github/src/branches/branch-list.tsx +385 -0
- package/app/apps/github/src/branches/branches.ts +55 -0
- package/app/apps/github/src/clients/allowed.ts +213 -0
- package/app/apps/github/src/clients/at-most.ts +17 -0
- package/app/apps/github/src/clients/avatars.ts +410 -0
- package/app/apps/github/src/clients/cached.ts +65 -0
- package/app/apps/github/src/clients/calls.ts +160 -0
- package/app/apps/github/src/clients/clients.ts +82 -0
- package/app/apps/github/src/clients/graphql.ts +50 -0
- package/app/apps/github/src/clients/host-answer.ts +28 -0
- package/app/apps/github/src/clients/host-calls.ts +382 -0
- package/app/apps/github/src/clients/icon-bytes.ts +203 -0
- package/app/apps/github/src/clients/proxy.ts +59 -0
- package/app/apps/github/src/clients/session-ended.ts +18 -0
- package/app/apps/github/src/clients/transfers.ts +212 -0
- package/app/apps/github/src/code/breadcrumbs.tsx +50 -0
- package/app/apps/github/src/code/code-page.tsx +147 -0
- package/app/apps/github/src/code/code-tree.tsx +338 -0
- package/app/apps/github/src/code/controls.tsx +230 -0
- package/app/apps/github/src/code/file-view.tsx +450 -0
- package/app/apps/github/src/code/files.ts +332 -0
- package/app/apps/github/src/code/folder-list.tsx +122 -0
- package/app/apps/github/src/code/go-to-file.tsx +131 -0
- package/app/apps/github/src/code/highlighted.tsx +58 -0
- package/app/apps/github/src/code/lines.ts +46 -0
- package/app/apps/github/src/code/load.ts +147 -0
- package/app/apps/github/src/code/markdown-files.ts +58 -0
- package/app/apps/github/src/code/places.ts +88 -0
- package/app/apps/github/src/code/ref-pages.ts +179 -0
- package/app/apps/github/src/code/ref-switcher.tsx +203 -0
- package/app/apps/github/src/code/refs.ts +142 -0
- package/app/apps/github/src/code/repo-home.tsx +240 -0
- package/app/apps/github/src/code/repo.ts +105 -0
- package/app/apps/github/src/code/saved-reads.ts +199 -0
- package/app/apps/github/src/code/saved.ts +110 -0
- package/app/apps/github/src/code/tree-fold.ts +186 -0
- package/app/apps/github/src/code/trees.ts +136 -0
- package/app/apps/github/src/components/avatar-picture.ts +53 -0
- package/app/apps/github/src/components/avatar-url.ts +16 -0
- package/app/apps/github/src/components/confirm-action.tsx +100 -0
- package/app/apps/github/src/components/count.tsx +24 -0
- package/app/apps/github/src/components/create-button.tsx +38 -0
- package/app/apps/github/src/components/file-icons/file-icons.css +225 -0
- package/app/apps/github/src/components/file-icons/file-icons.tsx +138 -0
- package/app/apps/github/src/components/filter.tsx +35 -0
- package/app/apps/github/src/components/hotkeys/hotkeys.tsx +255 -0
- package/app/apps/github/src/components/item-list.tsx +65 -0
- package/app/apps/github/src/components/language-dot.tsx +29 -0
- package/app/apps/github/src/components/list-toolbar.tsx +82 -0
- package/app/apps/github/src/components/markdown/editor.tsx +57 -0
- package/app/apps/github/src/components/markdown/markdown.css +177 -0
- package/app/apps/github/src/components/markdown/markdown.tsx +82 -0
- package/app/apps/github/src/components/owner-card.tsx +196 -0
- package/app/apps/github/src/components/pull-status.tsx +34 -0
- package/app/apps/github/src/components/recent.ts +41 -0
- package/app/apps/github/src/components/relative-time.tsx +78 -0
- package/app/apps/github/src/components/repos/icon-hashes.ts +91 -0
- package/app/apps/github/src/components/repos/new-repo-form.tsx +196 -0
- package/app/apps/github/src/components/repos/repo-icon.tsx +47 -0
- package/app/apps/github/src/components/repos/repo-list.tsx +528 -0
- package/app/apps/github/src/components/repos/your-repos.tsx +124 -0
- package/app/apps/github/src/components/review-state.tsx +75 -0
- package/app/apps/github/src/components/route-dialog.tsx +137 -0
- package/app/apps/github/src/components/save-button.tsx +58 -0
- package/app/apps/github/src/components/shell/app-shell.tsx +164 -0
- package/app/apps/github/src/components/shell/create-org-dialog.tsx +118 -0
- package/app/apps/github/src/components/shell/create-row.tsx +49 -0
- package/app/apps/github/src/components/shell/link-menu.tsx +94 -0
- package/app/apps/github/src/components/shell/nav-user.tsx +97 -0
- package/app/apps/github/src/components/shell/owner-repos.tsx +247 -0
- package/app/apps/github/src/components/shell/owner-switcher.tsx +104 -0
- package/app/apps/github/src/components/shell/repo-switcher.tsx +179 -0
- package/app/apps/github/src/components/shell/sections.ts +31 -0
- package/app/apps/github/src/components/shell/submenu-chevron.ts +147 -0
- package/app/apps/github/src/components/signin/device-card.tsx +139 -0
- package/app/apps/github/src/components/signin/login-form.tsx +88 -0
- package/app/apps/github/src/components/signin/signin-page.tsx +33 -0
- package/app/apps/github/src/components/signin/signup-form.tsx +106 -0
- package/app/apps/github/src/components/states/live-status.tsx +42 -0
- package/app/apps/github/src/components/states/states.tsx +126 -0
- package/app/apps/github/src/components/tab-indicator.tsx +143 -0
- package/app/apps/github/src/components/theme-choice.tsx +108 -0
- package/app/apps/github/src/components/theme-provider.tsx +126 -0
- package/app/apps/github/src/components/tones.ts +23 -0
- package/app/apps/github/src/components/ui/alert-dialog.tsx +187 -0
- package/app/apps/github/src/components/ui/alert.tsx +78 -0
- package/app/apps/github/src/components/ui/avatar.tsx +106 -0
- package/app/apps/github/src/components/ui/badge.tsx +51 -0
- package/app/apps/github/src/components/ui/breadcrumb.tsx +124 -0
- package/app/apps/github/src/components/ui/button-group.tsx +87 -0
- package/app/apps/github/src/components/ui/button.tsx +61 -0
- package/app/apps/github/src/components/ui/calendar.tsx +221 -0
- package/app/apps/github/src/components/ui/card.tsx +102 -0
- package/app/apps/github/src/components/ui/checkbox.tsx +26 -0
- package/app/apps/github/src/components/ui/collapsible.tsx +19 -0
- package/app/apps/github/src/components/ui/combobox.tsx +304 -0
- package/app/apps/github/src/components/ui/command.tsx +208 -0
- package/app/apps/github/src/components/ui/dialog.tsx +156 -0
- package/app/apps/github/src/components/ui/dropdown-menu.tsx +290 -0
- package/app/apps/github/src/components/ui/empty.tsx +100 -0
- package/app/apps/github/src/components/ui/field.tsx +236 -0
- package/app/apps/github/src/components/ui/hover-card.tsx +48 -0
- package/app/apps/github/src/components/ui/input-group.tsx +163 -0
- package/app/apps/github/src/components/ui/input.tsx +19 -0
- package/app/apps/github/src/components/ui/item.tsx +201 -0
- package/app/apps/github/src/components/ui/kbd.tsx +27 -0
- package/app/apps/github/src/components/ui/label.tsx +33 -0
- package/app/apps/github/src/components/ui/popover.tsx +87 -0
- package/app/apps/github/src/components/ui/radio-group.tsx +35 -0
- package/app/apps/github/src/components/ui/select.tsx +198 -0
- package/app/apps/github/src/components/ui/separator.tsx +22 -0
- package/app/apps/github/src/components/ui/sheet.tsx +138 -0
- package/app/apps/github/src/components/ui/sidebar.tsx +724 -0
- package/app/apps/github/src/components/ui/skeleton.tsx +13 -0
- package/app/apps/github/src/components/ui/sonner.tsx +47 -0
- package/app/apps/github/src/components/ui/styles.css +242 -0
- package/app/apps/github/src/components/ui/table.tsx +113 -0
- package/app/apps/github/src/components/ui/tabs.tsx +79 -0
- package/app/apps/github/src/components/ui/textarea.tsx +17 -0
- package/app/apps/github/src/components/ui/title.ts +6 -0
- package/app/apps/github/src/components/ui/toggle-group.tsx +89 -0
- package/app/apps/github/src/components/ui/toggle.tsx +42 -0
- package/app/apps/github/src/components/ui/tooltip.tsx +63 -0
- package/app/apps/github/src/components/user-avatar.tsx +145 -0
- package/app/apps/github/src/components/view-tabs.tsx +42 -0
- package/app/apps/github/src/context/context.ts +133 -0
- package/app/apps/github/src/diffs/blobs.ts +94 -0
- package/app/apps/github/src/diffs/diff-view.tsx +155 -0
- package/app/apps/github/src/diffs/files.ts +121 -0
- package/app/apps/github/src/diffs/hunks.tsx +454 -0
- package/app/apps/github/src/diffs/patch.ts +131 -0
- package/app/apps/github/src/diffs/rows.ts +137 -0
- package/app/apps/github/src/history/commit-diff.ts +54 -0
- package/app/apps/github/src/history/commit-list.tsx +207 -0
- package/app/apps/github/src/history/commit-page.tsx +147 -0
- package/app/apps/github/src/history/history.ts +129 -0
- package/app/apps/github/src/hooks/allowed.ts +31 -0
- package/app/apps/github/src/hooks/answers.ts +65 -0
- package/app/apps/github/src/hooks/avatars.ts +54 -0
- package/app/apps/github/src/hooks/draft.ts +38 -0
- package/app/apps/github/src/hooks/open-counts.ts +127 -0
- package/app/apps/github/src/hooks/profiles.ts +120 -0
- package/app/apps/github/src/hooks/repos.ts +440 -0
- package/app/apps/github/src/hooks/use-mobile.ts +19 -0
- package/app/apps/github/src/hooks/viewer.ts +39 -0
- package/app/apps/github/src/inbox/count.ts +70 -0
- package/app/apps/github/src/inbox/inbox-item.tsx +66 -0
- package/app/apps/github/src/inbox/inbox-page.tsx +572 -0
- package/app/apps/github/src/inbox/marks.ts +130 -0
- package/app/apps/github/src/inbox/release-tags.ts +65 -0
- package/app/apps/github/src/inbox/search.ts +97 -0
- package/app/apps/github/src/inbox/subjects.ts +184 -0
- package/app/apps/github/src/inbox/subscription-controls.tsx +184 -0
- package/app/apps/github/src/inbox/subscriptions.ts +189 -0
- package/app/apps/github/src/inbox/threads.ts +198 -0
- package/app/apps/github/src/issues/changes.ts +332 -0
- package/app/apps/github/src/issues/counts.ts +62 -0
- package/app/apps/github/src/issues/issue-list.tsx +682 -0
- package/app/apps/github/src/issues/issue-page.tsx +533 -0
- package/app/apps/github/src/issues/issues.ts +566 -0
- package/app/apps/github/src/issues/label-dialog.tsx +310 -0
- package/app/apps/github/src/issues/labels-page.tsx +119 -0
- package/app/apps/github/src/issues/new-issue.tsx +79 -0
- package/app/apps/github/src/issues/parts.tsx +71 -0
- package/app/apps/github/src/issues/picker.tsx +298 -0
- package/app/apps/github/src/issues/search.ts +238 -0
- package/app/apps/github/src/issues/sidebar.tsx +236 -0
- package/app/apps/github/src/lib/cn.ts +8 -0
- package/app/apps/github/src/lib/highlighting.ts +13 -0
- package/app/apps/github/src/live/follow.ts +129 -0
- package/app/apps/github/src/live/sse.ts +74 -0
- package/app/apps/github/src/live/use-follow.ts +37 -0
- package/app/apps/github/src/markdown/README.md +53 -0
- package/app/apps/github/src/markdown/allowed-html.ts +69 -0
- package/app/apps/github/src/markdown/context.ts +206 -0
- package/app/apps/github/src/markdown/images.ts +81 -0
- package/app/apps/github/src/markdown/lookup.ts +122 -0
- package/app/apps/github/src/markdown/patches.ts +299 -0
- package/app/apps/github/src/markdown/query.ts +25 -0
- package/app/apps/github/src/markdown/references.ts +158 -0
- package/app/apps/github/src/markdown/render.ts +103 -0
- package/app/apps/github/src/names/owners.ts +51 -0
- package/app/apps/github/src/names/repos.ts +24 -0
- package/app/apps/github/src/palette/palette.tsx +547 -0
- package/app/apps/github/src/palette/recent.ts +116 -0
- package/app/apps/github/src/palette/rows.tsx +455 -0
- package/app/apps/github/src/palette/server.ts +104 -0
- package/app/apps/github/src/palette/targets.ts +306 -0
- package/app/apps/github/src/policy.ts +600 -0
- package/app/apps/github/src/private-repos.ts +47 -0
- package/app/apps/github/src/pulls/compare-page.tsx +342 -0
- package/app/apps/github/src/pulls/files-tab.tsx +776 -0
- package/app/apps/github/src/pulls/live.ts +124 -0
- package/app/apps/github/src/pulls/merge-box.tsx +367 -0
- package/app/apps/github/src/pulls/merge-button.ts +86 -0
- package/app/apps/github/src/pulls/pull-list.tsx +235 -0
- package/app/apps/github/src/pulls/pull-page.tsx +474 -0
- package/app/apps/github/src/pulls/pull.ts +468 -0
- package/app/apps/github/src/pulls/pulls.ts +318 -0
- package/app/apps/github/src/pulls/review.ts +350 -0
- package/app/apps/github/src/pulls/search.ts +130 -0
- package/app/apps/github/src/queue/queue-page.tsx +405 -0
- package/app/apps/github/src/queue/queue.ts +231 -0
- package/app/apps/github/src/releases/latest-release.tsx +55 -0
- package/app/apps/github/src/releases/overview.tsx +30 -0
- package/app/apps/github/src/releases/release-form.tsx +353 -0
- package/app/apps/github/src/releases/release-list.tsx +173 -0
- package/app/apps/github/src/releases/release-view.tsx +183 -0
- package/app/apps/github/src/releases/releases.ts +308 -0
- package/app/apps/github/src/releases/tag-list.tsx +134 -0
- package/app/apps/github/src/router.tsx +46 -0
- package/app/apps/github/src/routes/$owner/$repo/_code/blob/$.tsx +33 -0
- package/app/apps/github/src/routes/$owner/$repo/_code/tree/$.tsx +28 -0
- package/app/apps/github/src/routes/$owner/$repo/_code.tsx +11 -0
- package/app/apps/github/src/routes/$owner/$repo/_releases/releases/index.tsx +19 -0
- package/app/apps/github/src/routes/$owner/$repo/_releases/tags.tsx +19 -0
- package/app/apps/github/src/routes/$owner/$repo/_releases.tsx +23 -0
- package/app/apps/github/src/routes/$owner/$repo/branches.tsx +39 -0
- package/app/apps/github/src/routes/$owner/$repo/commit/$sha.tsx +34 -0
- package/app/apps/github/src/routes/$owner/$repo/commits/$.tsx +42 -0
- package/app/apps/github/src/routes/$owner/$repo/compare/$.tsx +19 -0
- package/app/apps/github/src/routes/$owner/$repo/index.tsx +24 -0
- package/app/apps/github/src/routes/$owner/$repo/issues/$number.tsx +50 -0
- package/app/apps/github/src/routes/$owner/$repo/issues/index.tsx +53 -0
- package/app/apps/github/src/routes/$owner/$repo/issues/new.tsx +43 -0
- package/app/apps/github/src/routes/$owner/$repo/labels.tsx +22 -0
- package/app/apps/github/src/routes/$owner/$repo/pull/$number/checks.tsx +15 -0
- package/app/apps/github/src/routes/$owner/$repo/pull/$number/commits.tsx +15 -0
- package/app/apps/github/src/routes/$owner/$repo/pull/$number/files.tsx +15 -0
- package/app/apps/github/src/routes/$owner/$repo/pull/$number/index.tsx +19 -0
- package/app/apps/github/src/routes/$owner/$repo/pull/$number/route.tsx +52 -0
- package/app/apps/github/src/routes/$owner/$repo/pulls/index.tsx +48 -0
- package/app/apps/github/src/routes/$owner/$repo/queue.tsx +22 -0
- package/app/apps/github/src/routes/$owner/$repo/releases/edit/$.tsx +58 -0
- package/app/apps/github/src/routes/$owner/$repo/releases/new.tsx +40 -0
- package/app/apps/github/src/routes/$owner/$repo/releases/tag/$.tsx +35 -0
- package/app/apps/github/src/routes/$owner/$repo/route.tsx +102 -0
- package/app/apps/github/src/routes/$owner/$repo/settings/access.tsx +9 -0
- package/app/apps/github/src/routes/$owner/$repo/settings/hooks/index.tsx +16 -0
- package/app/apps/github/src/routes/$owner/$repo/settings/hooks/new.tsx +25 -0
- package/app/apps/github/src/routes/$owner/$repo/settings/index.tsx +17 -0
- package/app/apps/github/src/routes/$owner/$repo/settings/route.tsx +28 -0
- package/app/apps/github/src/routes/$owner/$repo/settings/rules.tsx +12 -0
- package/app/apps/github/src/routes/$owner/$repo/tokens.tsx +16 -0
- package/app/apps/github/src/routes/$owner/index.tsx +45 -0
- package/app/apps/github/src/routes/-nav.tsx +293 -0
- package/app/apps/github/src/routes/-root-testing.ts +107 -0
- package/app/apps/github/src/routes/__root.tsx +225 -0
- package/app/apps/github/src/routes/api.auth.$.ts +18 -0
- package/app/apps/github/src/routes/downloads/$owner/$repo/$asset.tsx +10 -0
- package/app/apps/github/src/routes/file-icons[.]svg.ts +21 -0
- package/app/apps/github/src/routes/icons/$owner/$repo.tsx +14 -0
- package/app/apps/github/src/routes/icons/$owner/index.tsx +14 -0
- package/app/apps/github/src/routes/icons/index.tsx +12 -0
- package/app/apps/github/src/routes/index.tsx +48 -0
- package/app/apps/github/src/routes/login.tsx +24 -0
- package/app/apps/github/src/routes/login_.code.tsx +31 -0
- package/app/apps/github/src/routes/new.tsx +91 -0
- package/app/apps/github/src/routes/notifications.tsx +20 -0
- package/app/apps/github/src/routes/orgs/$owner/issues.tsx +8 -0
- package/app/apps/github/src/routes/orgs/$owner/pulls.tsx +8 -0
- package/app/apps/github/src/routes/orgs/$owner/releases.tsx +8 -0
- package/app/apps/github/src/routes/orgs/$owner/route.tsx +25 -0
- package/app/apps/github/src/routes/orgs/$owner/settings.tsx +30 -0
- package/app/apps/github/src/routes/orgs/$owner/tokens.tsx +15 -0
- package/app/apps/github/src/routes/settings.tsx +54 -0
- package/app/apps/github/src/routes/signup.tsx +24 -0
- package/app/apps/github/src/routes/uploads/$owner/$repo/$release.tsx +10 -0
- package/app/apps/github/src/seed/acme.ts +498 -0
- package/app/apps/github/src/seed/licenses.ts +114 -0
- package/app/apps/github/src/seed/seed.ts +538 -0
- package/app/apps/github/src/server.ts +97 -0
- package/app/apps/github/src/settings/account-avatar.tsx +43 -0
- package/app/apps/github/src/settings/avatar-section.tsx +140 -0
- package/app/apps/github/src/settings/avatar.ts +58 -0
- package/app/apps/github/src/settings/general-settings.tsx +439 -0
- package/app/apps/github/src/settings/general.ts +73 -0
- package/app/apps/github/src/settings/hook-form.tsx +195 -0
- package/app/apps/github/src/settings/hook-list.tsx +166 -0
- package/app/apps/github/src/settings/hooks.ts +87 -0
- package/app/apps/github/src/settings/org-settings.tsx +228 -0
- package/app/apps/github/src/settings/orgs.ts +116 -0
- package/app/apps/github/src/settings/rule-list.tsx +72 -0
- package/app/apps/github/src/settings/rules.ts +27 -0
- package/app/apps/github/src/settings/settings-layout.tsx +114 -0
- package/app/apps/github/src/signin/approval.ts +186 -0
- package/app/apps/github/src/signin/auth-client.ts +127 -0
- package/app/apps/github/src/signin/auth.ts +753 -0
- package/app/apps/github/src/signin/avatars.ts +126 -0
- package/app/apps/github/src/signin/client-ip.ts +39 -0
- package/app/apps/github/src/signin/credentials.ts +26 -0
- package/app/apps/github/src/signin/device-code.ts +95 -0
- package/app/apps/github/src/signin/device-prompt.ts +125 -0
- package/app/apps/github/src/signin/errors.ts +135 -0
- package/app/apps/github/src/signin/host-session.ts +68 -0
- package/app/apps/github/src/signin/orgs.ts +283 -0
- package/app/apps/github/src/signin/owner-names.ts +183 -0
- package/app/apps/github/src/signin/person-lock.ts +104 -0
- package/app/apps/github/src/signin/seal.ts +88 -0
- package/app/apps/github/src/signin/seed-people.ts +92 -0
- package/app/apps/github/src/signin/session.ts +48 -0
- package/app/apps/github/src/signin/unborn.ts +110 -0
- package/app/apps/github/src/signin/viewer-orgs.ts +47 -0
- package/app/apps/github/src/signin/viewer.ts +74 -0
- package/app/apps/github/src/signin/with-headers.ts +14 -0
- package/app/apps/github/src/tokens/tokens-page.tsx +529 -0
- package/app/apps/github/src/tokens/tokens.ts +54 -0
- package/app/apps/github/tsconfig.json +25 -0
- package/app/apps/github/tsconfig.worker.json +24 -0
- package/app/apps/github/vite.config.ts +21 -0
- package/app/pnpm-workspace.yaml +216 -0
- package/app/tsconfig.base.json +127 -0
- package/dist/LICENSE +202 -0
- package/dist/cli/child.mjs +42 -0
- package/dist/cli/commands.mjs +23 -0
- package/dist/cli/credentials.mjs +84 -0
- package/dist/cli/documents.mjs +214 -0
- package/dist/cli/events.mjs +55 -0
- package/dist/cli/failure.mjs +105 -0
- package/dist/cli/folders.mjs +32 -0
- package/dist/cli/git-remote-ref0.d.mts +1 -0
- package/dist/cli/git-remote-ref0.mjs +29 -0
- package/dist/cli/git.mjs +48 -0
- package/dist/cli/helpers.mjs +51 -0
- package/dist/cli/hosts.mjs +127 -0
- package/dist/cli/node.mjs +15 -0
- package/dist/cli/output.mjs +135 -0
- package/dist/cli/prompt.mjs +35 -0
- package/dist/cli/queue.mjs +215 -0
- package/dist/cli/ref0-git.d.mts +1 -0
- package/dist/cli/ref0-git.mjs +144 -0
- package/dist/cli/repos.mjs +162 -0
- package/dist/cli/resources.mjs +131 -0
- package/dist/cli/run-end.mjs +57 -0
- package/dist/cli/runtime.mjs +50 -0
- package/dist/cli/setup.mjs +1410 -0
- package/dist/cli/sign-in.mjs +162 -0
- package/dist/cli/sign-ins.mjs +79 -0
- package/dist/cli/sqlite.mjs +28 -0
- package/dist/cli/tokens.mjs +141 -0
- package/dist/cli/vendor.mjs +123 -0
- package/dist/init/app.mjs +231 -0
- package/dist/init/astro.mjs +49 -0
- package/dist/init/choices.mjs +497 -0
- package/dist/init/cloudflare.mjs +324 -0
- package/dist/init/create.mjs +151 -0
- package/dist/init/detect.mjs +297 -0
- package/dist/init/files.mjs +75 -0
- package/dist/init/frameworks.mjs +248 -0
- package/dist/init/git-module.mjs +49 -0
- package/dist/init/github-routes.mjs +66 -0
- package/dist/init/jsonc.mjs +143 -0
- package/dist/init/metadata.mjs +48 -0
- package/dist/init/mounts.mjs +109 -0
- package/dist/init/next.mjs +205 -0
- package/dist/init/node-app.mjs +189 -0
- package/dist/init/node.mjs +458 -0
- package/dist/init/nuxt.mjs +115 -0
- package/dist/init/output.mjs +21 -0
- package/dist/init/plan.mjs +82 -0
- package/dist/init/range-major.mjs +257 -0
- package/dist/init/react-router.mjs +59 -0
- package/dist/init/routes.mjs +148 -0
- package/dist/init/shell.mjs +4 -0
- package/dist/init/stores.mjs +51 -0
- package/dist/init/sveltekit.mjs +139 -0
- package/dist/init/template.mjs +142 -0
- package/dist/init/templates.mjs +343 -0
- package/dist/init/vercel.mjs +383 -0
- package/dist/init/versions.mjs +32 -0
- package/dist/init/workspace.mjs +20 -0
- package/package.json +48 -6
- package/README.md +0 -5
|
@@ -0,0 +1,753 @@
|
|
|
1
|
+
import type { D1Database } from '@cloudflare/workers-types';
|
|
2
|
+
import type { TokenCalls } from '@ref0/git';
|
|
3
|
+
import { passkey } from '@better-auth/passkey';
|
|
4
|
+
import {
|
|
5
|
+
type Auth,
|
|
6
|
+
type BetterAuthOptions,
|
|
7
|
+
type DBAdapter,
|
|
8
|
+
type DBTransactionAdapter,
|
|
9
|
+
betterAuth,
|
|
10
|
+
getCurrentAdapter,
|
|
11
|
+
} from 'better-auth';
|
|
12
|
+
import { APIError, createAuthMiddleware, getSessionFromCtx } from 'better-auth/api';
|
|
13
|
+
import { organization } from 'better-auth/plugins/organization';
|
|
14
|
+
import { username } from 'better-auth/plugins/username';
|
|
15
|
+
import * as Option from 'effect/Option';
|
|
16
|
+
import * as Schema from 'effect/Schema';
|
|
17
|
+
import type { Dialect } from 'kysely';
|
|
18
|
+
|
|
19
|
+
import type { Session, SessionCookie } from '../context/context.ts';
|
|
20
|
+
import { loginProblem, ownerProblem } from '../names/owners.ts';
|
|
21
|
+
import { deleteUnlessLast } from './credentials.ts';
|
|
22
|
+
import { closedCode, lastCredentialCode, reservedCode } from './errors.ts';
|
|
23
|
+
import {
|
|
24
|
+
claimInTransaction,
|
|
25
|
+
claimLogin,
|
|
26
|
+
holdLogin,
|
|
27
|
+
isHeld,
|
|
28
|
+
isTaken,
|
|
29
|
+
ownerNames,
|
|
30
|
+
} from './owner-names.ts';
|
|
31
|
+
import { open, seal } from './seal.ts';
|
|
32
|
+
import { avatarTable, avatars } from './avatars.ts';
|
|
33
|
+
import { type Unborn, forgetUnborn } from './unborn.ts';
|
|
34
|
+
|
|
35
|
+
/** A Kysely dialect, and the SQL it speaks, as Better Auth takes one: libSQL's, or Postgres's. */
|
|
36
|
+
export interface DialectDatabase {
|
|
37
|
+
readonly dialect: Dialect;
|
|
38
|
+
readonly type: 'sqlite' | 'postgres';
|
|
39
|
+
/** Whether Better Auth wraps its writes in transactions: off unless the dialect has them. */
|
|
40
|
+
readonly transaction?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A value `node:sqlite` binds to a statement's parameter. */
|
|
44
|
+
export type NodeSqliteValue = string | number | bigint | null | Uint8Array;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* `node:sqlite`'s `DatabaseSync`, by the parts the app calls. The pages' types hold no Node's, so
|
|
48
|
+
* it's named by its shape, and Better Auth tells it from D1's binding with `instanceof` as it runs.
|
|
49
|
+
*/
|
|
50
|
+
export interface NodeSqliteDatabase {
|
|
51
|
+
readonly prepare: (sql: string) => {
|
|
52
|
+
readonly run: (...values: NodeSqliteValue[]) => { readonly changes: number | bigint };
|
|
53
|
+
};
|
|
54
|
+
readonly close: () => void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Where people and sessions are kept, as the platform's file passes it: on Cloudflare, the
|
|
59
|
+
* `AUTH_DB` binding, which Better Auth reads through its own D1 dialect; on Node with SQLite,
|
|
60
|
+
* `node:sqlite`'s file, which it reads through its own too; and elsewhere a dialect.
|
|
61
|
+
*/
|
|
62
|
+
export type AuthDatabase = D1Database | NodeSqliteDatabase | DialectDatabase;
|
|
63
|
+
|
|
64
|
+
/** What the app's people need: their database, the session secret, where it's served, and Git's tokens. */
|
|
65
|
+
export interface StarterAuthOptions {
|
|
66
|
+
/** Better Auth's tables, which `migrations/sql/` makes in D1. */
|
|
67
|
+
readonly database: AuthDatabase;
|
|
68
|
+
/** `SESSION_SECRET`, which signs Better Auth's cookie. */
|
|
69
|
+
readonly secret: string;
|
|
70
|
+
/** `APP_ORIGIN`: Better Auth's base URL, and the one origin its POSTs are taken from. */
|
|
71
|
+
readonly origin: string;
|
|
72
|
+
/** Whether anyone may sign up: `SIGN_UP=closed` turns it off. */
|
|
73
|
+
readonly signUp: boolean;
|
|
74
|
+
/** The key a session's Git token is sealed under in D1. */
|
|
75
|
+
readonly key: Promise<CryptoKey>;
|
|
76
|
+
/** Git's tokens, which each session mints its own from, and revokes at its end. */
|
|
77
|
+
readonly tokens: TokenCalls;
|
|
78
|
+
/** Whether Git holds a repo under `<login>/`, or couldn't say: such a person is never forgotten. */
|
|
79
|
+
readonly ownsRepos: (login: string) => Promise<boolean>;
|
|
80
|
+
/**
|
|
81
|
+
* The header Better Auth keys its rate limits on, which only the platform sets: Cloudflare's
|
|
82
|
+
* `cf-connecting-ip`, or on Node `clientIpHeader`, which the server sets from the caller's
|
|
83
|
+
* address after dropping any copy the caller sent.
|
|
84
|
+
*/
|
|
85
|
+
readonly ipHeader: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** How long a session lasts, and its Git token with it: it's never refreshed. */
|
|
89
|
+
const sessionSeconds = 30 * 24 * 60 * 60;
|
|
90
|
+
|
|
91
|
+
const purpose = 'git-token';
|
|
92
|
+
|
|
93
|
+
/** The key a session's Git token is made with, which its delete revokes it by. */
|
|
94
|
+
const sessionKeyOf = (id: string) => `session:${id}`;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Better Auth's routes the starter turns off: signing in by email, which no one has, changing it,
|
|
98
|
+
* and the resets and checks that would mail it. And deleting a person, whose login holds repos and
|
|
99
|
+
* tokens (Q631). Each is refused in its `//` form too, by the route that mounts the rest.
|
|
100
|
+
*/
|
|
101
|
+
export const disabledPaths: readonly string[] = [
|
|
102
|
+
'/sign-in/email',
|
|
103
|
+
'/change-email',
|
|
104
|
+
'/request-password-reset',
|
|
105
|
+
'/reset-password',
|
|
106
|
+
'/forget-password',
|
|
107
|
+
'/send-verification-email',
|
|
108
|
+
'/verify-email',
|
|
109
|
+
'/delete-user',
|
|
110
|
+
// An org adds its members by login, at once: invitations go by email, which no one has (Q636).
|
|
111
|
+
'/organization/invite-member',
|
|
112
|
+
'/organization/accept-invitation',
|
|
113
|
+
'/organization/reject-invitation',
|
|
114
|
+
'/organization/cancel-invitation',
|
|
115
|
+
'/organization/get-invitation',
|
|
116
|
+
'/organization/list-invitations',
|
|
117
|
+
'/organization/list-user-invitations',
|
|
118
|
+
];
|
|
119
|
+
|
|
120
|
+
/** Sign-up's body, as far as the app reads it: a login, and the name, which may be empty. */
|
|
121
|
+
const SignUpBody = Schema.Struct({ username: Schema.String, name: Schema.optional(Schema.String) });
|
|
122
|
+
|
|
123
|
+
const signUpBodyOf = Schema.decodeUnknownOption(SignUpBody);
|
|
124
|
+
|
|
125
|
+
/** The body of `/organization/check-slug`, as the plugin reads it. */
|
|
126
|
+
const SlugBody = Schema.Struct({ slug: Schema.String });
|
|
127
|
+
|
|
128
|
+
const slugBodyOf = Schema.decodeUnknownOption(SlugBody);
|
|
129
|
+
|
|
130
|
+
/** Refuses a login no one may sign up with, in the app's own words: reserved, or not a login. */
|
|
131
|
+
const refuseLogin = (login: string) => {
|
|
132
|
+
const problem = ownerProblem(login);
|
|
133
|
+
if (problem === undefined) return;
|
|
134
|
+
|
|
135
|
+
const code = loginProblem(login) === undefined ? reservedCode : 'INVALID_USERNAME';
|
|
136
|
+
throw new APIError('BAD_REQUEST', { code, message: problem });
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/** The email Better Auth asks every person for, which reaches no one: the login's, at the host. */
|
|
140
|
+
const hiddenEmail = (login: string, hostname: string) =>
|
|
141
|
+
`${login.toLowerCase()}@users.noreply.${hostname}`;
|
|
142
|
+
|
|
143
|
+
/** The name a person goes by: the one they gave, or their login when they left it empty. */
|
|
144
|
+
const named = (login: string, name: string | undefined) =>
|
|
145
|
+
name === undefined || name.trim() === '' ? login : name;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* What the app sets in sign-up's body, once its login is checked in the app's own words: the
|
|
149
|
+
* hidden email from the login, whatever the page sent, and the login as the name left empty.
|
|
150
|
+
*/
|
|
151
|
+
const signUpFields = (body: Option.Option<typeof SignUpBody.Type>, hostname: string) => {
|
|
152
|
+
const sent = Option.getOrUndefined(body);
|
|
153
|
+
const login = sent?.username ?? '';
|
|
154
|
+
refuseLogin(login);
|
|
155
|
+
|
|
156
|
+
return { name: named(login, sent?.name), email: hiddenEmail(login, hostname) };
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/** A passkey sign-up's refusal when `SIGN_UP=closed`, as a password sign-up's is refused. */
|
|
160
|
+
const closedText = 'Sign-up is closed on this host';
|
|
161
|
+
|
|
162
|
+
/** Who a passkey sign-up is for, as its page sends it in the plugin's `context`: a JSON object. */
|
|
163
|
+
const NewPerson = Schema.fromJsonString(
|
|
164
|
+
Schema.Struct({ login: Schema.String, name: Schema.optional(Schema.String) }),
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const newPersonOf = Schema.decodeUnknownOption(NewPerson);
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The person a passkey sign-up is for, from its `context`, once sign-up is open and the login is
|
|
171
|
+
* one: the same refusals as a password sign-up's.
|
|
172
|
+
*/
|
|
173
|
+
const newPerson = (context: string | null | undefined, signUp: boolean) => {
|
|
174
|
+
if (!signUp) throw new APIError('BAD_REQUEST', { code: closedCode, message: closedText });
|
|
175
|
+
|
|
176
|
+
const person = Option.getOrUndefined(newPersonOf(context));
|
|
177
|
+
refuseLogin(person?.login ?? '');
|
|
178
|
+
|
|
179
|
+
return { login: person?.login ?? '', name: person?.name };
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The plugin's user ID for a person who doesn't exist yet: `afterVerification` makes them. The
|
|
184
|
+
* prefix holds no login's characters, so it never names a real person's ID.
|
|
185
|
+
*/
|
|
186
|
+
const pending = 'sign-up:';
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The person each passkey sign-up made, by its request's context, so a failure after the person
|
|
190
|
+
* was made deletes them: D1 has no transactions to roll the person back with the passkey.
|
|
191
|
+
*/
|
|
192
|
+
const madeBySignUp = new WeakMap<object, Unborn>();
|
|
193
|
+
|
|
194
|
+
/** Each passkey route's limit per IP: a sign-up's and a sign-in's are a password's, 3 per 10 s. */
|
|
195
|
+
const passkeyLimits = {
|
|
196
|
+
'/passkey/verify-registration': { window: 10, max: 3 },
|
|
197
|
+
'/passkey/verify-authentication': { window: 10, max: 3 },
|
|
198
|
+
// Each writes a challenge's row, and a login page asks for one on each load, for autofill.
|
|
199
|
+
'/passkey/generate-register-options': { window: 10, max: 10 },
|
|
200
|
+
'/passkey/generate-authenticate-options': { window: 10, max: 10 },
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/** The routes that add a passkey, which a session past `freshAge` may not. */
|
|
204
|
+
const addingPaths: readonly string[] = [
|
|
205
|
+
'/passkey/generate-register-options',
|
|
206
|
+
'/passkey/verify-registration',
|
|
207
|
+
];
|
|
208
|
+
|
|
209
|
+
/** Delete-passkey's body, as far as the app reads it. */
|
|
210
|
+
const DeleteBody = Schema.Struct({ id: Schema.String });
|
|
211
|
+
|
|
212
|
+
const deleteBodyOf = Schema.decodeUnknownOption(DeleteBody);
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The login an org's slug is, once checked in the app's own words, as a sign-up's is: lowercased,
|
|
216
|
+
* since an org's login shares the people's namespace.
|
|
217
|
+
*/
|
|
218
|
+
const orgLogin = (slug: string | undefined) => {
|
|
219
|
+
const login = (slug ?? '').toLowerCase();
|
|
220
|
+
const problem = ownerProblem(login);
|
|
221
|
+
|
|
222
|
+
if (problem !== undefined) {
|
|
223
|
+
const code = loginProblem(login) === undefined ? reservedCode : 'INVALID_SLUG';
|
|
224
|
+
throw new APIError('BAD_REQUEST', { code, message: problem });
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return login;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/** A login some person or org holds, or held: refused, as a taken one. */
|
|
231
|
+
const taken = (code: string) =>
|
|
232
|
+
new APIError('BAD_REQUEST', { code, message: 'That login is taken' });
|
|
233
|
+
|
|
234
|
+
/** A create's claim on a login, from the hook that claims it to the one that holds it. */
|
|
235
|
+
interface Claimed {
|
|
236
|
+
readonly login: string;
|
|
237
|
+
readonly claim: string;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Each person's create's claim, by its request's context, until `create.after` holds it. */
|
|
241
|
+
const personClaims = new WeakMap<object, Claimed>();
|
|
242
|
+
|
|
243
|
+
/** Each org's create's claim, by the creator's row, which the plugin passes both its hooks. */
|
|
244
|
+
const orgClaims = new WeakMap<object, Claimed>();
|
|
245
|
+
|
|
246
|
+
/** What Better Auth gives a person's `create.after`: their row, and the request's context or `null`. */
|
|
247
|
+
type PersonMade = Parameters<
|
|
248
|
+
NonNullable<
|
|
249
|
+
NonNullable<
|
|
250
|
+
NonNullable<NonNullable<BetterAuthOptions['databaseHooks']>['user']>['create']
|
|
251
|
+
>['after']
|
|
252
|
+
>
|
|
253
|
+
>;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* The claim `create.before` left for a person `create.after` is given, taken from where it was
|
|
257
|
+
* kept: by the request's context, or, for a create no request makes, by its login in `requestless`.
|
|
258
|
+
*/
|
|
259
|
+
const claimOf = (
|
|
260
|
+
requestless: Map<string, string>,
|
|
261
|
+
user: PersonMade[0],
|
|
262
|
+
context: PersonMade[1],
|
|
263
|
+
): Claimed | undefined => {
|
|
264
|
+
// Outside a request Better Auth's context store answers `undefined`, though its type says `null`.
|
|
265
|
+
if (context !== null && context !== undefined) {
|
|
266
|
+
const claimed = personClaims.get(context);
|
|
267
|
+
personClaims.delete(context);
|
|
268
|
+
|
|
269
|
+
return claimed;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const login = Option.getOrUndefined(namedOf(user))?.username.toLowerCase();
|
|
273
|
+
const claim = login === undefined ? undefined : requestless.get(login);
|
|
274
|
+
if (login === undefined || claim === undefined) return undefined;
|
|
275
|
+
|
|
276
|
+
requestless.delete(login);
|
|
277
|
+
|
|
278
|
+
return { login, claim };
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
/** Whether a hook writes inside a real transaction: `current` is its own, and can roll back. */
|
|
282
|
+
const inTransaction = (base: DBAdapter, current: DBTransactionAdapter) => {
|
|
283
|
+
const transaction = base.options?.adapterConfig.transaction;
|
|
284
|
+
|
|
285
|
+
return transaction !== undefined && transaction !== false && current !== base;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/** A person's row, as far as a session reads it: Better Auth's username is their login. */
|
|
289
|
+
const Named = Schema.Struct({ username: Schema.String });
|
|
290
|
+
|
|
291
|
+
const namedOf = Schema.decodeUnknownOption(Named);
|
|
292
|
+
|
|
293
|
+
/** The login on a row, when it holds one: never the admin's `_admin`, which no login spells. */
|
|
294
|
+
const loginOf = (row: Option.Option<typeof Named.Type>) => {
|
|
295
|
+
const login = Option.getOrUndefined(row)?.username;
|
|
296
|
+
|
|
297
|
+
return login !== undefined && loginProblem(login) === undefined ? login : undefined;
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
/** A session row's Git token, as D1 holds it: sealed, beside its ID. */
|
|
301
|
+
const HeldToken = Schema.Struct({ gitTokenId: Schema.String, gitToken: Schema.String });
|
|
302
|
+
|
|
303
|
+
const heldTokenOf = Schema.decodeUnknownOption(HeldToken);
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Better Auth for the starter: a person signs up with a login and a passkey or a password, and
|
|
307
|
+
* signs in with either. Each session mints its person a capped Git token, sealed in D1, and revokes it when the
|
|
308
|
+
* session ends. A login that isn't one, even in a row written past Better Auth, makes no session.
|
|
309
|
+
*/
|
|
310
|
+
export const starterAuth = (options: StarterAuthOptions): Auth => {
|
|
311
|
+
const hostname = new URL(options.origin).hostname;
|
|
312
|
+
/**
|
|
313
|
+
* Each claim of a person's create that no request makes, by its login, until `create.after`
|
|
314
|
+
* holds it: a hold is conditional, so a token another create of that login left here still holds
|
|
315
|
+
* the claim only while the claim is its, for a person who exists.
|
|
316
|
+
*/
|
|
317
|
+
const requestlessClaims = new Map<string, string>();
|
|
318
|
+
|
|
319
|
+
const auth = betterAuth<BetterAuthOptions>({
|
|
320
|
+
database: options.database,
|
|
321
|
+
secret: options.secret,
|
|
322
|
+
baseURL: options.origin,
|
|
323
|
+
trustedOrigins: [options.origin],
|
|
324
|
+
telemetry: { enabled: false },
|
|
325
|
+
disabledPaths: [...disabledPaths],
|
|
326
|
+
emailAndPassword: {
|
|
327
|
+
enabled: true,
|
|
328
|
+
disableSignUp: !options.signUp,
|
|
329
|
+
minPasswordLength: 15,
|
|
330
|
+
maxPasswordLength: 256,
|
|
331
|
+
},
|
|
332
|
+
plugins: [
|
|
333
|
+
username({
|
|
334
|
+
minUsernameLength: 1,
|
|
335
|
+
maxUsernameLength: 39,
|
|
336
|
+
usernameValidator: (login) => loginProblem(login) === undefined,
|
|
337
|
+
immutableUsername: true,
|
|
338
|
+
displayUsername: false,
|
|
339
|
+
}),
|
|
340
|
+
passkey({
|
|
341
|
+
rpID: hostname,
|
|
342
|
+
rpName: hostname,
|
|
343
|
+
origin: options.origin,
|
|
344
|
+
// A passkey signs in with no login typed, so the authenticator must keep it.
|
|
345
|
+
authenticatorSelection: { residentKey: 'required', userVerification: 'required' },
|
|
346
|
+
registration: {
|
|
347
|
+
requireSession: false,
|
|
348
|
+
// Only a sign-up with no session comes here: a signed-in person adds a passkey as themself.
|
|
349
|
+
resolveUser: async ({ ctx, context }) => {
|
|
350
|
+
const person = newPerson(context, options.signUp);
|
|
351
|
+
|
|
352
|
+
// A person's login or an org's: they share one namespace.
|
|
353
|
+
if (await isHeld(ctx.context.adapter, person.login.toLowerCase())) {
|
|
354
|
+
throw new APIError('BAD_REQUEST', {
|
|
355
|
+
code: 'USERNAME_IS_ALREADY_TAKEN',
|
|
356
|
+
message: 'Username is already taken. Please try another.',
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const name = named(person.login, person.name);
|
|
361
|
+
|
|
362
|
+
return { id: `${pending}${person.login}`, name: person.login, displayName: name };
|
|
363
|
+
},
|
|
364
|
+
afterVerification: async ({ ctx, user, context }) => {
|
|
365
|
+
if (!user.id.startsWith(pending)) return undefined;
|
|
366
|
+
|
|
367
|
+
// The login may have been taken, or sign-up closed, since its challenge: check again.
|
|
368
|
+
const person = newPerson(context, options.signUp);
|
|
369
|
+
const made = await ctx.context.internalAdapter.createUser(
|
|
370
|
+
{
|
|
371
|
+
username: person.login.toLowerCase(),
|
|
372
|
+
name: named(person.login, person.name),
|
|
373
|
+
email: hiddenEmail(person.login, hostname),
|
|
374
|
+
emailVerified: false,
|
|
375
|
+
},
|
|
376
|
+
{ method: 'passkey' },
|
|
377
|
+
);
|
|
378
|
+
madeBySignUp.set(ctx.context, { id: made.id, login: person.login.toLowerCase() });
|
|
379
|
+
|
|
380
|
+
return { userId: made.id };
|
|
381
|
+
},
|
|
382
|
+
},
|
|
383
|
+
}),
|
|
384
|
+
organization({
|
|
385
|
+
allowUserToCreateOrganization: true,
|
|
386
|
+
organizationHooks: {
|
|
387
|
+
beforeCreateOrganization: async ({ organization: made, user }) => {
|
|
388
|
+
const login = orgLogin(made.slug);
|
|
389
|
+
const claim = await claimLogin(await adapterOf(), login);
|
|
390
|
+
if (claim === undefined) throw taken('ORGANIZATION_ALREADY_EXISTS');
|
|
391
|
+
|
|
392
|
+
orgClaims.set(user, { login, claim });
|
|
393
|
+
|
|
394
|
+
return { data: { ...made, slug: login } };
|
|
395
|
+
},
|
|
396
|
+
// The plugin writes the org and its owner in no transaction, so its claim is held once
|
|
397
|
+
// both exist. A retake that replaced the claim since wins: the org it made is deleted.
|
|
398
|
+
afterCreateOrganization: async ({ organization: made, user }) => {
|
|
399
|
+
const claimed = orgClaims.get(user);
|
|
400
|
+
orgClaims.delete(user);
|
|
401
|
+
|
|
402
|
+
if (claimed === undefined) throw taken('ORGANIZATION_ALREADY_EXISTS');
|
|
403
|
+
|
|
404
|
+
const adapter = await adapterOf();
|
|
405
|
+
if (await holdLogin(adapter, claimed.login, claimed.claim)) return;
|
|
406
|
+
|
|
407
|
+
await adapter.deleteMany({
|
|
408
|
+
model: 'member',
|
|
409
|
+
where: [{ field: 'organizationId', value: made.id }],
|
|
410
|
+
});
|
|
411
|
+
await adapter.delete({
|
|
412
|
+
model: 'organization',
|
|
413
|
+
where: [{ field: 'id', value: made.id }],
|
|
414
|
+
});
|
|
415
|
+
throw taken('ORGANIZATION_ALREADY_EXISTS');
|
|
416
|
+
},
|
|
417
|
+
// An org's picture goes with it, so no org made later under a new ID finds a row.
|
|
418
|
+
afterDeleteOrganization: async ({ organization: deleted }) => {
|
|
419
|
+
await avatars(adapterOf).remove('org', deleted.id);
|
|
420
|
+
},
|
|
421
|
+
// A login holds repos and tokens, so an org's never changes, as a person's doesn't.
|
|
422
|
+
beforeUpdateOrganization: async ({ organization: changed, member }) => {
|
|
423
|
+
if (changed.slug === undefined) return;
|
|
424
|
+
|
|
425
|
+
const adapter = await adapterOf();
|
|
426
|
+
const held = await adapter.findOne<{ slug: string }>({
|
|
427
|
+
model: 'organization',
|
|
428
|
+
where: [{ field: 'id', value: member.organizationId }],
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
if (held?.slug !== changed.slug) {
|
|
432
|
+
throw new APIError('BAD_REQUEST', {
|
|
433
|
+
code: 'SLUG_IS_IMMUTABLE',
|
|
434
|
+
message: "An org's login can't change",
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
},
|
|
438
|
+
},
|
|
439
|
+
}),
|
|
440
|
+
ownerNames(),
|
|
441
|
+
avatarTable(),
|
|
442
|
+
// A credential ID names one passkey only, since the plugin finds a sign-in's passkey by it.
|
|
443
|
+
// Its schema only indexes the column, so this makes it unique, in each dialect's migration.
|
|
444
|
+
{
|
|
445
|
+
id: 'unique-credential-id',
|
|
446
|
+
schema: {
|
|
447
|
+
passkey: { fields: { credentialID: { type: 'string', required: true, unique: true } } },
|
|
448
|
+
},
|
|
449
|
+
},
|
|
450
|
+
],
|
|
451
|
+
session: {
|
|
452
|
+
expiresIn: sessionSeconds,
|
|
453
|
+
disableSessionRefresh: true,
|
|
454
|
+
additionalFields: {
|
|
455
|
+
gitTokenId: { type: 'string', required: false, input: false, returned: false },
|
|
456
|
+
gitToken: { type: 'string', required: false, input: false, returned: false },
|
|
457
|
+
},
|
|
458
|
+
},
|
|
459
|
+
rateLimit: { enabled: true, storage: 'database', customRules: passkeyLimits },
|
|
460
|
+
advanced: {
|
|
461
|
+
// An IPv6 caller is keyed by its /64, Better Auth's `ipv6Subnet` default, so one host's
|
|
462
|
+
// addresses share a bucket.
|
|
463
|
+
ipAddress: { ipAddressHeaders: [options.ipHeader] },
|
|
464
|
+
// On, whatever `NODE_ENV` says: Better Auth turns both off under `test`.
|
|
465
|
+
disableOriginCheck: false,
|
|
466
|
+
disableCSRFCheck: false,
|
|
467
|
+
},
|
|
468
|
+
hooks: {
|
|
469
|
+
before: createAuthMiddleware(async (ctx) => {
|
|
470
|
+
// A browser names its page's origin on every POST, and only the app's own may sign in or
|
|
471
|
+
// up: Better Auth checks it only once a cookie is sent, and `/sign-in/username` never.
|
|
472
|
+
const from = ctx.request?.headers.get('origin');
|
|
473
|
+
|
|
474
|
+
if (from !== null && from !== undefined && from !== options.origin) {
|
|
475
|
+
throw new APIError('FORBIDDEN', { code: 'INVALID_ORIGIN', message: 'Invalid origin' });
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (addingPaths.includes(ctx.path)) await refuseStale(ctx);
|
|
479
|
+
|
|
480
|
+
// The plugin checks a slug only against orgs: this answers as a create of it would.
|
|
481
|
+
if (ctx.path === '/organization/check-slug') {
|
|
482
|
+
const login = orgLogin(Option.getOrUndefined(slugBodyOf(ctx.body))?.slug);
|
|
483
|
+
if (await isTaken(await adapterOf(), login))
|
|
484
|
+
throw taken('ORGANIZATION_SLUG_ALREADY_TAKEN');
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (ctx.path === '/passkey/delete-passkey') {
|
|
488
|
+
const deleted = await deleteOrRefuse(options.database, ctx);
|
|
489
|
+
if (deleted) return ctx.json({ status: true });
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
return ctx.path === '/sign-up/email'
|
|
493
|
+
? { context: { body: signUpFields(signUpBodyOf(ctx.body), hostname) } }
|
|
494
|
+
: undefined;
|
|
495
|
+
}),
|
|
496
|
+
after: createAuthMiddleware(async (ctx) => {
|
|
497
|
+
// A passkey sign-up that failed once its person was made, at the passkey's row or the
|
|
498
|
+
// session's Git token, forgets that person, so their login is free to try again.
|
|
499
|
+
const made = madeBySignUp.get(ctx.context);
|
|
500
|
+
if (made === undefined || !(ctx.context.returned instanceof APIError)) return;
|
|
501
|
+
|
|
502
|
+
await forgetUnborn(options, made);
|
|
503
|
+
}),
|
|
504
|
+
},
|
|
505
|
+
databaseHooks: {
|
|
506
|
+
user: {
|
|
507
|
+
create: {
|
|
508
|
+
// Inside the sign-up's transaction where the database has one, the claim is taken and
|
|
509
|
+
// held on its connection, with the person, since `create.after` waits for the commit
|
|
510
|
+
// (Better Auth's `db/with-hooks.mjs:34`), when a retake could take the claim. On D1,
|
|
511
|
+
// which has none, the claim is taken here and `create.after` holds it once the person,
|
|
512
|
+
// their passkey and their session exist.
|
|
513
|
+
before: async (user, context) => {
|
|
514
|
+
// The username plugin lowercases a login before this hook, but a create past it may
|
|
515
|
+
// not: a claim is the lowercase login's, as `owner_names` and every read of it are.
|
|
516
|
+
const login = namedOf(user).pipe(Option.map((named) => named.username.toLowerCase()));
|
|
517
|
+
if (Option.isNone(login)) return;
|
|
518
|
+
|
|
519
|
+
const base = await adapterOf();
|
|
520
|
+
const current = await getCurrentAdapter(base);
|
|
521
|
+
|
|
522
|
+
if (inTransaction(base, current)) {
|
|
523
|
+
const claim = await claimInTransaction(current, login.value);
|
|
524
|
+
|
|
525
|
+
if (claim === undefined || !(await holdLogin(current, login.value, claim))) {
|
|
526
|
+
throw taken('USERNAME_IS_ALREADY_TAKEN');
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const claim = await claimLogin(base, login.value);
|
|
533
|
+
if (claim === undefined) throw taken('USERNAME_IS_ALREADY_TAKEN');
|
|
534
|
+
|
|
535
|
+
if (context === null || context === undefined) {
|
|
536
|
+
requestlessClaims.set(login.value, claim);
|
|
537
|
+
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
personClaims.set(context, { login: login.value, claim });
|
|
542
|
+
},
|
|
543
|
+
// A retake that replaced the claim since the person was made wins: the person is
|
|
544
|
+
// deleted with their sessions, whose hooks revoke each one's Git token by its key, so
|
|
545
|
+
// none is minted after, and their passkey, by its foreign key. A passkey sign-up's
|
|
546
|
+
// cleanup then has no one left.
|
|
547
|
+
after: async (user, context) => {
|
|
548
|
+
const claimed = claimOf(requestlessClaims, user, context);
|
|
549
|
+
if (claimed === undefined) return;
|
|
550
|
+
|
|
551
|
+
if (await holdLogin(await adapterOf(), claimed.login, claimed.claim)) return;
|
|
552
|
+
|
|
553
|
+
if (context !== null && context !== undefined) madeBySignUp.delete(context.context);
|
|
554
|
+
await (await auth.$context).internalAdapter.deleteUser(user.id);
|
|
555
|
+
throw taken('USERNAME_IS_ALREADY_TAKEN');
|
|
556
|
+
},
|
|
557
|
+
},
|
|
558
|
+
// A person's picture goes with them, however they're deleted: a retaken claim above, or
|
|
559
|
+
// the admin's delete, since the app has no route of its own for it.
|
|
560
|
+
delete: {
|
|
561
|
+
after: async (user) => {
|
|
562
|
+
await avatars(adapterOf).remove('user', user.id);
|
|
563
|
+
},
|
|
564
|
+
},
|
|
565
|
+
},
|
|
566
|
+
session: {
|
|
567
|
+
create: {
|
|
568
|
+
before: async (session, ctx) => {
|
|
569
|
+
const user = await ctx?.context.internalAdapter.findUserById(session.userId);
|
|
570
|
+
|
|
571
|
+
return loginOf(namedOf(user)) === undefined ? false : undefined;
|
|
572
|
+
},
|
|
573
|
+
// Minted once the session commits, so no rollback leaves it live (#498). A session left
|
|
574
|
+
// without one is signed out, never minted lazily.
|
|
575
|
+
after: async (session, ctx) => {
|
|
576
|
+
const context = ctx?.context;
|
|
577
|
+
const user = await context?.internalAdapter.findUserById(session.userId);
|
|
578
|
+
const login = loginOf(namedOf(user));
|
|
579
|
+
if (context === undefined || login === undefined) return;
|
|
580
|
+
|
|
581
|
+
const made = await options.tokens.create(
|
|
582
|
+
{ prefix: '' },
|
|
583
|
+
{
|
|
584
|
+
scope: 'write',
|
|
585
|
+
subject: { id: login },
|
|
586
|
+
expiresAt: session.expiresAt,
|
|
587
|
+
key: sessionKeyOf(session.id),
|
|
588
|
+
},
|
|
589
|
+
);
|
|
590
|
+
if (made.kind !== 'success') return;
|
|
591
|
+
|
|
592
|
+
const gitToken = await seal(await options.key, purpose, made.value.secret);
|
|
593
|
+
const held = await context.adapter.update({
|
|
594
|
+
model: 'session',
|
|
595
|
+
where: [
|
|
596
|
+
{ field: 'id', value: session.id },
|
|
597
|
+
{ field: 'gitToken', value: null },
|
|
598
|
+
],
|
|
599
|
+
update: { gitTokenId: made.value.id, gitToken },
|
|
600
|
+
});
|
|
601
|
+
// Only a row with no token takes it; `returning` answers nothing for one it skipped.
|
|
602
|
+
if (held === null || held === undefined) await options.tokens.revoke(made.value.id);
|
|
603
|
+
},
|
|
604
|
+
},
|
|
605
|
+
delete: {
|
|
606
|
+
after: async (session) => {
|
|
607
|
+
// By key, not the row Better Auth read before deleting: a token attached since isn't
|
|
608
|
+
// on it, and one still to come is refused until the session would end.
|
|
609
|
+
const key = sessionKeyOf(session.id);
|
|
610
|
+
const until = session.expiresAt;
|
|
611
|
+
const revoked = await options.tokens.revokeKey({ prefix: '' }, key, { until });
|
|
612
|
+
if (revoked.kind !== 'success') console.error(`Git didn't revoke session token ${key}`);
|
|
613
|
+
},
|
|
614
|
+
},
|
|
615
|
+
},
|
|
616
|
+
},
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
/** Better Auth's adapter, which the organization plugin's hooks, given no context, read through. */
|
|
620
|
+
const adapterOf = async (): Promise<DBAdapter> => (await auth.$context).adapter;
|
|
621
|
+
|
|
622
|
+
return auth;
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* The starter's Better Auth, as the app calls it: its handler, its session and its context. Its
|
|
627
|
+
* plugins' own types stay inside, since they name zod's, which the app doesn't depend on.
|
|
628
|
+
*/
|
|
629
|
+
export type StarterAuth = Auth;
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Better Auth's adapter, once its schema check has settled. Making Better Auth starts the check,
|
|
633
|
+
* and its own routes await it, so only a read of `$context` alone needs this.
|
|
634
|
+
*/
|
|
635
|
+
export const readyAdapter = async (auth: StarterAuth): Promise<DBAdapter> => {
|
|
636
|
+
const context = await auth.$context;
|
|
637
|
+
await context.checkSchema?.();
|
|
638
|
+
|
|
639
|
+
return context.adapter;
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* Better Auth for a request: the isolate's, kept once a request's check of it has settled, or else
|
|
644
|
+
* one `make` makes for this request, which awaits its own check. A Worker settles a promise only
|
|
645
|
+
* in the request that made it, so no request awaits another's check: one that ended first, by a
|
|
646
|
+
* disconnect or an early answer, would leave it unsettled, and every later sign-in with it (#480).
|
|
647
|
+
*
|
|
648
|
+
* So requests that arrive together, before any is kept, each make an instance and run its schema
|
|
649
|
+
* check, and only the first to settle is kept. That's the cost of sharing no promise, and it's
|
|
650
|
+
* bounded by how many requests arrive in that window: once one is kept, no request makes another.
|
|
651
|
+
*/
|
|
652
|
+
export const readyAuth = (make: () => StarterAuth): (() => Promise<StarterAuth>) => {
|
|
653
|
+
let ready: StarterAuth | undefined;
|
|
654
|
+
|
|
655
|
+
return async () => {
|
|
656
|
+
if (ready !== undefined) return ready;
|
|
657
|
+
|
|
658
|
+
const made = make();
|
|
659
|
+
await readyAdapter(made);
|
|
660
|
+
ready ??= made;
|
|
661
|
+
|
|
662
|
+
return ready;
|
|
663
|
+
};
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
type HookContext = Parameters<Parameters<typeof createAuthMiddleware>[0]>[0];
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* Refuses adding a passkey from a session older than `freshAge`, since sessions last 30 days: a
|
|
670
|
+
* stolen cookie mustn't plant a credential that outlasts it. With no session, it's a sign-up.
|
|
671
|
+
*/
|
|
672
|
+
const refuseStale = async (ctx: HookContext) => {
|
|
673
|
+
const found = await getSessionFromCtx(ctx);
|
|
674
|
+
if (found === null) return;
|
|
675
|
+
|
|
676
|
+
const age = Date.now() - new Date(found.session.createdAt).getTime();
|
|
677
|
+
if (age < ctx.context.sessionConfig.freshAge * 1000) return;
|
|
678
|
+
|
|
679
|
+
throw new APIError('FORBIDDEN', { code: 'SESSION_NOT_FRESH', message: 'Session is not fresh' });
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Deletes the session's passkey that the body names, unless it's their last credential, which is
|
|
684
|
+
* refused. With no session, a bad body, or a passkey that isn't theirs, it deletes nothing, and
|
|
685
|
+
* the plugin's own route answers as it does: its check of the owner runs inside the route.
|
|
686
|
+
*/
|
|
687
|
+
const deleteOrRefuse = async (database: AuthDatabase, ctx: HookContext) => {
|
|
688
|
+
const found = await getSessionFromCtx(ctx);
|
|
689
|
+
const body = Option.getOrUndefined(deleteBodyOf(ctx.body));
|
|
690
|
+
if (found === null || body === undefined) return false;
|
|
691
|
+
|
|
692
|
+
const passkey = { userId: found.user.id, id: body.id };
|
|
693
|
+
if (await deleteUnlessLast(database, passkey)) return true;
|
|
694
|
+
|
|
695
|
+
const theirs = await ctx.context.adapter.findOne({
|
|
696
|
+
model: 'passkey',
|
|
697
|
+
where: [
|
|
698
|
+
{ field: 'id', value: passkey.id },
|
|
699
|
+
{ field: 'userId', value: passkey.userId },
|
|
700
|
+
],
|
|
701
|
+
});
|
|
702
|
+
if (theirs === null) return false;
|
|
703
|
+
|
|
704
|
+
throw new APIError('BAD_REQUEST', {
|
|
705
|
+
code: lastCredentialCode,
|
|
706
|
+
message: 'Add another passkey or a password before you remove this one',
|
|
707
|
+
});
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* The browser's session, from Better Auth's cookie, or `undefined` when it has none, it has
|
|
712
|
+
* ended, or its login isn't one. Its Git token is opened from the session's row, which Better
|
|
713
|
+
* Auth never answers the browser with.
|
|
714
|
+
*/
|
|
715
|
+
export const sessionFrom =
|
|
716
|
+
(auth: StarterAuth, key: Promise<CryptoKey>) =>
|
|
717
|
+
async (request: Request): Promise<Session | undefined> => {
|
|
718
|
+
const found = await auth.api.getSession({ headers: request.headers });
|
|
719
|
+
const login = loginOf(namedOf(found?.user));
|
|
720
|
+
if (found === null || login === undefined) return undefined;
|
|
721
|
+
|
|
722
|
+
const { adapter } = await auth.$context;
|
|
723
|
+
const row = await adapter.findOne({
|
|
724
|
+
model: 'session',
|
|
725
|
+
where: [{ field: 'id', value: found.session.id }],
|
|
726
|
+
});
|
|
727
|
+
const held = Option.getOrUndefined(heldTokenOf(row));
|
|
728
|
+
const token = held === undefined ? undefined : await open(await key, purpose, held.gitToken);
|
|
729
|
+
if (held === undefined || token === undefined) return undefined;
|
|
730
|
+
|
|
731
|
+
return { login, token, tokenId: held.gitTokenId, expiresAt: found.session.expiresAt.getTime() };
|
|
732
|
+
};
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Ends the browser's session, once Git refuses its token: Better Auth deletes its row, and the
|
|
736
|
+
* delete hook revokes the token, so a revoke Git already made only logs its id.
|
|
737
|
+
*/
|
|
738
|
+
export const sessionEnder =
|
|
739
|
+
(auth: StarterAuth) =>
|
|
740
|
+
async (request: Request): Promise<void> => {
|
|
741
|
+
const found = await auth.api.getSession({ headers: request.headers });
|
|
742
|
+
if (found === null) return;
|
|
743
|
+
|
|
744
|
+
const { internalAdapter } = await auth.$context;
|
|
745
|
+
await internalAdapter.deleteSession(found.session.token);
|
|
746
|
+
};
|
|
747
|
+
|
|
748
|
+
/** Better Auth's session cookie: its name, `__Secure-` on https, and the path it was set at. */
|
|
749
|
+
export const sessionCookieOf = (auth: StarterAuth) => async (): Promise<SessionCookie> => {
|
|
750
|
+
const { name, attributes } = (await auth.$context).authCookies.sessionToken;
|
|
751
|
+
|
|
752
|
+
return { name, path: attributes.path ?? '/', secure: attributes.secure === true };
|
|
753
|
+
};
|