@vite-hub/auth 0.0.4 → 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.
Files changed (87) hide show
  1. package/README.md +443 -32
  2. package/dist/abilities-DfAnFYwG.d.ts +93 -0
  3. package/dist/abilities.d.ts +2 -0
  4. package/dist/abilities.js +43 -0
  5. package/dist/ability-error-CqgBrBfR.js +91 -0
  6. package/dist/ability-error-DcgChwCN.d.ts +34 -0
  7. package/dist/admin-claim-code-CDipqeAU.js +22 -0
  8. package/dist/agent.d.ts +23 -12
  9. package/dist/agent.js +30 -8
  10. package/dist/application-path-DuTpfGFj.js +62 -0
  11. package/dist/auth-instance-CR6b1W80.js +1168 -0
  12. package/dist/bun-sqlite.d.ts +8 -0
  13. package/dist/cli-ubYJpAL_.d.ts +163 -0
  14. package/dist/cli.d.ts +7 -0
  15. package/dist/cli.js +108 -0
  16. package/dist/console-runtime-DTopBP-x.d.ts +71 -0
  17. package/dist/console-runtime-J9_Vx9pT.js +1166 -0
  18. package/dist/console-runtime.d.ts +3 -0
  19. package/dist/console-runtime.js +2 -0
  20. package/dist/console-types-CmmnKGMx.js +38 -0
  21. package/dist/console.d.ts +219 -0
  22. package/dist/console.js +15 -0
  23. package/dist/database-options-BRNef9oy.js +29 -0
  24. package/dist/database-schema-options-ENuRMduM.js +12 -0
  25. package/dist/database.d.ts +12 -0
  26. package/dist/database.js +45 -0
  27. package/dist/definition-CqQMcDEK.js +280 -0
  28. package/dist/error-diagnostics-C8XtVz0i.js +138 -0
  29. package/dist/events-ByzenEVj.js +314 -0
  30. package/dist/forward.d.ts +50 -0
  31. package/dist/forward.js +120 -0
  32. package/dist/host-declarations.d.ts +6 -1
  33. package/dist/index.d.ts +26 -5
  34. package/dist/index.js +3 -95
  35. package/dist/internal/server.d.ts +5 -0
  36. package/dist/internal/server.js +858 -0
  37. package/dist/invariant-D8_xBqmZ.js +22 -0
  38. package/dist/mcp.d.ts +187 -0
  39. package/dist/mcp.js +353 -0
  40. package/dist/members-D9lfaEO_.js +533 -0
  41. package/dist/nuxt-bridge-Cl988STZ.js +44 -0
  42. package/dist/nuxt.d.ts +36 -7
  43. package/dist/nuxt.js +116 -7
  44. package/dist/object-Da1zyPCR.js +11 -0
  45. package/dist/page-tools-shared-l4eyj53D.js +19 -0
  46. package/dist/presets-BGZi6Dfu.d.ts +25 -0
  47. package/dist/presets.d.ts +2 -0
  48. package/dist/presets.js +32 -0
  49. package/dist/request-z6DHHmrb.js +47 -0
  50. package/dist/runtime/console.d.ts +3 -0
  51. package/dist/runtime/console.js +4 -0
  52. package/dist/runtime/empty-abilities.d.ts +5 -0
  53. package/dist/runtime/empty-abilities.js +4 -0
  54. package/dist/runtime/empty-client.d.ts +6 -0
  55. package/dist/runtime/empty-client.js +5 -0
  56. package/dist/runtime/empty-definition.d.ts +1 -2
  57. package/dist/runtime/nuxt.d.ts +2 -3
  58. package/dist/runtime/nuxt.js +3 -2
  59. package/dist/runtime/server.d.ts +2 -0
  60. package/dist/runtime/server.js +77 -0
  61. package/dist/runtime/vue-nuxt.d.ts +4 -0
  62. package/dist/runtime/vue-nuxt.js +160 -0
  63. package/dist/runtime-state-ojQCzKOc.js +25 -0
  64. package/dist/schema.d.ts +137 -0
  65. package/dist/schema.js +147 -0
  66. package/dist/server-C0qnKN2W.d.ts +143 -0
  67. package/dist/server-bQGzVQ--.d.ts +41 -0
  68. package/dist/server.d.ts +3 -22
  69. package/dist/server.js +3 -2
  70. package/dist/session-cookies-BbH3UR2m.js +227 -0
  71. package/dist/set-cookie-C8NOhdRE.js +90 -0
  72. package/dist/shared-BwcdRGxD.js +12 -0
  73. package/dist/table-specs-8gq_FCaq.js +771 -0
  74. package/dist/table-specs-DbXyaXWs.d.ts +842 -0
  75. package/dist/test.d.ts +26 -0
  76. package/dist/test.js +25 -0
  77. package/dist/types-CNaeH2Fd.d.ts +569 -0
  78. package/dist/vite-CV400XXY.js +1556 -0
  79. package/dist/vite.d.ts +145 -18
  80. package/dist/vite.js +2 -2
  81. package/dist/vue.d.ts +343 -17
  82. package/dist/vue.js +640 -15
  83. package/package.json +156 -27
  84. package/dist/server-BZn8iQrB.js +0 -370
  85. package/dist/shared-QSYBHaMG.js +0 -12
  86. package/dist/types-jaRlW3D1.d.ts +0 -117
  87. package/dist/vite-Cmey8ODS.js +0 -1018
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "@vite-hub/auth",
3
- "version": "0.0.4",
3
+ "version": "0.3.0",
4
4
  "description": "Better Auth-powered authentication primitives and Vite integration for ViteHub.",
5
5
  "keywords": [
6
6
  "auth",
7
7
  "authentication",
8
8
  "better-auth",
9
+ "nuxt",
10
+ "nuxt-module",
11
+ "session",
12
+ "typescript",
9
13
  "vite",
10
14
  "vitehub"
11
15
  ],
12
- "homepage": "https://github.com/vite-hub/vitehub#readme",
16
+ "homepage": "https://vitehub.dev",
13
17
  "bugs": "https://github.com/vite-hub/vitehub/issues",
14
18
  "license": "Apache-2.0",
15
19
  "author": "onmax <maximogarciamtnez@gmail.com>",
@@ -26,52 +30,177 @@
26
30
  "sideEffects": false,
27
31
  "types": "./dist/index.d.ts",
28
32
  "imports": {
33
+ "#vitehub/auth/abilities": {
34
+ "types": "./dist/runtime/empty-abilities.d.ts",
35
+ "default": "./dist/runtime/empty-abilities.js"
36
+ },
37
+ "#vitehub/auth/client": {
38
+ "types": "./dist/runtime/empty-client.d.ts",
39
+ "default": "./dist/runtime/empty-client.js"
40
+ },
29
41
  "#vitehub/auth/definition": {
30
42
  "types": "./dist/runtime/empty-definition.d.ts",
31
43
  "default": "./dist/runtime/empty-definition.js"
32
44
  }
33
45
  },
34
46
  "exports": {
35
- ".": "./dist/index.js",
36
- "./agent": "./dist/agent.js",
37
- "./nuxt": "./dist/nuxt.js",
38
- "./server": "./dist/server.js",
39
- "./vite": "./dist/vite.js",
40
- "./vue": "./dist/vue.js",
47
+ ".": {
48
+ "types": "./dist/index.d.ts",
49
+ "import": "./dist/index.js",
50
+ "default": "./dist/index.js"
51
+ },
52
+ "./abilities": {
53
+ "types": "./dist/abilities.d.ts",
54
+ "import": "./dist/abilities.js",
55
+ "default": "./dist/abilities.js"
56
+ },
57
+ "./agent": {
58
+ "types": "./dist/agent.d.ts",
59
+ "import": "./dist/agent.js",
60
+ "default": "./dist/agent.js"
61
+ },
62
+ "./cli": {
63
+ "types": "./dist/cli.d.ts",
64
+ "import": "./dist/cli.js",
65
+ "default": "./dist/cli.js"
66
+ },
67
+ "./console": {
68
+ "types": "./dist/console.d.ts",
69
+ "import": "./dist/console.js",
70
+ "default": "./dist/console.js"
71
+ },
72
+ "./console-runtime": {
73
+ "types": "./dist/console-runtime.d.ts",
74
+ "import": "./dist/console-runtime.js",
75
+ "default": "./dist/console-runtime.js"
76
+ },
77
+ "./database": {
78
+ "types": "./dist/database.d.ts",
79
+ "import": "./dist/database.js",
80
+ "default": "./dist/database.js"
81
+ },
82
+ "./forward": {
83
+ "types": "./dist/forward.d.ts",
84
+ "import": "./dist/forward.js",
85
+ "default": "./dist/forward.js"
86
+ },
87
+ "./internal/server": {
88
+ "types": "./dist/internal/server.d.ts",
89
+ "import": "./dist/internal/server.js",
90
+ "default": "./dist/internal/server.js"
91
+ },
92
+ "./mcp": {
93
+ "types": "./dist/mcp.d.ts",
94
+ "import": "./dist/mcp.js",
95
+ "default": "./dist/mcp.js"
96
+ },
97
+ "./nuxt": {
98
+ "types": "./dist/nuxt.d.ts",
99
+ "import": "./dist/nuxt.js",
100
+ "default": "./dist/nuxt.js"
101
+ },
102
+ "./presets": {
103
+ "types": "./dist/presets.d.ts",
104
+ "import": "./dist/presets.js",
105
+ "default": "./dist/presets.js"
106
+ },
107
+ "./runtime/console": {
108
+ "types": "./dist/runtime/console.d.ts",
109
+ "import": "./dist/runtime/console.js",
110
+ "default": "./dist/runtime/console.js"
111
+ },
112
+ "./runtime/server": {
113
+ "types": "./dist/runtime/server.d.ts",
114
+ "import": "./dist/runtime/server.js",
115
+ "default": "./dist/runtime/server.js"
116
+ },
117
+ "./schema": {
118
+ "types": "./dist/schema.d.ts",
119
+ "import": "./dist/schema.js",
120
+ "default": "./dist/schema.js"
121
+ },
122
+ "./server": {
123
+ "types": "./dist/server.d.ts",
124
+ "import": "./dist/server.js",
125
+ "default": "./dist/server.js"
126
+ },
127
+ "./test": {
128
+ "types": "./dist/test.d.ts",
129
+ "import": "./dist/test.js",
130
+ "default": "./dist/test.js"
131
+ },
132
+ "./vite": {
133
+ "types": "./dist/vite.d.ts",
134
+ "import": "./dist/vite.js",
135
+ "default": "./dist/vite.js"
136
+ },
137
+ "./vue": {
138
+ "types": "./dist/vue.d.ts",
139
+ "import": "./dist/vue.js",
140
+ "default": "./dist/vue.js"
141
+ },
41
142
  "./package.json": "./package.json"
42
143
  },
43
144
  "dependencies": {
44
- "@cloudflare/workers-types": "^4.20260509.1",
45
- "@types/node": "^24.13.3",
46
- "@vite-hub/env": "0.0.4",
47
- "@vite-hub/runtime": "0.0.4",
48
- "better-auth": "^1.6.23",
49
- "bun-types": "^1.3.10"
145
+ "@noble/hashes": "^2.2.0",
146
+ "@types/node": "^24.19.1",
147
+ "@vite-hub/env": "0.3.0",
148
+ "@vite-hub/runtime": "0.3.0",
149
+ "@vite-hub/web-container": "0.3.0",
150
+ "better-auth": "^1.7.15",
151
+ "h3": "^2.0.8",
152
+ "nostics": "1.4.0",
153
+ "rou3": "^1.0.0",
154
+ "valibot": "^1.5.0"
50
155
  },
51
156
  "devDependencies": {
157
+ "@better-auth/api-key": "1.7.15",
158
+ "@better-auth/oauth-provider": "^1.7.15",
159
+ "@better-auth/passkey": "1.7.15",
160
+ "@better-auth/stripe": "1.7.15",
161
+ "@cloudflare/workers-types": "^5.20261016.1",
52
162
  "@types/json-schema": "^7.0.15",
53
163
  "@types/mdast": "^4.0.4",
54
- "@vite-hub/agent": "0.0.4",
55
- "@types/node": "^24.13.3",
56
- "publint": "^0.3.21",
164
+ "@types/node": "^24.19.1",
165
+ "@vite-hub/agent": "0.3.0",
166
+ "@vite-hub/database": "0.3.0",
167
+ "@vite-hub/internal": "0.0.1",
168
+ "bun-types": "^1.4.3",
169
+ "happy-dom": "20.14.5",
170
+ "miniflare": "4.20260730.0",
171
+ "nitro": "^3.0.261103-rc",
172
+ "publint": "^0.3.25",
173
+ "stripe": "23.1.0",
57
174
  "typescript": "^6.0.3",
58
- "vite": "^8.0.8",
59
- "vite-plus": "^0.1.24",
60
- "vitest": "^4.1.4",
61
- "vue": "^3.5.0",
62
- "@vite-hub/internal": "0.0.1"
175
+ "undici": "8.12.2",
176
+ "vite": "^8.2.2",
177
+ "vite-plus": "^0.3.3",
178
+ "vitest": "^4.1.11",
179
+ "vue": "3.5.44",
180
+ "drizzle-kit": "1.0.0-rc.5",
181
+ "drizzle-orm": "1.0.0-rc.5"
63
182
  },
64
183
  "peerDependencies": {
184
+ "@better-auth/oauth-provider": "^1.7.15",
65
185
  "@types/json-schema": "^7.0.15",
66
186
  "@types/mdast": "^4.0.4",
67
- "@vite-hub/agent": "0.0.4",
187
+ "@vite-hub/agent": "0.3.0",
188
+ "@vite-hub/database": "0.3.0",
189
+ "nitro": "3.0.261102-rc || ^3.0.261103-rc",
68
190
  "vite": ">=8.0.0",
69
- "vue": "^3.5.0"
191
+ "vue": "^3.5.0",
192
+ "drizzle-orm": "1.0.0-rc.5 || ^1.0.0"
70
193
  },
71
194
  "peerDependenciesMeta": {
195
+ "@better-auth/oauth-provider": {
196
+ "optional": true
197
+ },
72
198
  "@vite-hub/agent": {
73
199
  "optional": true
74
200
  },
201
+ "@vite-hub/database": {
202
+ "optional": true
203
+ },
75
204
  "vite": {
76
205
  "optional": true
77
206
  },
@@ -80,12 +209,12 @@
80
209
  }
81
210
  },
82
211
  "engines": {
83
- "node": ">=24.0.0"
212
+ "node": ">=22.18.0"
84
213
  },
85
214
  "scripts": {
86
215
  "build": "vp pack",
87
216
  "dev": "vp pack --watch",
88
- "typecheck": "tsc --noEmit -p ./tsconfig.json",
89
- "test": "vp run -t @vite-hub/auth#build && vp test"
217
+ "typecheck": "vp run --cache -t @vite-hub/auth#build && node ../../test/package-typecheck.mjs --noEmit -p ./tsconfig.json",
218
+ "test": "vp run --cache -t @vite-hub/auth#build && vp test"
90
219
  }
91
220
  }
@@ -1,370 +0,0 @@
1
- import { n as normalizeAuthBasePath } from "./shared-QSYBHaMG.js";
2
- import discoveredDefinition from "#vitehub/auth/definition";
3
- import { betterAuth } from "better-auth";
4
- import { ViteHubError, isViteHubError } from "@vite-hub/runtime";
5
- //#region src/errors.ts
6
- function invalidAuthenticationErrorOptions() {
7
- throw new TypeError("[vitehub] Invalid authentication error options.");
8
- }
9
- function readAuthenticationErrorOption(value, key) {
10
- if (typeof value !== "object" || value === null) invalidAuthenticationErrorOptions();
11
- try {
12
- if (Array.isArray(value)) invalidAuthenticationErrorOptions();
13
- const descriptor = Object.getOwnPropertyDescriptor(value, key);
14
- if (descriptor === void 0) return void 0;
15
- if (!("value" in descriptor)) invalidAuthenticationErrorOptions();
16
- return descriptor.value;
17
- } catch {
18
- invalidAuthenticationErrorOptions();
19
- }
20
- }
21
- function createAuthenticationProviderError(options) {
22
- const cause = readAuthenticationErrorOption(options, "cause");
23
- const operation = readAuthenticationErrorOption(options, "operation");
24
- if (operation !== "get-auth-for-request" && operation !== "get-session") invalidAuthenticationErrorOptions();
25
- return new ViteHubError("AUTH_PROVIDER_OPERATION_FAILED", "[vitehub] Authentication provider operation failed.", {
26
- cause,
27
- details: {
28
- operation,
29
- provider: "better-auth"
30
- }
31
- });
32
- }
33
- function throwAuthenticationProviderError(cause, operation) {
34
- if (isViteHubError(cause) || isAuthenticationAbortError(cause)) throw cause;
35
- throw createAuthenticationProviderError({
36
- cause,
37
- operation
38
- });
39
- }
40
- function isAuthenticationAbortError(error) {
41
- if (typeof error !== "object" || error === null) return false;
42
- try {
43
- return Reflect.get(error, "name") === "AbortError";
44
- } catch {
45
- return false;
46
- }
47
- }
48
- //#endregion
49
- //#region src/session.ts
50
- async function getAuthenticationSession(auth, input) {
51
- const [api, getSession] = await readProvider(() => {
52
- const api = readRecordProperty(auth, "api");
53
- return [api, api && readProperty(api, "getSession")];
54
- });
55
- if (typeof getSession !== "function") throw new TypeError("Better Auth did not expose api.getSession().");
56
- const value = await readProvider(() => Reflect.apply(getSession, api, [input]));
57
- if (value == null) return value;
58
- const session = await readProvider(() => resolveAuthenticationSession(value));
59
- if (!session) throw new TypeError("Better Auth returned an invalid session response.");
60
- return session;
61
- }
62
- async function readProvider(read) {
63
- try {
64
- return await read();
65
- } catch (cause) {
66
- throwAuthenticationProviderError(cause, "get-session");
67
- }
68
- }
69
- function readRecordProperty(value, property) {
70
- if (!isRecord(value)) return;
71
- const nested = readProperty(value, property);
72
- return isRecord(nested) ? nested : void 0;
73
- }
74
- function resolveAuthenticationSession(value) {
75
- if (!isRecord(value)) return;
76
- const session = value.session;
77
- const user = value.user;
78
- if (!isRecord(session) || !isRecord(user)) return;
79
- const userId = user.id;
80
- if (typeof userId !== "string" || userId.length === 0) return;
81
- return {
82
- auth: value,
83
- session,
84
- user,
85
- userId
86
- };
87
- }
88
- function readProperty(value, property) {
89
- return value[property];
90
- }
91
- function isRecord(value) {
92
- return typeof value === "object" && value !== null && !Array.isArray(value);
93
- }
94
- //#endregion
95
- //#region src/server.ts
96
- function hasRuntimeOptions(options) {
97
- return Boolean(options && Object.keys(options).length > 0);
98
- }
99
- function hasRequestRuntimeOptions(definition) {
100
- if (typeof definition.options === "function") return true;
101
- return typeof definition.options.runtime === "function";
102
- }
103
- let authRuntimeEnvResolver;
104
- function setAuthRuntimeEnvResolver(resolver) {
105
- authRuntimeEnvResolver = resolver;
106
- }
107
- function resolveAuthRuntimeEnv(event) {
108
- return authRuntimeEnvResolver?.(event) ?? {};
109
- }
110
- function requestOrigin(request) {
111
- return request ? new URL(request.url).origin : "http://localhost";
112
- }
113
- function createAuthRuntimeContext(request, event) {
114
- return {
115
- env: resolveAuthRuntimeEnv(event ?? request),
116
- ...request ? { request } : {},
117
- requestOrigin: requestOrigin(request)
118
- };
119
- }
120
- function isPlainObject(value) {
121
- return Boolean(value) && typeof value === "object" && !Array.isArray(value);
122
- }
123
- function isAuthDatabaseMetadata(value) {
124
- return value === true || isPlainObject(value) && typeof value.name === "string" && Object.keys(value).every((key) => key === "dedicated" || key === "name");
125
- }
126
- function isAuthSecondaryStorageMetadata(value) {
127
- return value === true || isPlainObject(value) && typeof value.store === "string" && Object.keys(value).every((key) => key === "store");
128
- }
129
- function resolveDefinitionOptions(definition, request, event, runtimeOptions = {}) {
130
- const context = createAuthRuntimeContext(request, event);
131
- const options = typeof definition.options === "function" ? definition.options(context) : definition.options;
132
- const runtime = options.runtime;
133
- const resolvedRuntime = !runtime ? {} : typeof runtime === "function" ? runtime(context) : runtime;
134
- return {
135
- ...options,
136
- ...resolvedRuntime,
137
- ...runtimeOptions
138
- };
139
- }
140
- function resolveRequestRuntimeOptions(definition, request, event) {
141
- if (typeof definition.options === "function") return resolveDefinitionOptions(definition, request, event);
142
- const runtime = definition.options.runtime;
143
- if (!runtime) return {};
144
- const context = createAuthRuntimeContext(request, event);
145
- return typeof runtime === "function" ? runtime(context) : runtime;
146
- }
147
- function hasStaticTrustedOrigins(definition) {
148
- return typeof definition.options !== "function" && "trustedOrigins" in definition.options;
149
- }
150
- function definitionBasePath(definition) {
151
- if (typeof definition.options === "function") return;
152
- return definition.options.basePath;
153
- }
154
- function stripViteHubOptions(options) {
155
- const { access: _access, database, route: _route, runtime: _runtime, secondaryStorage, ...rest } = options;
156
- return {
157
- ...rest,
158
- ...!isAuthDatabaseMetadata(database) ? { database } : {},
159
- ...!isAuthSecondaryStorageMetadata(secondaryStorage) ? { secondaryStorage } : {},
160
- basePath: normalizeAuthBasePath(typeof options.basePath === "string" ? options.basePath : void 0)
161
- };
162
- }
163
- function hasTrustedOrigins(options) {
164
- return "trustedOrigins" in options;
165
- }
166
- function toRequest(request) {
167
- if (request instanceof Request) return request;
168
- const init = {
169
- headers: request.headers,
170
- method: request.method,
171
- signal: request.signal
172
- };
173
- if (request.body) {
174
- init.body = request.body;
175
- init.duplex = "half";
176
- }
177
- return new Request(request.url, init);
178
- }
179
- function unwrapAuthRequest(input) {
180
- return "req" in input ? input.req : input;
181
- }
182
- function createAuthRequestRuntimeOptions(definition, request, runtimeOptions = {}, event) {
183
- const requestRuntimeOptions = {
184
- ...resolveRequestRuntimeOptions(definition, request, event),
185
- ...runtimeOptions
186
- };
187
- const baseURL = requestRuntimeOptions.baseURL || new URL(request.url).origin;
188
- return {
189
- baseURL,
190
- ...!hasTrustedOrigins(requestRuntimeOptions) && !hasStaticTrustedOrigins(definition) ? { trustedOrigins: [baseURL] } : {},
191
- ...requestRuntimeOptions
192
- };
193
- }
194
- const authRuntimeStateKey = Symbol.for("vitehub.auth.runtime");
195
- function getAuthRuntimeState() {
196
- const globalScope = globalThis;
197
- globalScope[authRuntimeStateKey] ??= {};
198
- return globalScope[authRuntimeStateKey];
199
- }
200
- function resolveDefaultDefinition() {
201
- if (!discoveredDefinition) throw new Error("[vitehub] No Auth Definition was discovered. Add `server/auth.ts` or `server.auth.ts`.");
202
- return discoveredDefinition;
203
- }
204
- function createBetterAuthOptions(definition, runtimeOptions = {}) {
205
- return stripViteHubOptions(resolveDefinitionOptions(definition, void 0, void 0, runtimeOptions));
206
- }
207
- function createBetterAuthOptionsFromResolved(options) {
208
- return stripViteHubOptions(options);
209
- }
210
- function createAuthenticationProvider(options) {
211
- try {
212
- return betterAuth(options);
213
- } catch (cause) {
214
- throwAuthenticationProviderError(cause, "get-auth-for-request");
215
- }
216
- }
217
- function resolveBetterAuthOptionsForRequest(definition, request, runtimeOptions, event) {
218
- return createBetterAuthOptionsFromResolved(resolveDefinitionOptions(definition, request, event, createAuthRequestRuntimeOptions(definition, request, runtimeOptions, event)));
219
- }
220
- function createAuth(definition, runtimeOptions) {
221
- return betterAuth(createBetterAuthOptions(definition, runtimeOptions));
222
- }
223
- function createAuthForRequest(definition, request, runtimeOptions, event) {
224
- return betterAuth(resolveBetterAuthOptionsForRequest(definition, request, runtimeOptions, event));
225
- }
226
- function handleAuthRequest(definition, request, runtimeOptions, event) {
227
- return createAuthForRequest(definition, request, runtimeOptions, event).handler(toRequest(request));
228
- }
229
- function createAuthHandler(definition, runtimeOptions) {
230
- return createAuth(definition, runtimeOptions).handler;
231
- }
232
- function resetAuth() {
233
- const state = getAuthRuntimeState();
234
- state.auth = void 0;
235
- state.definition = void 0;
236
- }
237
- function getAuthForDefinition(definition, runtimeOptions) {
238
- if (hasRuntimeOptions(runtimeOptions)) return createAuthenticationProvider(createBetterAuthOptions(definition, runtimeOptions));
239
- const state = getAuthRuntimeState();
240
- if (!state.auth || state.definition !== definition) {
241
- state.auth = createAuthenticationProvider(createBetterAuthOptions(definition));
242
- state.definition = definition;
243
- }
244
- return state.auth;
245
- }
246
- function getAuth(runtimeOptions) {
247
- return getAuthForDefinition(resolveDefaultDefinition(), runtimeOptions);
248
- }
249
- function getAuthForRequest(request, runtimeOptions, event) {
250
- const definition = resolveDefaultDefinition();
251
- if (!hasRequestRuntimeOptions(definition) && !hasRuntimeOptions(runtimeOptions)) return getAuthForDefinition(definition);
252
- return createAuthenticationProvider(resolveBetterAuthOptionsForRequest(definition, request, runtimeOptions, event));
253
- }
254
- async function assertAuthOrigin(request, event) {
255
- const origin = request.headers.get("origin");
256
- if (!origin) throw new TypeError("The request origin is required.");
257
- const auth = getAuthForRequest(request, void 0, event);
258
- if (!(await auth.$context).isTrustedOrigin(origin)) throw new TypeError("The request origin is not trusted.");
259
- return auth;
260
- }
261
- function handleAuth(input, runtimeOptions) {
262
- const request = unwrapAuthRequest(input);
263
- return handleAuthRequest(resolveDefaultDefinition(), request, runtimeOptions, input);
264
- }
265
- function authRequestBasePath(definition, request, event) {
266
- const options = createAuthRequestRuntimeOptions(definition, request, {}, event);
267
- return normalizeAuthBasePath(typeof options.basePath === "string" ? options.basePath : definitionBasePath(definition));
268
- }
269
- function authBaseURL(definition, request, event) {
270
- return new URL(createAuthRequestRuntimeOptions(definition, request, {}, event).baseURL).origin;
271
- }
272
- function wantsHtml(request) {
273
- return request.method === "GET" && request.headers.get("accept")?.includes("text/html") === true;
274
- }
275
- function createForbiddenResponse(request) {
276
- return wantsHtml(request) ? new Response("Forbidden.", {
277
- headers: { "content-type": "text/plain; charset=utf-8" },
278
- status: 403
279
- }) : Response.json({ error: "Forbidden." }, { status: 403 });
280
- }
281
- function authAccessRoutes(definition, request, event, routeIndexes) {
282
- const routes = resolveDefinitionOptions(definition, request, event).access?.routes;
283
- return routeIndexes.map((routeIndex) => {
284
- const route = routes?.[routeIndex];
285
- if (!route) throw new TypeError(`[vitehub] Auth access route ${routeIndex} is unavailable at runtime.`);
286
- return route;
287
- });
288
- }
289
- async function createSignInResponse(definition, request, signIn, event) {
290
- const origin = authBaseURL(definition, request, event);
291
- const response = await handleAuthRequest(definition, new Request(`${origin}${authRequestBasePath(definition, request, event)}/sign-in/social`, {
292
- body: JSON.stringify({
293
- callbackURL: signIn.callbackURL,
294
- errorCallbackURL: signIn.errorCallbackURL,
295
- provider: signIn.provider,
296
- requestSignUp: signIn.requestSignUp,
297
- scopes: signIn.scopes
298
- }),
299
- headers: {
300
- "content-type": "application/json",
301
- "accept": "application/json",
302
- "origin": origin
303
- },
304
- method: "POST"
305
- }), void 0, event);
306
- if (!response.ok) return response;
307
- const body = await response.json().catch(() => void 0);
308
- if (typeof body?.url !== "string") return Response.json({ error: "Auth sign-in did not return a redirect URL." }, { status: 502 });
309
- const headers = new Headers(response.headers);
310
- headers.delete("content-type");
311
- headers.set("location", body.url);
312
- return new Response(null, {
313
- headers,
314
- status: 302
315
- });
316
- }
317
- async function requireAuthRequest(input, definition, routeIndexes, requiredAuthorizeRouteIndexes = []) {
318
- const request = unwrapAuthRequest(input);
319
- const session = await getAuthenticationSession(createAuthenticationProvider(resolveBetterAuthOptionsForRequest(definition, request, void 0, input)), { headers: request.headers });
320
- if (session) {
321
- if (routeIndexes === void 0) return;
322
- const context = {
323
- request,
324
- session: session.session,
325
- user: session.user
326
- };
327
- const requiredAuthorizeRoutes = new Set(requiredAuthorizeRouteIndexes);
328
- const routes = authAccessRoutes(definition, request, input, routeIndexes);
329
- for (const [index, route] of routes.entries()) {
330
- const authorize = route instanceof Object ? route.authorize : void 0;
331
- if (!authorize && requiredAuthorizeRoutes.has(routeIndexes[index])) return createForbiddenResponse(request);
332
- if (!authorize) continue;
333
- const result = await authorize(context);
334
- if (result instanceof Response) return result;
335
- if (result !== true) return createForbiddenResponse(request);
336
- }
337
- return;
338
- }
339
- if (!wantsHtml(request)) return Response.json({ error: "Unauthorized." }, { status: 401 });
340
- if (new URL(request.url).searchParams.has("auth_error")) return new Response("Unauthorized.", {
341
- headers: { "content-type": "text/plain; charset=utf-8" },
342
- status: 403
343
- });
344
- const signIn = resolveDefinitionOptions(definition, request, input).access?.signIn;
345
- return signIn ? await createSignInResponse(definition, request, signIn, input) : Response.json({ error: "Unauthorized." }, { status: 401 });
346
- }
347
- async function requireAuth(input, definition = resolveDefaultDefinition()) {
348
- return requireAuthRequest(input, definition);
349
- }
350
- async function requireAuthAccessRoutes(input, routeIndexes, definition = resolveDefaultDefinition(), requiredAuthorizeRouteIndexes = []) {
351
- if (!Array.isArray(routeIndexes) || routeIndexes.length === 0 || routeIndexes.some((routeIndex) => !Number.isSafeInteger(routeIndex) || routeIndex < 0)) throw new TypeError("[vitehub] Auth access route indexes must be a non-empty array of non-negative integers.");
352
- if (!Array.isArray(requiredAuthorizeRouteIndexes) || requiredAuthorizeRouteIndexes.some((routeIndex) => !Number.isSafeInteger(routeIndex) || routeIndex < 0)) throw new TypeError("[vitehub] Required Auth authorize route indexes must be an array of non-negative integers.");
353
- return requireAuthRequest(input, definition, routeIndexes, requiredAuthorizeRouteIndexes);
354
- }
355
- const auth = new Proxy({}, {
356
- get(_target, property, receiver) {
357
- return Reflect.get(getAuth(), property, receiver);
358
- },
359
- getOwnPropertyDescriptor(_target, property) {
360
- return Reflect.getOwnPropertyDescriptor(getAuth(), property);
361
- },
362
- has(_target, property) {
363
- return Reflect.has(getAuth(), property);
364
- },
365
- ownKeys() {
366
- return Reflect.ownKeys(getAuth());
367
- }
368
- });
369
- //#endregion
370
- export { getAuthenticationSession as _, createAuthHandler as a, getAuth as c, handleAuth as d, handleAuthRequest as f, setAuthRuntimeEnvResolver as g, resetAuth as h, createAuthForRequest as i, getAuthForDefinition as l, requireAuthAccessRoutes as m, auth as n, createAuthRequestRuntimeOptions as o, requireAuth as p, createAuth as r, createBetterAuthOptions as s, assertAuthOrigin as t, getAuthForRequest as u, invalidAuthenticationErrorOptions as v, throwAuthenticationProviderError as y };
@@ -1,12 +0,0 @@
1
- //#region src/shared.ts
2
- const defaultAuthBasePath = "/api/auth";
3
- function normalizeAuthBasePath(value) {
4
- const path = value?.trim() || defaultAuthBasePath;
5
- if (!path.startsWith("/")) throw new TypeError("`defineAuth()` basePath must start with `/`.");
6
- return path.length > 1 ? path.replace(/\/+$/g, "") : path;
7
- }
8
- function isAuthRequestPath(pathname, basePath) {
9
- return pathname === basePath || pathname.startsWith(`${basePath}/`);
10
- }
11
- //#endregion
12
- export { normalizeAuthBasePath as n, isAuthRequestPath as t };