@vite-hub/auth 0.1.0 → 0.3.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/README.md +333 -49
- package/dist/abilities-DfAnFYwG.d.ts +93 -0
- package/dist/abilities.d.ts +2 -0
- package/dist/abilities.js +43 -0
- package/dist/ability-error-CqgBrBfR.js +91 -0
- package/dist/ability-error-DcgChwCN.d.ts +34 -0
- package/dist/admin-claim-code-CDipqeAU.js +22 -0
- package/dist/agent.d.ts +5 -5
- package/dist/agent.js +17 -9
- package/dist/application-path-DuTpfGFj.js +62 -0
- package/dist/auth-instance-CR6b1W80.js +1168 -0
- package/dist/bun-sqlite.d.ts +8 -0
- package/dist/cli-ubYJpAL_.d.ts +163 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +47 -7
- package/dist/console-runtime-DTopBP-x.d.ts +71 -0
- package/dist/console-runtime-J9_Vx9pT.js +1166 -0
- package/dist/console-runtime.d.ts +3 -3
- package/dist/console-runtime.js +2 -819
- package/dist/console.d.ts +215 -3
- package/dist/console.js +1 -1
- package/dist/database-options-BRNef9oy.js +29 -0
- package/dist/database-schema-options-ENuRMduM.js +12 -0
- package/dist/database.d.ts +12 -0
- package/dist/database.js +45 -0
- package/dist/definition-CqQMcDEK.js +280 -0
- package/dist/error-diagnostics-C8XtVz0i.js +138 -0
- package/dist/{events-P7mI4IQb.js → events-ByzenEVj.js} +122 -4
- package/dist/forward.d.ts +50 -0
- package/dist/forward.js +120 -0
- package/dist/host-declarations.d.ts +2 -1
- package/dist/index.d.ts +24 -3
- package/dist/index.js +3 -102
- package/dist/internal/server.d.ts +5 -0
- package/dist/internal/server.js +858 -0
- package/dist/invariant-D8_xBqmZ.js +22 -0
- package/dist/mcp.d.ts +187 -0
- package/dist/mcp.js +353 -0
- package/dist/members-D9lfaEO_.js +533 -0
- package/dist/nuxt-bridge-Cl988STZ.js +44 -0
- package/dist/nuxt.d.ts +13 -5
- package/dist/nuxt.js +90 -13
- package/dist/object-Da1zyPCR.js +11 -0
- package/dist/page-tools-shared-l4eyj53D.js +19 -0
- package/dist/presets.js +1 -1
- package/dist/request-z6DHHmrb.js +47 -0
- package/dist/runtime/console.d.ts +3 -7
- package/dist/runtime/console.js +4 -42
- package/dist/runtime/empty-abilities.d.ts +5 -0
- package/dist/runtime/empty-abilities.js +4 -0
- package/dist/runtime/empty-client.d.ts +6 -0
- package/dist/runtime/empty-client.js +5 -0
- package/dist/runtime/empty-definition.d.ts +1 -1
- package/dist/runtime/nuxt.js +1 -1
- package/dist/runtime/server.d.ts +2 -0
- package/dist/runtime/server.js +77 -0
- package/dist/runtime/vue-nuxt.d.ts +3 -2
- package/dist/runtime/vue-nuxt.js +157 -5
- package/dist/runtime-state-ojQCzKOc.js +25 -0
- package/dist/schema.d.ts +137 -0
- package/dist/schema.js +147 -0
- package/dist/server-C0qnKN2W.d.ts +143 -0
- package/dist/server-bQGzVQ--.d.ts +41 -0
- package/dist/server.d.ts +3 -2
- package/dist/server.js +3 -3
- package/dist/session-cookies-BbH3UR2m.js +227 -0
- package/dist/{set-cookie-2as_DbHe.js → set-cookie-C8NOhdRE.js} +27 -12
- package/dist/{shared-DnpgYiLF.js → shared-BwcdRGxD.js} +2 -5
- package/dist/table-specs-8gq_FCaq.js +771 -0
- package/dist/table-specs-DbXyaXWs.d.ts +842 -0
- package/dist/test.d.ts +26 -0
- package/dist/test.js +25 -0
- package/dist/{events-BkmK2dFY.d.ts → types-CNaeH2Fd.d.ts} +240 -157
- package/dist/vite-CV400XXY.js +1556 -0
- package/dist/vite.d.ts +138 -20
- package/dist/vite.js +2 -2
- package/dist/vue.d.ts +330 -21
- package/dist/vue.js +583 -36
- package/package.json +84 -15
- package/dist/application-path-C290OAIs.js +0 -18
- package/dist/cli-mgXvsBtA.d.ts +0 -85
- package/dist/console-B7eERmGv.d.ts +0 -180
- package/dist/console-runtime-bZQtnX2Y.d.ts +0 -39
- package/dist/error-diagnostics-CL0yjHiM.js +0 -79
- package/dist/events-config-CjcFOhTE.js +0 -177
- package/dist/inspect-DPtsW8dB.d.ts +0 -36
- package/dist/server-B7LU19dS.js +0 -1730
- package/dist/server-CTD_JAww.d.ts +0 -77
- package/dist/vite-COWkwxsj.js +0 -990
package/dist/console.d.ts
CHANGED
|
@@ -1,7 +1,219 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { $ as AuthConsoleField, G as AuthConsoleActivityQuery, J as AuthConsoleColumn, K as AuthConsoleAgent, Q as AuthConsoleEventUser, St as authConsoleEventGroups, U as AuthConsoleAction, W as AuthConsoleActivity, X as AuthConsoleEvent, Y as AuthConsoleDescription, Z as AuthConsoleEventGroup, _t as AuthConsoleSessionUser, at as AuthConsoleImpersonation, bt as AuthConsoleValue, ct as AuthConsoleOptions, dt as AuthConsoleOverview, et as AuthConsoleFieldPolicy, ft as AuthConsolePage, gt as AuthConsoleSession, ht as AuthConsoleRoles, it as AuthConsoleHostUserInput, lt as AuthConsoleOrganization, mt as AuthConsoleQuery, nt as AuthConsoleHostOptions, ot as AuthConsoleList, pt as AuthConsolePolicy, q as AuthConsoleCapability, rt as AuthConsoleHostSession, st as AuthConsoleMutation, tt as AuthConsoleFieldType, ut as AuthConsoleOrganizationDetail, vt as AuthConsoleUser, xt as authConsoleActionNames, yt as AuthConsoleUserDetail } from "./types-CNaeH2Fd.js";
|
|
2
|
+
//#region ../internal/src/console.d.ts
|
|
3
|
+
/** Lists discovered Definitions in a sidebar and shows the selected Definition's fields. */
|
|
4
|
+
interface ViteHubConsoleDefinitionCatalogView {
|
|
5
|
+
kind: "definition-catalog";
|
|
6
|
+
/** States which runtime data this catalog does not include. */
|
|
7
|
+
notice: string;
|
|
8
|
+
}
|
|
9
|
+
/** Lists Workspace Definitions like a catalog, and browses the refs, revisions, and files of the selected one. */
|
|
10
|
+
interface ViteHubConsoleWorkspaceBrowserView {
|
|
11
|
+
kind: "workspace-browser";
|
|
12
|
+
/** States which runtime data the browser does not include. */
|
|
13
|
+
notice: string;
|
|
14
|
+
}
|
|
15
|
+
interface ViteHubConsoleRecordColumn {
|
|
16
|
+
/** Set it for a column with few values, such as a status. The Console then filters the table by its values. */
|
|
17
|
+
filter?: boolean;
|
|
18
|
+
/** Key in `ViteHubConsoleRecord.cells`. */
|
|
19
|
+
key: string;
|
|
20
|
+
label: string;
|
|
21
|
+
}
|
|
22
|
+
/** Shows records in a table and the selected record's fields in a detail panel. */
|
|
23
|
+
interface ViteHubConsoleRecordTableView {
|
|
24
|
+
columns: readonly ViteHubConsoleRecordColumn[];
|
|
25
|
+
kind: "record-table";
|
|
26
|
+
/** States the source and limits of the records. */
|
|
27
|
+
notice: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Lists the collections and documents of an owner runtime and edits them with owner actions: a form generated from each
|
|
31
|
+
* collection's JSON Schema, a Markdown body, save, publish, and revision history.
|
|
32
|
+
*/
|
|
33
|
+
interface ViteHubConsoleDocumentEditorView {
|
|
34
|
+
kind: "document-editor";
|
|
35
|
+
/** States where documents are stored and what the editor does not do. */
|
|
36
|
+
notice: string;
|
|
37
|
+
}
|
|
38
|
+
interface ViteHubConsoleOperationColumn {
|
|
39
|
+
/** Dot path into each item of the result, such as `author.name` or `messages.0.body`. */
|
|
40
|
+
key: string;
|
|
41
|
+
label: string;
|
|
42
|
+
/**
|
|
43
|
+
* `datetime` shows a time in milliseconds since the epoch as a relative date, such as `5m ago`. `excerpt` cuts a long
|
|
44
|
+
* text. `status` shows the value as a status with a colored dot, such as `Open`. `tones` picks the color of each value.
|
|
45
|
+
* `switch` shows a boolean as a switch that calls the `switch` operation.
|
|
46
|
+
*/
|
|
47
|
+
format?: "datetime" | "excerpt" | "status" | "switch";
|
|
48
|
+
/** The color of each value of a `status` column, such as `{ open: "info", resolved: "success" }`. Other values are neutral. */
|
|
49
|
+
tones?: Readonly<Record<string, "error" | "info" | "neutral" | "success" | "warning">>;
|
|
50
|
+
/**
|
|
51
|
+
* What a `switch` column calls when the user flips it: `operation` with an input that maps each key of `input` to a
|
|
52
|
+
* dot path of the item, and the column `key` to the new value. `{ operation: "skills.set", input: { name: "name" } }`
|
|
53
|
+
* on the column `enabled` calls `skills.set` with `{ name, enabled }`, then loads the list again. A user who may only
|
|
54
|
+
* read the section sees the switch disabled.
|
|
55
|
+
*/
|
|
56
|
+
switch?: {
|
|
57
|
+
operation: string;
|
|
58
|
+
input: Readonly<Record<string, string>>;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A button on each row that calls `operation` with the Console user, then loads the list again. `input` maps each input
|
|
63
|
+
* key to a dot path of the item, as a `switch` column does.
|
|
64
|
+
*/
|
|
65
|
+
interface ViteHubConsoleOperationAction {
|
|
66
|
+
label: string;
|
|
67
|
+
operation: string;
|
|
68
|
+
input: Readonly<Record<string, string>>;
|
|
69
|
+
/**
|
|
70
|
+
* The result is `{ url }`, such as a one-time link or a provider sign-in: the Console opens it in a new tab, and loads
|
|
71
|
+
* the list again when the person comes back. The URL must be `https:`, or `http:` on a loopback host.
|
|
72
|
+
*/
|
|
73
|
+
tab?: true;
|
|
74
|
+
/** Show the button only on rows where each dot path holds one of the listed values. */
|
|
75
|
+
when?: Readonly<Record<string, readonly string[]>>;
|
|
76
|
+
/** Ask this question before the call. `{path}` takes a value from the item. */
|
|
77
|
+
confirm?: string;
|
|
78
|
+
}
|
|
79
|
+
/** A chip above the list. Choosing it merges `input` into the operation input. The first chip is the default. */
|
|
80
|
+
interface ViteHubConsoleOperationFilter {
|
|
81
|
+
id: string;
|
|
82
|
+
label: string;
|
|
83
|
+
input: Readonly<Record<string, unknown>>;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Lists the items that an operation returns, called over the Console RPC with the signed-in Console user. The operation
|
|
87
|
+
* checks the access of that user for each item, so the view shows only what the user may read. `operation` must return an
|
|
88
|
+
* array of objects with an `id`. A failure of the operation, with its `fix`, shows where the list would be.
|
|
89
|
+
*/
|
|
90
|
+
interface ViteHubConsoleOperationView {
|
|
91
|
+
/** Buttons on each row. A user who may only read the section does not see them. */
|
|
92
|
+
actions?: readonly ViteHubConsoleOperationAction[];
|
|
93
|
+
columns: readonly ViteHubConsoleOperationColumn[];
|
|
94
|
+
/**
|
|
95
|
+
* Opens the item of a row in a side panel. `comment-thread` shows an item of `comments.list` as a comment thread
|
|
96
|
+
* where the user reads, replies, resolves, reopens, and mentions people. The panel calls the `comments.*` operations
|
|
97
|
+
* with the Console user, and `?thread=<id>` in the URL opens it.
|
|
98
|
+
*/
|
|
99
|
+
detail?: {
|
|
100
|
+
kind: "comment-thread";
|
|
101
|
+
};
|
|
102
|
+
filters?: readonly ViteHubConsoleOperationFilter[];
|
|
103
|
+
kind: "operation-view";
|
|
104
|
+
/**
|
|
105
|
+
* Makes each row a link: `{path}` placeholders take values from the item and are URL-encoded, and `{path:path}` takes an
|
|
106
|
+
* app path and encodes each segment (a value that is not an app path, such as `//evil.com/x`, gives no link).
|
|
107
|
+
* `{path:href}` takes an app URL that is already encoded, with its query, as it is. Rows where `when.path` is not
|
|
108
|
+
* `when.equals` get none.
|
|
109
|
+
*/
|
|
110
|
+
link?: {
|
|
111
|
+
label?: string;
|
|
112
|
+
template: string;
|
|
113
|
+
when?: {
|
|
114
|
+
equals: string;
|
|
115
|
+
path: string;
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
notice: string;
|
|
119
|
+
/** Operation name, such as `comments.list`. */
|
|
120
|
+
operation: string;
|
|
121
|
+
/** Fetch every page with `limit` and a `before` object whose keys read these dot paths from the last item. */
|
|
122
|
+
pagination?: {
|
|
123
|
+
limit: number;
|
|
124
|
+
cursor: Readonly<Record<string, string>>;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
interface ViteHubConsoleAuthView {
|
|
128
|
+
kind: "auth-management";
|
|
129
|
+
notice: string;
|
|
130
|
+
}
|
|
131
|
+
interface ViteHubConsoleAuthSection extends ViteHubConsoleSectionDescriptor<ViteHubConsoleAuthView> {}
|
|
132
|
+
/**
|
|
133
|
+
* The webhooks of the app, their pending debounce timers, and their delivery log. The Console calls the `webhooks.*`
|
|
134
|
+
* operations of `@vite-hub/webhooks` with the Console user to list them, send a test, send a pending delivery now,
|
|
135
|
+
* cancel it, and redeliver. It has no build-time data.
|
|
136
|
+
*/
|
|
137
|
+
interface ViteHubConsoleWebhooksView {
|
|
138
|
+
kind: "webhooks";
|
|
139
|
+
notice: string;
|
|
140
|
+
}
|
|
141
|
+
type ViteHubConsoleSectionView = ViteHubConsoleOperationView | ViteHubConsoleAuthView | ViteHubConsoleWebhooksView | ViteHubConsoleDefinitionCatalogView | ViteHubConsoleRecordTableView | ViteHubConsoleDocumentEditorView | ViteHubConsoleWorkspaceBrowserView;
|
|
142
|
+
/**
|
|
143
|
+
* The overview of a document editor section: document counts by state, the changes that wait for a publish, recent
|
|
144
|
+
* activity, and a New button per collection. It reads the owner `overview` action.
|
|
145
|
+
*/
|
|
146
|
+
interface ViteHubConsoleDocumentDashboardView {
|
|
147
|
+
kind: "document-dashboard";
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The media library of a document editor section: folders, uploads, alt text, focal points, and crops of the Blob Store
|
|
151
|
+
* that `blob.serve` serves. The owner decides who may use it with `mediaAccess`, and finds the documents that use a file
|
|
152
|
+
* with `references`. The page needs the Blob section, so declare it with `requires: ["blob"]`.
|
|
153
|
+
*/
|
|
154
|
+
interface ViteHubConsoleMediaLibraryView {
|
|
155
|
+
kind: "media-library";
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The singletons of a document editor section, such as site settings: one row per singleton that opens it in the
|
|
159
|
+
* editor. Singletons appear here instead of among the collections, and the Console hides the page when the section has
|
|
160
|
+
* none.
|
|
161
|
+
*/
|
|
162
|
+
interface ViteHubConsoleDocumentSettingsView {
|
|
163
|
+
kind: "document-settings";
|
|
164
|
+
}
|
|
165
|
+
/** A view that the Console UI in `vite-hub` renders for a section page. */
|
|
166
|
+
type ViteHubConsoleSectionPageView = ViteHubConsoleDocumentDashboardView | ViteHubConsoleDocumentSettingsView | ViteHubConsoleMediaLibraryView;
|
|
167
|
+
/**
|
|
168
|
+
* A page of a section next to its main view, such as the Content dashboard or media library. The section sidebar lists
|
|
169
|
+
* the pages, and each page has its own URL:
|
|
170
|
+
*
|
|
171
|
+
* - `/_vitehub/<section>` serves the `index` page, or the main view when the section has none.
|
|
172
|
+
* - `/_vitehub/<section>/-/<page>` serves every other page. The `-` segment keeps page URLs apart from the main view's
|
|
173
|
+
* own segments, such as a collection name.
|
|
174
|
+
*
|
|
175
|
+
* Pages share the access of their section: `console.authorize` that allows the section allows every page of it.
|
|
176
|
+
*/
|
|
177
|
+
interface ViteHubConsoleSectionPage {
|
|
178
|
+
/** Lowercase route segment, for example `media`. */
|
|
179
|
+
id: string;
|
|
180
|
+
/** Display name, for example `Media`. */
|
|
181
|
+
label: string;
|
|
182
|
+
/** Iconify class name, for example `i-lucide-image`. */
|
|
183
|
+
icon: string;
|
|
184
|
+
view: ViteHubConsoleSectionPageView;
|
|
185
|
+
/**
|
|
186
|
+
* Serve this page at the section root. At most one page sets it, and `/_vitehub/<section>/-/<id>` then redirects to
|
|
187
|
+
* the root.
|
|
188
|
+
*/
|
|
189
|
+
index?: boolean;
|
|
190
|
+
/**
|
|
191
|
+
* Sidebar heading, for example `Assets`. Pages without a group lead the sidebar, above the main view's navigation.
|
|
192
|
+
* Grouped pages follow it, in the order their group first appears.
|
|
193
|
+
*/
|
|
194
|
+
group?: string;
|
|
195
|
+
/** Section ids that the project must enable, such as `["blob"]`. The Console leaves the page out otherwise. */
|
|
196
|
+
requires?: readonly string[];
|
|
197
|
+
}
|
|
198
|
+
/** Serializable description of a Console section. The Console UI in `vite-hub` renders the view. */
|
|
199
|
+
interface ViteHubConsoleSectionDescriptor<TView extends ViteHubConsoleSectionView = ViteHubConsoleSectionView> {
|
|
200
|
+
description: string;
|
|
201
|
+
/**
|
|
202
|
+
* Iconify class name, for example `i-lucide-inbox`. The Console draws with Lucide; `ph` also resolves. The
|
|
203
|
+
* Console build bundles the icon of every section in the `vite-hub` Console registry and fails if it cannot resolve one.
|
|
204
|
+
*/
|
|
205
|
+
icon: string;
|
|
206
|
+
/** Lowercase route segment, for example `queues`. The Console serves the section at `/_vitehub/<id>`. */
|
|
207
|
+
id: string;
|
|
208
|
+
/** Plural display name, for example `Queues`. */
|
|
209
|
+
label: string;
|
|
210
|
+
view: TView;
|
|
211
|
+
/** Pages next to the main view, in sidebar order. Only document editor sections render pages today. */
|
|
212
|
+
pages?: readonly ViteHubConsoleSectionPage[];
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
|
3
215
|
//#region src/console.d.ts
|
|
4
216
|
/** The owner descriptor. The host supplies the navigation and renders the Auth views. */
|
|
5
217
|
export declare const authConsoleSection: ViteHubConsoleAuthSection;
|
|
6
218
|
//#endregion
|
|
7
|
-
export { type AuthConsoleAction, type AuthConsoleActivity, type AuthConsoleActivityQuery, type AuthConsoleAgent, type AuthConsoleCapability, type AuthConsoleColumn, type AuthConsoleDescription, type AuthConsoleEvent, type AuthConsoleEventGroup, type AuthConsoleEventUser, type AuthConsoleField, type AuthConsoleFieldPolicy, type AuthConsoleFieldType, type AuthConsoleHostOptions, type AuthConsoleImpersonation, type AuthConsoleList, type AuthConsoleMutation, type AuthConsoleOptions, type AuthConsoleOrganization, type AuthConsoleOrganizationDetail, type AuthConsoleOverview, type AuthConsolePage, type AuthConsolePolicy, type AuthConsoleQuery, type AuthConsoleSession, type AuthConsoleUser, type AuthConsoleUserDetail, type AuthConsoleValue, type authConsoleActionNames, authConsoleEventGroups };
|
|
219
|
+
export { type AuthConsoleAction, type AuthConsoleActivity, type AuthConsoleActivityQuery, type AuthConsoleAgent, type AuthConsoleCapability, type AuthConsoleColumn, type AuthConsoleDescription, type AuthConsoleEvent, type AuthConsoleEventGroup, type AuthConsoleEventUser, type AuthConsoleField, type AuthConsoleFieldPolicy, type AuthConsoleFieldType, type AuthConsoleHostOptions, type AuthConsoleHostSession, type AuthConsoleHostUserInput, type AuthConsoleImpersonation, type AuthConsoleList, type AuthConsoleMutation, type AuthConsoleOptions, type AuthConsoleOrganization, type AuthConsoleOrganizationDetail, type AuthConsoleOverview, type AuthConsolePage, type AuthConsolePolicy, type AuthConsoleQuery, type AuthConsoleRoles, type AuthConsoleSession, type AuthConsoleSessionUser, type AuthConsoleUser, type AuthConsoleUserDetail, type AuthConsoleValue, type authConsoleActionNames, authConsoleEventGroups };
|
package/dist/console.js
CHANGED
|
@@ -4,7 +4,7 @@ import { n as authConsoleEventGroups } from "./console-types-CmmnKGMx.js";
|
|
|
4
4
|
const authConsoleSection = {
|
|
5
5
|
id: "auth",
|
|
6
6
|
label: "Auth",
|
|
7
|
-
icon: "i-
|
|
7
|
+
icon: "i-vitehub-auth",
|
|
8
8
|
description: "Manage users, sessions, organizations, and approved OAuth clients.",
|
|
9
9
|
view: {
|
|
10
10
|
kind: "auth-management",
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { t as authErrorDiagnostics } from "./error-diagnostics-C8XtVz0i.js";
|
|
2
|
+
//#region src/database-options.ts
|
|
3
|
+
/** Reads the adapter's target without loading Better Auth or the Database runtime. */
|
|
4
|
+
function authDatabaseName(database) {
|
|
5
|
+
if (typeof database !== "function") return;
|
|
6
|
+
const name = Reflect.get(database, "vitehubDatabase");
|
|
7
|
+
return typeof name === "string" ? name : void 0;
|
|
8
|
+
}
|
|
9
|
+
/** The owned schema has text IDs and references, with no database-generated default. */
|
|
10
|
+
function assertAuthDatabaseIdStrategy(generateId) {
|
|
11
|
+
if (generateId === "serial" || generateId === "uuid" || generateId === false) throw authErrorDiagnostics.AUTH_C0053({ message: "`authDatabase()` owns tables with text IDs. Use Better Auth's default ID generator or a custom function returning strings; `advanced.database.generateId` cannot be `serial`, `uuid`, or `false`. Use another Better Auth adapter to own a different ID schema." });
|
|
12
|
+
}
|
|
13
|
+
/** The Auth Vite plugin reads `additionalFields` from the Definition to add their columns, so they must be static. */
|
|
14
|
+
function authDatabaseAdditionalFieldsError(detail) {
|
|
15
|
+
return authErrorDiagnostics.AUTH_C0055({ message: `\`authDatabase()\` adds a column for each additional field, but ${detail}.` });
|
|
16
|
+
}
|
|
17
|
+
/** The Better Auth options need a table, a column, or a name that the Database of `authDatabase()` does not have. */
|
|
18
|
+
function authDatabaseSchemaError(detail) {
|
|
19
|
+
return authErrorDiagnostics.AUTH_C0056({ message: `\`authDatabase()\` cannot store the Better Auth options: ${detail}.` });
|
|
20
|
+
}
|
|
21
|
+
function authDatabasePlacementError() {
|
|
22
|
+
return authErrorDiagnostics.AUTH_C0054({ message: "`authDatabase()` needs one statically invariant Database: call it inline as `defineAuth()`'s `database`, in an object or a callback's single unconditional returned object. Do not select it in branches." });
|
|
23
|
+
}
|
|
24
|
+
/** Outside a development server, Auth keeps users in memory only when the Definition asks for it. */
|
|
25
|
+
function authMemoryDatabaseError() {
|
|
26
|
+
return authErrorDiagnostics.AUTH_C0063({ message: "`defineAuth()` has no `database` or `preset`. Auth keeps users and sessions in memory only on a development server, because a restart or a second instance loses them." });
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { authDatabaseSchemaError as a, authDatabasePlacementError as i, authDatabaseAdditionalFieldsError as n, authMemoryDatabaseError as o, authDatabaseName as r, assertAuthDatabaseIdStrategy as t };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { a as authDatabaseSchemaError } from "./database-options-BRNef9oy.js";
|
|
2
|
+
import { getAuthTables } from "better-auth/db";
|
|
3
|
+
//#region src/database-schema-options.ts
|
|
4
|
+
/** Better Auth reads each table by its key and each column by its field key, so the options must not rename them. */
|
|
5
|
+
function assertAuthDatabaseNames(options) {
|
|
6
|
+
for (const [model, table] of Object.entries(getAuthTables(options))) {
|
|
7
|
+
if (table.modelName !== model) throw authDatabaseSchemaError(`${model}.modelName cannot be ${JSON.stringify(table.modelName)}, because the table keeps the key \`${model}\``);
|
|
8
|
+
for (const [key, field] of Object.entries(table.fields)) if (field.fieldName && field.fieldName !== key) throw authDatabaseSchemaError(`${model}.${key} cannot map to ${JSON.stringify(field.fieldName)}, because the column keeps the key \`${key}\``);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { assertAuthDatabaseNames as t };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { f as AuthAppDatabase } from "./types-CNaeH2Fd.js";
|
|
2
|
+
//#region src/database.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* The Better Auth `database` adapter for the app Database `name` (default `"default"`). Pass it inline as the
|
|
5
|
+
* `database` of `defineAuth()`. Auth then adds the Better Auth tables, with the fields of `admin()`, the
|
|
6
|
+
* Console MCP OAuth tables, the table of Better Auth's limiter, the `additionalFields` of the Definition, and the tables of
|
|
7
|
+
* the plugins that it calls, such as `twoFactor()` and `apiKey()`, to that Database. The adapter uses the Database of each
|
|
8
|
+
* request: SQLite, libSQL, Cloudflare D1, or Postgres.
|
|
9
|
+
*/
|
|
10
|
+
export declare function authDatabase(name?: string): AuthAppDatabase;
|
|
11
|
+
//#endregion
|
|
12
|
+
export type { AuthAppDatabase };
|
package/dist/database.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { t as authErrorDiagnostics } from "./error-diagnostics-C8XtVz0i.js";
|
|
2
|
+
import { a as authDatabaseSchemaError, t as assertAuthDatabaseIdStrategy } from "./database-options-BRNef9oy.js";
|
|
3
|
+
import { t as assertAuthDatabaseNames } from "./database-schema-options-ENuRMduM.js";
|
|
4
|
+
import { getAuthTables } from "better-auth/db";
|
|
5
|
+
import { getTableColumns, is, isTable } from "drizzle-orm";
|
|
6
|
+
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
|
7
|
+
import { databases } from "@vite-hub/database/runtime/registry";
|
|
8
|
+
import { PgAsyncDatabase } from "drizzle-orm/pg-core";
|
|
9
|
+
//#region src/database.ts
|
|
10
|
+
/**
|
|
11
|
+
* Compares the tables and fields that the options need, with the plugins and additional fields, against the Drizzle
|
|
12
|
+
* tables of the Database, before the adapter opens it.
|
|
13
|
+
*/
|
|
14
|
+
function assertAuthDatabaseSchema(options, schema, database) {
|
|
15
|
+
const fix = "Call the plugin in `server/auth.ts` and write `additionalFields` inline there, so that the Auth Vite plugin adds the columns. Then run `vitehub database generate` and apply the migration";
|
|
16
|
+
for (const [model, table] of Object.entries(getAuthTables(options))) {
|
|
17
|
+
const drizzleTable = Object.hasOwn(schema, model) ? schema[model] : void 0;
|
|
18
|
+
if (!isTable(drizzleTable)) throw authDatabaseSchemaError(`the ${JSON.stringify(database)} Database has no \`${model}\` table. ${fix}`);
|
|
19
|
+
const columns = getTableColumns(drizzleTable);
|
|
20
|
+
for (const key of Object.keys(table.fields)) if (!Object.hasOwn(columns, key)) throw authDatabaseSchemaError(`the \`${model}\` table of the ${JSON.stringify(database)} Database has no \`${key}\` column. ${fix}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The Better Auth `database` adapter for the app Database `name` (default `"default"`). Pass it inline as the
|
|
25
|
+
* `database` of `defineAuth()`. Auth then adds the Better Auth tables, with the fields of `admin()`, the
|
|
26
|
+
* Console MCP OAuth tables, the table of Better Auth's limiter, the `additionalFields` of the Definition, and the tables of
|
|
27
|
+
* the plugins that it calls, such as `twoFactor()` and `apiKey()`, to that Database. The adapter uses the Database of each
|
|
28
|
+
* request: SQLite, libSQL, Cloudflare D1, or Postgres.
|
|
29
|
+
*/
|
|
30
|
+
function authDatabase(name = "default") {
|
|
31
|
+
const adapter = (options) => {
|
|
32
|
+
assertAuthDatabaseIdStrategy(options.advanced?.database?.generateId);
|
|
33
|
+
assertAuthDatabaseNames(options);
|
|
34
|
+
const entry = Object.hasOwn(databases, name) ? databases[name] : void 0;
|
|
35
|
+
if (!entry || !Object.hasOwn(entry.schema, "user")) throw authErrorDiagnostics.AUTH_R0019({ message: `[vitehub] \`authDatabase(${JSON.stringify(name)})\` found no Better Auth tables in the ${JSON.stringify(name)} Database. Pass \`authDatabase()\` inline as the \`database\` of \`defineAuth()\` so that Auth adds them, enable the Database integration, and run \`vitehub database generate\`. \`vitehub inspect definitions --kind database\` lists the tables of each Database.` });
|
|
36
|
+
assertAuthDatabaseSchema(options, entry.schema, name);
|
|
37
|
+
return drizzleAdapter(entry.db, {
|
|
38
|
+
provider: is(entry.db, PgAsyncDatabase) ? "pg" : "sqlite",
|
|
39
|
+
schema: entry.schema
|
|
40
|
+
})(options);
|
|
41
|
+
};
|
|
42
|
+
return Object.assign(adapter, { vitehubDatabase: name });
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { authDatabase };
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { t as authErrorDiagnostics } from "./error-diagnostics-C8XtVz0i.js";
|
|
2
|
+
import { t as isRecord } from "./object-Da1zyPCR.js";
|
|
3
|
+
import { t as accessRouteSyntaxError } from "./application-path-DuTpfGFj.js";
|
|
4
|
+
import { t as normalizeAuthBasePath } from "./shared-BwcdRGxD.js";
|
|
5
|
+
import { r as authDatabaseName } from "./database-options-BRNef9oy.js";
|
|
6
|
+
//#region src/stateless-user-id.ts
|
|
7
|
+
const pendingUserIds = /* @__PURE__ */ new WeakMap();
|
|
8
|
+
/** The user ID of one provider account: the first 32 hex digits of SHA-256 over the provider ID and the account ID. */
|
|
9
|
+
async function statelessUserId(providerId, accountId) {
|
|
10
|
+
const input = new TextEncoder().encode(JSON.stringify([
|
|
11
|
+
"vitehub:auth:user",
|
|
12
|
+
providerId,
|
|
13
|
+
accountId
|
|
14
|
+
]));
|
|
15
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", input));
|
|
16
|
+
return Array.from(digest.slice(0, 16), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
17
|
+
}
|
|
18
|
+
/** The account ID that Better Auth stores for this provider profile, from the provider's own `accountSubject`. */
|
|
19
|
+
async function accountSubject(context, providerId, profile) {
|
|
20
|
+
const subject = await context.context.socialProviders.find((candidate) => candidate.id === providerId)?.accountSubject({
|
|
21
|
+
profile,
|
|
22
|
+
tokens: {}
|
|
23
|
+
});
|
|
24
|
+
return subject === void 0 || subject === null || String(subject).trim() === "" ? void 0 : String(subject);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Without a database, Better Auth creates a new user at each sign-in. These hooks give that user an ID derived from
|
|
28
|
+
* the provider account, so the same account gets the same `user.id` on every sign-in, instance, and restart.
|
|
29
|
+
* They wrap the application's own `user.validateUserInfo` and `databaseHooks.user.create.before`.
|
|
30
|
+
*/
|
|
31
|
+
function withStatelessUserId(options) {
|
|
32
|
+
const user = options.user ?? {};
|
|
33
|
+
const databaseHooks = options.databaseHooks ?? {};
|
|
34
|
+
const validateUserInfo = user.validateUserInfo;
|
|
35
|
+
const createBefore = databaseHooks.user?.create?.before;
|
|
36
|
+
const validate = async (data, context) => {
|
|
37
|
+
const { source } = data;
|
|
38
|
+
if (source.action === "create-user" && source.method === "oauth" && source.oauth) {
|
|
39
|
+
const accountId = await accountSubject(context, source.oauth.providerId, source.oauth.profile ?? {});
|
|
40
|
+
if (accountId === void 0) return {
|
|
41
|
+
error: "vitehub_account_subject_missing",
|
|
42
|
+
errorDescription: "The provider did not return an account ID."
|
|
43
|
+
};
|
|
44
|
+
pendingUserIds.set(context, await statelessUserId(source.oauth.providerId, accountId));
|
|
45
|
+
}
|
|
46
|
+
return validateUserInfo?.(data, context);
|
|
47
|
+
};
|
|
48
|
+
const before = async (data, context) => {
|
|
49
|
+
const result = createBefore ? await createBefore(data, context) : void 0;
|
|
50
|
+
const id = context ? pendingUserIds.get(context) : void 0;
|
|
51
|
+
if (result === false || id === void 0) return result;
|
|
52
|
+
if (context) pendingUserIds.delete(context);
|
|
53
|
+
return { data: {
|
|
54
|
+
...typeof result === "object" ? result.data : {},
|
|
55
|
+
id
|
|
56
|
+
} };
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
databaseHooks: {
|
|
60
|
+
...databaseHooks,
|
|
61
|
+
user: {
|
|
62
|
+
...databaseHooks.user,
|
|
63
|
+
create: {
|
|
64
|
+
...databaseHooks.user?.create,
|
|
65
|
+
before
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
user: {
|
|
70
|
+
...user,
|
|
71
|
+
validateUserInfo: validate
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/preset-options.ts
|
|
77
|
+
const verificationPluginIds = /* @__PURE__ */ new Set([
|
|
78
|
+
"email-otp",
|
|
79
|
+
"magic-link",
|
|
80
|
+
"one-time-token"
|
|
81
|
+
]);
|
|
82
|
+
const statelessOwnedOptions = [
|
|
83
|
+
["session", "expiresIn"],
|
|
84
|
+
[
|
|
85
|
+
"session",
|
|
86
|
+
"cookieCache",
|
|
87
|
+
"enabled"
|
|
88
|
+
],
|
|
89
|
+
[
|
|
90
|
+
"session",
|
|
91
|
+
"cookieCache",
|
|
92
|
+
"maxAge"
|
|
93
|
+
],
|
|
94
|
+
[
|
|
95
|
+
"session",
|
|
96
|
+
"cookieCache",
|
|
97
|
+
"refreshCache"
|
|
98
|
+
],
|
|
99
|
+
[
|
|
100
|
+
"session",
|
|
101
|
+
"cookieCache",
|
|
102
|
+
"strategy"
|
|
103
|
+
],
|
|
104
|
+
["account", "storeAccountCookie"],
|
|
105
|
+
["account", "storeStateStrategy"]
|
|
106
|
+
];
|
|
107
|
+
function readPath(value, path) {
|
|
108
|
+
let current = value;
|
|
109
|
+
for (const key of path) {
|
|
110
|
+
if (!isRecord(current) || !Object.hasOwn(current, key)) return void 0;
|
|
111
|
+
current = current[key];
|
|
112
|
+
}
|
|
113
|
+
return current;
|
|
114
|
+
}
|
|
115
|
+
function readStatelessPreset(value) {
|
|
116
|
+
if (isRecord(value) && value.name === "stateless" && typeof value.maxAge === "number" && Number.isSafeInteger(value.maxAge) && value.maxAge > 0) return {
|
|
117
|
+
name: "stateless",
|
|
118
|
+
maxAge: value.maxAge
|
|
119
|
+
};
|
|
120
|
+
throw authErrorDiagnostics.AUTH_C0043({ message: "`defineAuth()` preset must be a preset such as `stateless()` from `vite-hub/auth/presets`." });
|
|
121
|
+
}
|
|
122
|
+
function storedPluginId(plugin) {
|
|
123
|
+
if (!isRecord(plugin)) return;
|
|
124
|
+
const id = typeof plugin.id === "string" ? plugin.id : "unknown";
|
|
125
|
+
if (isRecord(plugin.schema) && Object.keys(plugin.schema).length > 0) return id;
|
|
126
|
+
if (verificationPluginIds.has(id)) return id;
|
|
127
|
+
}
|
|
128
|
+
/** Checks that the options can run with `stateless()` and returns the Better Auth options it sets. */
|
|
129
|
+
function applyStatelessPreset(preset, options) {
|
|
130
|
+
if (options.database !== void 0) throw authErrorDiagnostics.AUTH_C0044({ message: "`stateless()` keeps sessions in cookies and cannot use `database`. Remove `database`, or remove the preset to store sessions in a database." });
|
|
131
|
+
if (options.secondaryStorage !== void 0) throw authErrorDiagnostics.AUTH_C0045({ message: "`stateless()` keeps sessions in cookies and cannot use `secondaryStorage`. Remove `secondaryStorage`, or remove the preset to store sessions on the server." });
|
|
132
|
+
if (readPath(options, ["emailAndPassword", "enabled"]) === true) throw authErrorDiagnostics.AUTH_C0046({ message: "`stateless()` has no database to store users and passwords, so `emailAndPassword` cannot be enabled. Use `socialProviders`, or remove the preset and configure a database." });
|
|
133
|
+
const plugin = Array.isArray(options.plugins) ? options.plugins.map(storedPluginId).find(Boolean) : void 0;
|
|
134
|
+
if (plugin) throw authErrorDiagnostics.AUTH_C0047({ message: `\`stateless()\` has no database, and the Better Auth plugin "${plugin}" stores data in one. Remove the plugin, or remove the preset and configure a database.` });
|
|
135
|
+
const owned = statelessOwnedOptions.find((path) => readPath(options, path) !== void 0);
|
|
136
|
+
if (owned) throw authErrorDiagnostics.AUTH_C0048({ message: `\`stateless()\` sets \`${owned.join(".")}\`. Remove it from the Auth Definition, and pass \`maxAge\` to \`stateless()\` to change the session lifetime.` });
|
|
137
|
+
const session = isRecord(options.session) ? options.session : {};
|
|
138
|
+
const cookieCache = isRecord(session.cookieCache) ? session.cookieCache : {};
|
|
139
|
+
const account = isRecord(options.account) ? options.account : {};
|
|
140
|
+
return {
|
|
141
|
+
...options,
|
|
142
|
+
...withStatelessUserId(options),
|
|
143
|
+
account: {
|
|
144
|
+
...account,
|
|
145
|
+
storeAccountCookie: true,
|
|
146
|
+
storeStateStrategy: "cookie"
|
|
147
|
+
},
|
|
148
|
+
session: {
|
|
149
|
+
...session,
|
|
150
|
+
cookieCache: {
|
|
151
|
+
...cookieCache,
|
|
152
|
+
enabled: true,
|
|
153
|
+
maxAge: preset.maxAge,
|
|
154
|
+
refreshCache: true,
|
|
155
|
+
strategy: "jwe"
|
|
156
|
+
},
|
|
157
|
+
expiresIn: preset.maxAge
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/** Validates the `preset` option and returns the options with the preset's Better Auth options applied. */
|
|
162
|
+
function applyAuthPreset(options) {
|
|
163
|
+
if (options.preset === void 0) return options;
|
|
164
|
+
const { preset, ...rest } = options;
|
|
165
|
+
return applyStatelessPreset(readStatelessPreset(preset), rest);
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/events-config.ts
|
|
169
|
+
function validateAuthEvents(value) {
|
|
170
|
+
if (value === void 0 || value === false) return;
|
|
171
|
+
if (!value || typeof value !== "object" || !("store" in value) || !value.store || typeof value.store !== "object" || ![
|
|
172
|
+
"append",
|
|
173
|
+
"list",
|
|
174
|
+
"aggregate"
|
|
175
|
+
].every((key) => typeof Reflect.get(value.store, key) === "function") || "authorize" in value && value.authorize !== void 0 && typeof value.authorize !== "function" || "onEvent" in value && value.onEvent !== void 0 && typeof value.onEvent !== "function") throw authErrorDiagnostics.AUTH_C0050({ message: "`defineAuth()` events needs a store with append, list, and aggregate methods. events.authorize and events.onEvent must be functions. Set events to false to disable the log." });
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/definition.ts
|
|
179
|
+
const authRuntimeOptionNames = /* @__PURE__ */ new Set([
|
|
180
|
+
"baseURL",
|
|
181
|
+
"secret",
|
|
182
|
+
"secrets"
|
|
183
|
+
]);
|
|
184
|
+
const accessRouteKeys = /* @__PURE__ */ new Set([
|
|
185
|
+
"authorize",
|
|
186
|
+
"method",
|
|
187
|
+
"route"
|
|
188
|
+
]);
|
|
189
|
+
function validateAuthDatabase(value) {
|
|
190
|
+
if (typeof value === "undefined" || value === "memory" || authDatabaseName(value) !== void 0) return;
|
|
191
|
+
throw authErrorDiagnostics.AUTH_C0021({ message: "`defineAuth()` database must be `authDatabase()` from `vite-hub/auth/database` or `\"memory\"`. `true` and `{ name }` do not create storage. Return another Better Auth adapter from the Definition callback." });
|
|
192
|
+
}
|
|
193
|
+
/** `runtime` was a second way to resolve options. Its shallow merge replaced whole objects such as `advanced`. */
|
|
194
|
+
function assertNoRuntimeOption(options) {
|
|
195
|
+
if (!Object.hasOwn(options, "runtime")) return;
|
|
196
|
+
throw authErrorDiagnostics.AUTH_C0039({ message: "`defineAuth()` no longer takes `runtime`. Pass one callback that returns all options, so no option replaces another." });
|
|
197
|
+
}
|
|
198
|
+
function validateAuthSecondaryStorage(value) {
|
|
199
|
+
if (typeof value === "undefined") return;
|
|
200
|
+
throw authErrorDiagnostics.AUTH_C0024({ message: "`defineAuth()` secondaryStorage takes only a Better Auth adapter, from the Definition callback. `true` and `{ store }` do not create storage." });
|
|
201
|
+
}
|
|
202
|
+
function validateStringValue(value, label) {
|
|
203
|
+
if (typeof value !== "string" || value.trim().length === 0) throw authErrorDiagnostics.AUTH_C0026({ message: `\`defineAuth()\` ${label} must be a non-empty string.` });
|
|
204
|
+
}
|
|
205
|
+
function validateRouteValue(value, label) {
|
|
206
|
+
validateStringValue(value, label);
|
|
207
|
+
if (typeof value !== "string") return;
|
|
208
|
+
if (!value.startsWith("/")) throw authErrorDiagnostics.AUTH_C0027({ message: `\`defineAuth()\` ${label} must start with \`/\`.` });
|
|
209
|
+
const syntaxError = accessRouteSyntaxError(value, label);
|
|
210
|
+
if (syntaxError) throw authErrorDiagnostics.AUTH_C0068({ message: syntaxError });
|
|
211
|
+
}
|
|
212
|
+
function validateAuthSignIn(value) {
|
|
213
|
+
if (typeof value === "undefined") return;
|
|
214
|
+
if (!isRecord(value)) throw authErrorDiagnostics.AUTH_C0028({ message: "`defineAuth()` access.signIn must be an object." });
|
|
215
|
+
validateStringValue(value.provider, "access.signIn.provider");
|
|
216
|
+
if (typeof value.callbackURL !== "undefined") validateStringValue(value.callbackURL, "access.signIn.callbackURL");
|
|
217
|
+
if (typeof value.errorCallbackURL !== "undefined") validateStringValue(value.errorCallbackURL, "access.signIn.errorCallbackURL");
|
|
218
|
+
if (typeof value.requestSignUp !== "undefined" && typeof value.requestSignUp !== "boolean") throw authErrorDiagnostics.AUTH_C0029({ message: "`defineAuth()` access.signIn.requestSignUp must be a boolean." });
|
|
219
|
+
if (typeof value.scopes !== "undefined" && (!Array.isArray(value.scopes) || value.scopes.some((scope) => typeof scope !== "string"))) throw authErrorDiagnostics.AUTH_C0030({ message: "`defineAuth()` access.signIn.scopes must be an array of strings." });
|
|
220
|
+
}
|
|
221
|
+
function validateAuthAccessRoute(value, label) {
|
|
222
|
+
if (typeof value === "string") {
|
|
223
|
+
validateRouteValue(value, label);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (!isRecord(value)) throw authErrorDiagnostics.AUTH_C0031({ message: `\`defineAuth()\` ${label} must be a route string or route object.` });
|
|
227
|
+
const unknownKey = Object.keys(value).find((key) => !accessRouteKeys.has(key));
|
|
228
|
+
if (unknownKey) throw authErrorDiagnostics.AUTH_C0032({ message: `\`defineAuth()\` ${label} does not support the "${unknownKey}" option.` });
|
|
229
|
+
validateRouteValue(value.route, `${label}.route`);
|
|
230
|
+
if (typeof value.authorize !== "undefined" && typeof value.authorize !== "function") throw authErrorDiagnostics.AUTH_C0033({ message: `\`defineAuth()\` ${label}.authorize must be a function.` });
|
|
231
|
+
if (typeof value.method !== "undefined") validateStringValue(value.method, `${label}.method`);
|
|
232
|
+
}
|
|
233
|
+
function validateAuthAccess(value) {
|
|
234
|
+
if (typeof value === "undefined") return;
|
|
235
|
+
if (!isRecord(value)) throw authErrorDiagnostics.AUTH_C0034({ message: "`defineAuth()` access must be an object." });
|
|
236
|
+
const access = value;
|
|
237
|
+
if (typeof access.routes !== "undefined") {
|
|
238
|
+
if (!Array.isArray(access.routes)) throw authErrorDiagnostics.AUTH_C0035({ message: "`defineAuth()` access.routes must be an array." });
|
|
239
|
+
access.routes.forEach((route, index) => validateAuthAccessRoute(route, `access.routes[${index}]`));
|
|
240
|
+
}
|
|
241
|
+
validateAuthAccessPublic(access.public);
|
|
242
|
+
validateAuthAccessPolicy(access.policy);
|
|
243
|
+
validateAuthSignIn(access.signIn);
|
|
244
|
+
}
|
|
245
|
+
function validateAuthAccessPublic(value) {
|
|
246
|
+
if (typeof value === "undefined") return;
|
|
247
|
+
if (!Array.isArray(value) || Array.from(value).some((route) => typeof route !== "string" || !route.startsWith("/"))) throw authErrorDiagnostics.AUTH_C0064({ message: "`defineAuth()` access.public must be an array of routes that start with `/`." });
|
|
248
|
+
}
|
|
249
|
+
/** Also checked when a Definition callback returns the options. */
|
|
250
|
+
function validateAuthAccessPolicy(value) {
|
|
251
|
+
if (typeof value === "undefined") return;
|
|
252
|
+
const recheck = isRecord(value) ? value.recheck : void 0;
|
|
253
|
+
if (!isRecord(value) || typeof value.allow !== "function" || typeof recheck !== "undefined" && (typeof recheck !== "number" || !Number.isFinite(recheck) || recheck < 0)) throw authErrorDiagnostics.AUTH_C0065({ message: "`defineAuth()` access.policy must be an object with an `allow` function and an optional `recheck` of 0 or more milliseconds." });
|
|
254
|
+
}
|
|
255
|
+
function defineAuth(options) {
|
|
256
|
+
return defineAuthOptions(options);
|
|
257
|
+
}
|
|
258
|
+
function defineAuthOptions(options) {
|
|
259
|
+
if (typeof options === "function") return { options };
|
|
260
|
+
if (!isRecord(options)) throw authErrorDiagnostics.AUTH_C0036({ message: "`defineAuth()` expects an object or a callback." });
|
|
261
|
+
const viteHubOptions = options;
|
|
262
|
+
const runtimeOption = Object.keys(options).find((key) => authRuntimeOptionNames.has(key));
|
|
263
|
+
if (runtimeOption) throw authErrorDiagnostics.AUTH_C0037({ message: `\`defineAuth()\` ${runtimeOption} is resolved at runtime and cannot be defined in the Auth Definition.` });
|
|
264
|
+
if (typeof viteHubOptions.route !== "undefined" && viteHubOptions.route !== false) throw authErrorDiagnostics.AUTH_C0038({ message: "`defineAuth()` route can only be `false` when provided." });
|
|
265
|
+
assertNoRuntimeOption(options);
|
|
266
|
+
if (typeof viteHubOptions.basePath !== "undefined") {
|
|
267
|
+
if (typeof viteHubOptions.basePath !== "string") throw authErrorDiagnostics.AUTH_C0040({ message: "`defineAuth()` basePath must be a string." });
|
|
268
|
+
normalizeAuthBasePath(viteHubOptions.basePath);
|
|
269
|
+
}
|
|
270
|
+
const session = options.session;
|
|
271
|
+
if (isRecord(session) && typeof session.onRefresh !== "undefined" && typeof session.onRefresh !== "function") throw authErrorDiagnostics.AUTH_C0041({ message: "`defineAuth()` session.onRefresh must be a function." });
|
|
272
|
+
validateAuthEvents(options.events);
|
|
273
|
+
validateAuthDatabase(viteHubOptions.database);
|
|
274
|
+
validateAuthSecondaryStorage(viteHubOptions.secondaryStorage);
|
|
275
|
+
validateAuthAccess(viteHubOptions.access);
|
|
276
|
+
applyAuthPreset(options);
|
|
277
|
+
return { options };
|
|
278
|
+
}
|
|
279
|
+
//#endregion
|
|
280
|
+
export { applyAuthPreset as a, validateAuthEvents as i, defineAuth as n, validateAuthAccessPolicy as r, assertNoRuntimeOption as t };
|