hylekit 1.0.3 → 1.0.4
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 +126 -59
- package/dist/bff/index.cjs +5 -139
- package/dist/bff/index.cjs.map +1 -1
- package/dist/bff/index.d.cts +2 -1
- package/dist/bff/index.d.ts +2 -1
- package/dist/bff/index.js +5 -145
- package/dist/bff/index.js.map +1 -1
- package/dist/client/index.cjs +58 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +1530 -0
- package/dist/client/index.d.ts +1530 -0
- package/dist/client/index.js +31 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/nextjs.cjs +3 -214
- package/dist/client/nextjs.cjs.map +1 -1
- package/dist/client/nextjs.d.cts +4 -97
- package/dist/client/nextjs.d.ts +4 -97
- package/dist/client/nextjs.js +2 -218
- package/dist/client/nextjs.js.map +1 -1
- package/dist/client/sveltekit.cjs +3 -201
- package/dist/client/sveltekit.cjs.map +1 -1
- package/dist/client/sveltekit.d.cts +3 -72
- package/dist/client/sveltekit.d.ts +3 -72
- package/dist/client/sveltekit.js +2 -205
- package/dist/client/sveltekit.js.map +1 -1
- package/dist/{index-DYW73KK3.d.cts → index-B-5TEr94.d.cts} +28 -2
- package/dist/{index-DYW73KK3.d.ts → index-B-5TEr94.d.ts} +28 -2
- package/dist/index.cjs +276 -262
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +110 -3272
- package/dist/index.d.ts +110 -3272
- package/dist/index.js +270 -258
- package/dist/index.js.map +1 -1
- package/dist/server/express.cjs +71 -80
- package/dist/server/express.cjs.map +1 -1
- package/dist/server/express.d.cts +18 -6
- package/dist/server/express.d.ts +18 -6
- package/dist/server/express.js +70 -86
- package/dist/server/express.js.map +1 -1
- package/dist/server/nextjs.cjs +107 -0
- package/dist/server/nextjs.cjs.map +1 -0
- package/dist/server/nextjs.d.cts +90 -0
- package/dist/server/nextjs.d.ts +90 -0
- package/dist/server/nextjs.js +82 -0
- package/dist/server/nextjs.js.map +1 -0
- package/dist/server/sveltekit.cjs +94 -0
- package/dist/server/sveltekit.cjs.map +1 -0
- package/dist/server/sveltekit.d.cts +68 -0
- package/dist/server/sveltekit.d.ts +68 -0
- package/dist/server/sveltekit.js +69 -0
- package/dist/server/sveltekit.js.map +1 -0
- package/dist/{types-GOn9sn7-.d.ts → types-Wucl0qmN.d.cts} +1 -8
- package/dist/{types-BHiK1JUX.d.cts → types-Wucl0qmN.d.ts} +1 -8
- package/package.json +22 -6
package/dist/index.cjs
CHANGED
|
@@ -21,18 +21,20 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
account: () => account,
|
|
24
|
-
auth: () => auth,
|
|
25
24
|
bff: () => bff,
|
|
26
|
-
|
|
25
|
+
createAuth: () => createAuth,
|
|
26
|
+
createDb: () => createDb,
|
|
27
|
+
createExpressMiddleware: () => createExpressMiddleware,
|
|
27
28
|
createNextJsBff: () => createNextJsBff,
|
|
29
|
+
createNextJsServer: () => createNextJsServer,
|
|
28
30
|
createSvelteKitBff: () => createSvelteKitBff,
|
|
29
|
-
|
|
31
|
+
createSvelteKitServer: () => createSvelteKitServer,
|
|
30
32
|
default: () => src_default,
|
|
31
33
|
express: () => expressAdapter,
|
|
32
34
|
expressMiddleware: () => middleware,
|
|
33
35
|
getAuthContext: () => getAuthContext,
|
|
34
36
|
isAuthenticated: () => isAuthenticated,
|
|
35
|
-
|
|
37
|
+
schema: () => schema_exports,
|
|
36
38
|
session: () => session,
|
|
37
39
|
user: () => user,
|
|
38
40
|
verification: () => verification
|
|
@@ -40,13 +42,10 @@ __export(src_exports, {
|
|
|
40
42
|
module.exports = __toCommonJS(src_exports);
|
|
41
43
|
|
|
42
44
|
// src/lib/auth.ts
|
|
45
|
+
var import_server_only = require("server-only");
|
|
43
46
|
var import_better_auth = require("better-auth");
|
|
44
47
|
var import_drizzle = require("better-auth/adapters/drizzle");
|
|
45
48
|
|
|
46
|
-
// src/lib/db.ts
|
|
47
|
-
var import_libsql = require("drizzle-orm/libsql");
|
|
48
|
-
var import_client = require("@libsql/client");
|
|
49
|
-
|
|
50
49
|
// src/lib/schema.ts
|
|
51
50
|
var schema_exports = {};
|
|
52
51
|
__export(schema_exports, {
|
|
@@ -135,209 +134,211 @@ var accountRelations = (0, import_drizzle_orm.relations)(account, ({ one }) => (
|
|
|
135
134
|
})
|
|
136
135
|
}));
|
|
137
136
|
|
|
138
|
-
// src/lib/db.ts
|
|
139
|
-
var client = (0, import_client.createClient)({
|
|
140
|
-
url: process.env.HYLE_DATABASE_URL,
|
|
141
|
-
authToken: process.env.HYLE_DATABASE_AUTH_TOKEN
|
|
142
|
-
});
|
|
143
|
-
var db = (0, import_libsql.drizzle)(client, { schema: schema_exports });
|
|
144
|
-
|
|
145
137
|
// src/lib/auth.ts
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
google: {
|
|
158
|
-
clientId: process.env.GOOGLE_CLIENT_ID || "",
|
|
159
|
-
clientSecret: process.env.GOOGLE_CLIENT_SECRET || ""
|
|
160
|
-
}
|
|
161
|
-
},
|
|
162
|
-
session: {
|
|
163
|
-
expiresIn: 60 * 60 * 24 * 7,
|
|
138
|
+
function assertServerOnly(configName) {
|
|
139
|
+
if (typeof window !== "undefined") {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`[hylekit] SECURITY ERROR: "${configName}" contains secrets and must not be used on the client side. Only call ${configName}() in server-side code (e.g., API routes, server components, +page.server.ts).`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function createAuth(db, config) {
|
|
146
|
+
assertServerOnly("createAuth");
|
|
147
|
+
const sessionConfig = {
|
|
148
|
+
expiresIn: config.session?.expiresIn ?? 60 * 60 * 24 * 7,
|
|
164
149
|
// 7 days
|
|
165
|
-
updateAge: 60 * 60 * 24,
|
|
166
|
-
//
|
|
150
|
+
updateAge: config.session?.updateAge ?? 60 * 60 * 24,
|
|
151
|
+
// 24 hours
|
|
167
152
|
cookieCache: {
|
|
168
|
-
enabled: true,
|
|
169
|
-
maxAge: 60 * 5
|
|
153
|
+
enabled: config.session?.cookieCache?.enabled ?? true,
|
|
154
|
+
maxAge: config.session?.cookieCache?.maxAge ?? 60 * 5
|
|
170
155
|
// 5 minutes
|
|
171
156
|
}
|
|
157
|
+
};
|
|
158
|
+
return (0, import_better_auth.betterAuth)({
|
|
159
|
+
database: (0, import_drizzle.drizzleAdapter)(db, {
|
|
160
|
+
provider: "sqlite",
|
|
161
|
+
schema: {
|
|
162
|
+
...schema_exports
|
|
163
|
+
}
|
|
164
|
+
}),
|
|
165
|
+
baseURL: config.baseURL,
|
|
166
|
+
secret: config.secret,
|
|
167
|
+
trustedOrigins: config.trustedOrigins,
|
|
168
|
+
socialProviders: config.google ? {
|
|
169
|
+
google: {
|
|
170
|
+
clientId: config.google.clientId,
|
|
171
|
+
clientSecret: config.google.clientSecret
|
|
172
|
+
}
|
|
173
|
+
} : void 0,
|
|
174
|
+
session: sessionConfig
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// src/lib/db.ts
|
|
179
|
+
var import_server_only2 = require("server-only");
|
|
180
|
+
var import_libsql = require("drizzle-orm/libsql");
|
|
181
|
+
var import_client = require("@libsql/client");
|
|
182
|
+
function assertServerOnly2(configName) {
|
|
183
|
+
if (typeof window !== "undefined") {
|
|
184
|
+
throw new Error(
|
|
185
|
+
`[hylekit] SECURITY ERROR: "${configName}" contains secrets and must not be used on the client side. Only call ${configName}() in server-side code (e.g., API routes, server components, +page.server.ts).`
|
|
186
|
+
);
|
|
172
187
|
}
|
|
173
|
-
}
|
|
188
|
+
}
|
|
189
|
+
function createDb(config) {
|
|
190
|
+
assertServerOnly2("createDb");
|
|
191
|
+
const client = (0, import_client.createClient)({
|
|
192
|
+
url: config.url,
|
|
193
|
+
authToken: config.authToken
|
|
194
|
+
});
|
|
195
|
+
return (0, import_libsql.drizzle)(client, { schema: schema_exports });
|
|
196
|
+
}
|
|
174
197
|
|
|
175
|
-
// src/
|
|
198
|
+
// src/server/sveltekit.ts
|
|
199
|
+
var import_server_only3 = require("server-only");
|
|
176
200
|
var import_svelte_kit = require("better-auth/svelte-kit");
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
201
|
+
function createSvelteKitServer(auth, db) {
|
|
202
|
+
const handler = (0, import_svelte_kit.toSvelteKitHandler)(auth);
|
|
203
|
+
return {
|
|
204
|
+
/**
|
|
205
|
+
* The underlying BetterAuth instance.
|
|
206
|
+
*/
|
|
207
|
+
auth,
|
|
208
|
+
/**
|
|
209
|
+
* SvelteKit request handler for auth routes.
|
|
210
|
+
* Place this in `src/routes/api/auth/[...auth]/+server.ts`
|
|
211
|
+
*/
|
|
212
|
+
handler: {
|
|
213
|
+
GET: handler,
|
|
214
|
+
POST: handler
|
|
215
|
+
},
|
|
216
|
+
/**
|
|
217
|
+
* Creates a SvelteKit handle hook for session management.
|
|
218
|
+
*/
|
|
219
|
+
createHandle: () => {
|
|
220
|
+
return async ({ event, resolve }) => {
|
|
221
|
+
const session2 = await auth.api.getSession({
|
|
222
|
+
headers: event.request.headers
|
|
223
|
+
});
|
|
224
|
+
event.locals.session = session2;
|
|
225
|
+
event.locals.user = session2?.user ?? null;
|
|
226
|
+
return resolve(event);
|
|
227
|
+
};
|
|
228
|
+
},
|
|
229
|
+
/**
|
|
230
|
+
* Get session from request event.
|
|
231
|
+
*/
|
|
232
|
+
getSession: async (event) => {
|
|
233
|
+
return auth.api.getSession({
|
|
206
234
|
headers: event.request.headers
|
|
207
235
|
});
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Get session from request event.
|
|
215
|
-
*/
|
|
216
|
-
getSession: async (event) => {
|
|
217
|
-
return auth.api.getSession({
|
|
218
|
-
headers: event.request.headers
|
|
219
|
-
});
|
|
220
|
-
},
|
|
221
|
-
/**
|
|
222
|
-
* Check if user is authenticated.
|
|
223
|
-
*/
|
|
224
|
-
isAuthenticated: async (event) => {
|
|
225
|
-
const session2 = await auth.api.getSession({
|
|
226
|
-
headers: event.request.headers
|
|
227
|
-
});
|
|
228
|
-
return session2 !== null;
|
|
229
|
-
},
|
|
230
|
-
/**
|
|
231
|
-
* Wraps a function to ensure the user is authenticated before execution.
|
|
232
|
-
* Injects the user, session, and db into the first argument.
|
|
233
|
-
*/
|
|
234
|
-
makeAuthenticatedCall: (fn) => {
|
|
235
|
-
return async (event, ...args) => {
|
|
236
|
+
},
|
|
237
|
+
/**
|
|
238
|
+
* Check if user is authenticated.
|
|
239
|
+
*/
|
|
240
|
+
isAuthenticated: async (event) => {
|
|
236
241
|
const session2 = await auth.api.getSession({
|
|
237
242
|
headers: event.request.headers
|
|
238
243
|
});
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
244
|
+
return session2 !== null;
|
|
245
|
+
},
|
|
246
|
+
/**
|
|
247
|
+
* Wraps a function to ensure the user is authenticated before execution.
|
|
248
|
+
* Injects the user, session, and db into the first argument.
|
|
249
|
+
*/
|
|
250
|
+
makeAuthenticatedCall: (fn) => {
|
|
251
|
+
return async (event, ...args) => {
|
|
252
|
+
const session2 = await auth.api.getSession({
|
|
253
|
+
headers: event.request.headers
|
|
254
|
+
});
|
|
255
|
+
if (!session2) {
|
|
256
|
+
throw new Error("Unauthorized");
|
|
257
|
+
}
|
|
258
|
+
return fn({ user: session2.user, session: session2.session, db, event }, ...args);
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
}
|
|
246
263
|
|
|
247
|
-
// src/
|
|
264
|
+
// src/server/nextjs.ts
|
|
265
|
+
var import_server_only4 = require("server-only");
|
|
248
266
|
var import_next_js = require("better-auth/next-js");
|
|
249
|
-
var import_react = require("better-auth/react");
|
|
250
267
|
var import_headers = require("next/headers");
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
});
|
|
291
|
-
},
|
|
292
|
-
/**
|
|
293
|
-
* Check if user is authenticated.
|
|
294
|
-
* Use in Server Components.
|
|
295
|
-
*/
|
|
296
|
-
isAuthenticated: async () => {
|
|
297
|
-
const requestHeaders = await (0, import_headers.headers)();
|
|
298
|
-
const session2 = await auth.api.getSession({
|
|
299
|
-
headers: requestHeaders
|
|
300
|
-
});
|
|
301
|
-
return session2 !== null;
|
|
302
|
-
},
|
|
303
|
-
/**
|
|
304
|
-
* Get the current user or null.
|
|
305
|
-
* Convenience method for Server Components.
|
|
306
|
-
*/
|
|
307
|
-
getUser: async () => {
|
|
308
|
-
const requestHeaders = await (0, import_headers.headers)();
|
|
309
|
-
const session2 = await auth.api.getSession({
|
|
310
|
-
headers: requestHeaders
|
|
311
|
-
});
|
|
312
|
-
return session2?.user ?? null;
|
|
313
|
-
},
|
|
314
|
-
/**
|
|
315
|
-
* Wraps a function to ensure the user is authenticated before execution.
|
|
316
|
-
* Injects the user, session, and db into the first argument.
|
|
317
|
-
*/
|
|
318
|
-
makeAuthenticatedCall: (fn) => {
|
|
319
|
-
return async (...args) => {
|
|
268
|
+
function createNextJsServer(auth, db) {
|
|
269
|
+
const handler = (0, import_next_js.toNextJsHandler)(auth);
|
|
270
|
+
return {
|
|
271
|
+
/**
|
|
272
|
+
* The underlying BetterAuth instance.
|
|
273
|
+
*/
|
|
274
|
+
auth,
|
|
275
|
+
/**
|
|
276
|
+
* Next.js route handler for auth routes.
|
|
277
|
+
* Place this in `app/api/auth/[...auth]/route.ts`
|
|
278
|
+
*/
|
|
279
|
+
handler: {
|
|
280
|
+
GET: handler,
|
|
281
|
+
POST: handler
|
|
282
|
+
},
|
|
283
|
+
/**
|
|
284
|
+
* Get session from current request headers.
|
|
285
|
+
* Use in Server Components or Route Handlers.
|
|
286
|
+
*/
|
|
287
|
+
getSession: async () => {
|
|
288
|
+
const requestHeaders = await (0, import_headers.headers)();
|
|
289
|
+
return auth.api.getSession({
|
|
290
|
+
headers: requestHeaders
|
|
291
|
+
});
|
|
292
|
+
},
|
|
293
|
+
/**
|
|
294
|
+
* Get session from specific headers.
|
|
295
|
+
* Use when you have direct access to headers.
|
|
296
|
+
*/
|
|
297
|
+
getSessionFromHeaders: async (requestHeaders) => {
|
|
298
|
+
return auth.api.getSession({
|
|
299
|
+
headers: requestHeaders
|
|
300
|
+
});
|
|
301
|
+
},
|
|
302
|
+
/**
|
|
303
|
+
* Check if user is authenticated.
|
|
304
|
+
* Use in Server Components.
|
|
305
|
+
*/
|
|
306
|
+
isAuthenticated: async () => {
|
|
320
307
|
const requestHeaders = await (0, import_headers.headers)();
|
|
321
308
|
const session2 = await auth.api.getSession({
|
|
322
309
|
headers: requestHeaders
|
|
323
310
|
});
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
311
|
+
return session2 !== null;
|
|
312
|
+
},
|
|
313
|
+
/**
|
|
314
|
+
* Get the current user or null.
|
|
315
|
+
* Convenience method for Server Components.
|
|
316
|
+
*/
|
|
317
|
+
getUser: async () => {
|
|
318
|
+
const requestHeaders = await (0, import_headers.headers)();
|
|
319
|
+
const session2 = await auth.api.getSession({
|
|
320
|
+
headers: requestHeaders
|
|
321
|
+
});
|
|
322
|
+
return session2?.user ?? null;
|
|
323
|
+
},
|
|
324
|
+
/**
|
|
325
|
+
* Wraps a function to ensure the user is authenticated before execution.
|
|
326
|
+
* Injects the user, session, and db into the first argument.
|
|
327
|
+
*/
|
|
328
|
+
makeAuthenticatedCall: (fn) => {
|
|
329
|
+
return async (...args) => {
|
|
330
|
+
const requestHeaders = await (0, import_headers.headers)();
|
|
331
|
+
const session2 = await auth.api.getSession({
|
|
332
|
+
headers: requestHeaders
|
|
333
|
+
});
|
|
334
|
+
if (!session2) {
|
|
335
|
+
throw new Error("Unauthorized");
|
|
336
|
+
}
|
|
337
|
+
return fn({ user: session2.user, session: session2.session, db }, ...args);
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
}
|
|
341
342
|
|
|
342
343
|
// src/bff/client.ts
|
|
343
344
|
var BffClientBase = class {
|
|
@@ -383,13 +384,14 @@ var BffClientBase = class {
|
|
|
383
384
|
// src/bff/nextjs.ts
|
|
384
385
|
var import_headers2 = require("next/headers");
|
|
385
386
|
var NextJsBffClient = class extends BffClientBase {
|
|
386
|
-
constructor(config) {
|
|
387
|
+
constructor(auth, config) {
|
|
387
388
|
super(config);
|
|
389
|
+
this.auth = auth;
|
|
388
390
|
}
|
|
389
391
|
async getAuthHeaders() {
|
|
390
392
|
try {
|
|
391
393
|
const requestHeaders = await (0, import_headers2.headers)();
|
|
392
|
-
const sessionData = await auth.api.getSession({
|
|
394
|
+
const sessionData = await this.auth.api.getSession({
|
|
393
395
|
headers: requestHeaders
|
|
394
396
|
});
|
|
395
397
|
if (!sessionData) return {};
|
|
@@ -455,9 +457,9 @@ var NextJsBffClient = class extends BffClientBase {
|
|
|
455
457
|
});
|
|
456
458
|
}
|
|
457
459
|
};
|
|
458
|
-
var createNextJsBff = (baseUrlOrConfig) => {
|
|
460
|
+
var createNextJsBff = (auth, baseUrlOrConfig) => {
|
|
459
461
|
const config = typeof baseUrlOrConfig === "string" ? { baseUrl: baseUrlOrConfig } : baseUrlOrConfig;
|
|
460
|
-
return new NextJsBffClient(config);
|
|
462
|
+
return new NextJsBffClient(auth, config);
|
|
461
463
|
};
|
|
462
464
|
|
|
463
465
|
// src/bff/sveltekit.ts
|
|
@@ -530,76 +532,84 @@ function isUnauthenticatedRoute(path, patterns) {
|
|
|
530
532
|
}
|
|
531
533
|
return false;
|
|
532
534
|
}
|
|
533
|
-
var
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
535
|
+
var createExpressMiddleware = (db) => {
|
|
536
|
+
return (options = {}) => {
|
|
537
|
+
const {
|
|
538
|
+
unauthenticatedRoutes = [],
|
|
539
|
+
verifySession = false,
|
|
540
|
+
required = true
|
|
541
|
+
} = options;
|
|
542
|
+
if (verifySession && !db) {
|
|
543
|
+
throw new Error(
|
|
544
|
+
"[hylekit] CONFIGURATION ERROR: verifySession requires a database instance. Pass a db instance to createExpressMiddleware(db)."
|
|
545
|
+
);
|
|
543
546
|
}
|
|
544
|
-
|
|
545
|
-
const
|
|
546
|
-
|
|
547
|
-
if (!sessionHeader || typeof sessionHeader !== "string") {
|
|
548
|
-
if (required) {
|
|
549
|
-
return res.status(401).json({ error: "Unauthorized" });
|
|
550
|
-
}
|
|
547
|
+
return async (req, res, next) => {
|
|
548
|
+
const authReq = req;
|
|
549
|
+
if (isUnauthenticatedRoute(req.path, unauthenticatedRoutes)) {
|
|
551
550
|
return next();
|
|
552
551
|
}
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
const result = await db.select({
|
|
558
|
-
session,
|
|
559
|
-
user
|
|
560
|
-
}).from(session).innerJoin(user, (0, import_drizzle_orm2.eq)(session.userId, user.id)).where(
|
|
561
|
-
(0, import_drizzle_orm2.and)(
|
|
562
|
-
(0, import_drizzle_orm2.eq)(session.id, sessionData.id),
|
|
563
|
-
(0, import_drizzle_orm2.gt)(session.expiresAt, /* @__PURE__ */ new Date())
|
|
564
|
-
)
|
|
565
|
-
).limit(1);
|
|
566
|
-
if (result.length === 0) {
|
|
552
|
+
try {
|
|
553
|
+
const userHeader = req.headers["x-hyle-user"];
|
|
554
|
+
const sessionHeader = req.headers["x-hyle-session"];
|
|
555
|
+
if (!sessionHeader || typeof sessionHeader !== "string") {
|
|
567
556
|
if (required) {
|
|
568
|
-
return res.status(401).json({ error: "
|
|
557
|
+
return res.status(401).json({ error: "Unauthorized" });
|
|
569
558
|
}
|
|
570
559
|
return next();
|
|
571
560
|
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
authReq.user = authReq.authUser;
|
|
575
|
-
authReq.session = authReq.authSession;
|
|
576
|
-
return next();
|
|
577
|
-
}
|
|
578
|
-
let userData;
|
|
579
|
-
if (userHeader && typeof userHeader === "string") {
|
|
580
|
-
userData = JSON.parse(
|
|
581
|
-
Buffer.from(userHeader, "base64").toString("utf-8")
|
|
561
|
+
const sessionData = JSON.parse(
|
|
562
|
+
Buffer.from(sessionHeader, "base64").toString("utf-8")
|
|
582
563
|
);
|
|
564
|
+
if (verifySession && db) {
|
|
565
|
+
const result = await db.select({
|
|
566
|
+
session,
|
|
567
|
+
user
|
|
568
|
+
}).from(session).innerJoin(user, (0, import_drizzle_orm2.eq)(session.userId, user.id)).where(
|
|
569
|
+
(0, import_drizzle_orm2.and)(
|
|
570
|
+
(0, import_drizzle_orm2.eq)(session.id, sessionData.id),
|
|
571
|
+
(0, import_drizzle_orm2.gt)(session.expiresAt, /* @__PURE__ */ new Date())
|
|
572
|
+
)
|
|
573
|
+
).limit(1);
|
|
574
|
+
if (result.length === 0) {
|
|
575
|
+
if (required) {
|
|
576
|
+
return res.status(401).json({ error: "Invalid or expired session" });
|
|
577
|
+
}
|
|
578
|
+
return next();
|
|
579
|
+
}
|
|
580
|
+
authReq.authUser = result[0].user;
|
|
581
|
+
authReq.authSession = result[0].session;
|
|
582
|
+
authReq.user = authReq.authUser;
|
|
583
|
+
authReq.session = authReq.authSession;
|
|
584
|
+
return next();
|
|
585
|
+
}
|
|
586
|
+
let userData;
|
|
587
|
+
if (userHeader && typeof userHeader === "string") {
|
|
588
|
+
userData = JSON.parse(
|
|
589
|
+
Buffer.from(userHeader, "base64").toString("utf-8")
|
|
590
|
+
);
|
|
591
|
+
}
|
|
592
|
+
if (!userData && required) {
|
|
593
|
+
return res.status(401).json({ error: "Unauthorized" });
|
|
594
|
+
}
|
|
595
|
+
if (userData) {
|
|
596
|
+
authReq.authUser = userData;
|
|
597
|
+
authReq.authSession = sessionData;
|
|
598
|
+
authReq.user = authReq.authUser;
|
|
599
|
+
authReq.session = authReq.authSession;
|
|
600
|
+
}
|
|
601
|
+
next();
|
|
602
|
+
} catch (error) {
|
|
603
|
+
console.error("[hyle] Auth middleware error:", error);
|
|
604
|
+
if (required) {
|
|
605
|
+
return res.status(401).json({ error: "Authentication failed" });
|
|
606
|
+
}
|
|
607
|
+
next();
|
|
583
608
|
}
|
|
584
|
-
|
|
585
|
-
return res.status(401).json({ error: "Unauthorized" });
|
|
586
|
-
}
|
|
587
|
-
if (userData) {
|
|
588
|
-
authReq.authUser = userData;
|
|
589
|
-
authReq.authSession = sessionData;
|
|
590
|
-
authReq.user = authReq.authUser;
|
|
591
|
-
authReq.session = authReq.authSession;
|
|
592
|
-
}
|
|
593
|
-
next();
|
|
594
|
-
} catch (error) {
|
|
595
|
-
console.error("[hyle] Auth middleware error:", error);
|
|
596
|
-
if (required) {
|
|
597
|
-
return res.status(401).json({ error: "Authentication failed" });
|
|
598
|
-
}
|
|
599
|
-
next();
|
|
600
|
-
}
|
|
609
|
+
};
|
|
601
610
|
};
|
|
602
611
|
};
|
|
612
|
+
var middleware = createExpressMiddleware();
|
|
603
613
|
function isAuthenticated(req) {
|
|
604
614
|
return !!req.authUser;
|
|
605
615
|
}
|
|
@@ -611,6 +621,8 @@ function getAuthContext(req) {
|
|
|
611
621
|
return { user: authReq.authUser, session: authReq.authSession };
|
|
612
622
|
}
|
|
613
623
|
var expressAdapter = {
|
|
624
|
+
createMiddleware: createExpressMiddleware,
|
|
625
|
+
/** @deprecated Use createMiddleware instead */
|
|
614
626
|
middleware,
|
|
615
627
|
isAuthenticated,
|
|
616
628
|
getAuthContext
|
|
@@ -622,12 +634,12 @@ var bff = {
|
|
|
622
634
|
createSvelteKitBff
|
|
623
635
|
};
|
|
624
636
|
var hyle = {
|
|
625
|
-
|
|
626
|
-
|
|
637
|
+
createDb,
|
|
638
|
+
createAuth,
|
|
627
639
|
schema: schema_exports,
|
|
628
|
-
client: client4,
|
|
629
640
|
server: {
|
|
630
|
-
|
|
641
|
+
createSvelteKitServer,
|
|
642
|
+
createNextJsServer,
|
|
631
643
|
express: expressAdapter
|
|
632
644
|
},
|
|
633
645
|
bff
|
|
@@ -636,17 +648,19 @@ var src_default = hyle;
|
|
|
636
648
|
// Annotate the CommonJS export names for ESM import in node:
|
|
637
649
|
0 && (module.exports = {
|
|
638
650
|
account,
|
|
639
|
-
auth,
|
|
640
651
|
bff,
|
|
641
|
-
|
|
652
|
+
createAuth,
|
|
653
|
+
createDb,
|
|
654
|
+
createExpressMiddleware,
|
|
642
655
|
createNextJsBff,
|
|
656
|
+
createNextJsServer,
|
|
643
657
|
createSvelteKitBff,
|
|
644
|
-
|
|
658
|
+
createSvelteKitServer,
|
|
645
659
|
express,
|
|
646
660
|
expressMiddleware,
|
|
647
661
|
getAuthContext,
|
|
648
662
|
isAuthenticated,
|
|
649
|
-
|
|
663
|
+
schema,
|
|
650
664
|
session,
|
|
651
665
|
user,
|
|
652
666
|
verification
|