@anysoftinc/anydb-sdk 0.5.0 → 0.6.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 +40 -0
- package/dist/client.d.ts +9 -5
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +43 -101
- package/dist/nextauth-adapter.d.ts +11 -1
- package/dist/nextauth-adapter.d.ts.map +1 -1
- package/dist/nextauth-adapter.js +25 -4
- package/package.json +4 -7
- package/dist/anydb.datascript.core.js +0 -336
- package/dist/anydb.datascript.rules.js +0 -29
- package/dist/anydb.datascript.schema.js +0 -35
- package/dist/cljs.core.js +0 -38752
- package/dist/cljs.reader.js +0 -450
- package/dist/cljs.tools.reader.edn.js +0 -945
- package/dist/cljs.tools.reader.impl.commons.js +0 -205
- package/dist/cljs.tools.reader.impl.errors.js +0 -429
- package/dist/cljs.tools.reader.impl.inspect.js +0 -170
- package/dist/cljs.tools.reader.impl.utils.js +0 -413
- package/dist/cljs.tools.reader.js +0 -1815
- package/dist/cljs.tools.reader.reader_types.js +0 -826
- package/dist/cljs_env.js +0 -7672
- package/dist/clojure.data.js +0 -307
- package/dist/clojure.edn.js +0 -107
- package/dist/clojure.set.js +0 -394
- package/dist/clojure.string.js +0 -490
- package/dist/clojure.walk.js +0 -144
- package/dist/datascript-backend.d.ts +0 -26
- package/dist/datascript-backend.d.ts.map +0 -1
- package/dist/datascript-backend.js +0 -113
- package/dist/datascript.built_ins.js +0 -680
- package/dist/datascript.conn.js +0 -814
- package/dist/datascript.core.js +0 -1285
- package/dist/datascript.db.js +0 -4058
- package/dist/datascript.impl.entity.js +0 -588
- package/dist/datascript.lru.js +0 -213
- package/dist/datascript.parser.js +0 -8598
- package/dist/datascript.pull_api.js +0 -2287
- package/dist/datascript.pull_parser.js +0 -865
- package/dist/datascript.query.js +0 -2785
- package/dist/datascript.serialize.js +0 -352
- package/dist/datascript.storage.js +0 -50
- package/dist/datascript.util.js +0 -82
- package/dist/extend_clj.core.js +0 -134
- package/dist/me.tonsky.persistent_sorted_set.arrays.js +0 -54
- package/dist/me.tonsky.persistent_sorted_set.js +0 -2485
package/README.md
CHANGED
|
@@ -212,6 +212,46 @@ export default NextAuth({
|
|
|
212
212
|
// Your providers
|
|
213
213
|
],
|
|
214
214
|
});
|
|
215
|
+
|
|
216
|
+
Cookie names per app (avoid collisions)
|
|
217
|
+
|
|
218
|
+
If multiple apps share a domain, configure distinct cookie names so sessions don’t collide.
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
import NextAuth from "next-auth";
|
|
222
|
+
import { AnyDBAdapter, createNextAuthCookies } from "@anysoftinc/anydb-sdk/nextauth-adapter";
|
|
223
|
+
|
|
224
|
+
// ai-pm-suite
|
|
225
|
+
export default NextAuth({
|
|
226
|
+
adapter: AnyDBAdapter(db),
|
|
227
|
+
cookies: createNextAuthCookies("ai-pm-suite", { style: "cookie" }),
|
|
228
|
+
// ... other config
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// dashboard
|
|
232
|
+
export default NextAuth({
|
|
233
|
+
adapter: AnyDBAdapter(db),
|
|
234
|
+
cookies: createNextAuthCookies("dashboard", { style: "cookie" }),
|
|
235
|
+
// ... other config
|
|
236
|
+
});
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
This produces names like `ai-pm-suite.session.cookie`, `ai-pm-suite.callback.cookie`, and `ai-pm-suite.csrf.cookie` (prefixed with `__Host-` in production). Clear existing cookies after changing names.
|
|
240
|
+
|
|
241
|
+
SSO across apps (shared DB)
|
|
242
|
+
|
|
243
|
+
If multiple apps use the same AnyDB auth database and you want single sign-on across them, derive cookie names from the database name so they all share the same cookie namespace:
|
|
244
|
+
|
|
245
|
+
```ts
|
|
246
|
+
import NextAuth from "next-auth";
|
|
247
|
+
import { AnyDBAdapter, createNextAuthCookiesFromDb } from "@anysoftinc/anydb-sdk/nextauth-adapter";
|
|
248
|
+
|
|
249
|
+
export default NextAuth({
|
|
250
|
+
adapter: AnyDBAdapter(db),
|
|
251
|
+
// Use DB name for cookie namespace to share sessions across apps
|
|
252
|
+
cookies: createNextAuthCookiesFromDb(db, { style: "cookie" }),
|
|
253
|
+
// ... other config
|
|
254
|
+
});
|
|
215
255
|
```
|
|
216
256
|
|
|
217
257
|
## Auth Helpers (Server-side)
|
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { shouldUseDataScript, isDataScriptAvailable } from "./datascript-backend";
|
|
2
1
|
export interface Symbol {
|
|
3
2
|
_type: "symbol";
|
|
4
3
|
value: string;
|
|
@@ -86,7 +85,14 @@ export declare class AnyDBClient {
|
|
|
86
85
|
private client;
|
|
87
86
|
private storageAlias;
|
|
88
87
|
private dbName;
|
|
89
|
-
|
|
88
|
+
private schemas;
|
|
89
|
+
private initialized;
|
|
90
|
+
private initPromise;
|
|
91
|
+
constructor(client: DatomicClient, storageAlias: string, dbName: string, schemas: Array<Record<string, any>>[]);
|
|
92
|
+
private initializeSchemas;
|
|
93
|
+
private ensureInitialized;
|
|
94
|
+
getDbName(): string;
|
|
95
|
+
getStorageAlias(): string;
|
|
90
96
|
info(): Promise<DatabaseInfo>;
|
|
91
97
|
transact(txData: any[]): Promise<Transaction>;
|
|
92
98
|
query(q: SymbolicQuery, ...args: any[]): Promise<QueryResult>;
|
|
@@ -109,9 +115,7 @@ export declare class AnyDBClient {
|
|
|
109
115
|
subscribeToEvents(onEvent: (event: MessageEvent) => void, onError?: (error: Event) => void): EventSource;
|
|
110
116
|
}
|
|
111
117
|
export declare function createDatomicClient(config: ClientConfig): DatomicClient;
|
|
112
|
-
export declare function createAnyDBClient(client: DatomicClient, storageAlias: string, dbName: string): AnyDBClient;
|
|
118
|
+
export declare function createAnyDBClient(client: DatomicClient, storageAlias: string, dbName: string, schemas?: Array<Record<string, any>>[]): AnyDBClient;
|
|
113
119
|
export declare function pluckFirstColumn(rows: any[][]): any[];
|
|
114
|
-
export { shouldUseDataScript, isDataScriptAvailable };
|
|
115
120
|
export default DatomicClient;
|
|
116
|
-
export declare function ensureAttributes(db: AnyDBClient, schemaEntities: Array<Record<string, any>>): Promise<void>;
|
|
117
121
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AASA,MAAM,WAAW,MAAM;IACrB,KAAK,EAAE,QAAQ,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,IAAI;IACnB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,OAAO;IACtB,KAAK,EAAE,SAAS,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,eAAO,MAAM,GAAG,GAAI,MAAM,MAAM,KAAG,MAA4C,CAAC;AAChF,eAAO,MAAM,IAAI,GAAI,IAAI,MAAM,KAAG,IAAsC,CAAC;AACzE,eAAO,MAAM,EAAE,GAAI,MAAM,MAAM,KAAG,OAajC,CAAC;AAEF,MAAM,MAAM,UAAU,GAClB,MAAM,GACN,IAAI,GACJ,OAAO,GACP,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,IAAI,GACJ,GAAG,EAAE,CAAC;AACV,MAAM,MAAM,WAAW,GAAG,UAAU,EAAE,CAAC;AAEvC,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,UAAU,EAAE,CAAC;IACnB,KAAK,EAAE,WAAW,EAAE,CAAC;IACrB,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC;CACnB;AAED,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AACvC,MAAM,MAAM,MAAM,GAAG,MAAM,GAAG,GAAG,CAAC;AAElC,MAAM,WAAW,KAAK;IACpB,CA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AAM,EAAE,GAAG,CAAC,CAAC,EAAO,GACzC,WAAW,CAEb;AAGD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,GAAG,GAAG,EAAE,CAGrD;AAED,eAAe,aAAa,CAAC"}
|
package/dist/client.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
// AnyDB TypeScript SDK v2 - strict
|
|
1
|
+
// AnyDB TypeScript SDK v2 - strict HTTP client
|
|
2
2
|
// - Explicit EDN symbolic types (Keyword, Symbol, UUID)
|
|
3
3
|
// - Strict SymbolicQuery API
|
|
4
|
-
// - Explicit DataScript selection via storage alias (no fallback)
|
|
5
4
|
// - Keywordized JS<->EDN conversion for object keys
|
|
6
5
|
import { parseEDNString as parseEdn } from "edn-data";
|
|
7
|
-
import { DataScriptBackend, shouldUseDataScript, isDataScriptAvailable, } from "./datascript-backend";
|
|
8
6
|
export const sym = (name) => ({ _type: "symbol", value: name });
|
|
9
7
|
export const uuid = (id) => ({ _type: "uuid", value: id });
|
|
10
8
|
export const kw = (name) => {
|
|
@@ -267,41 +265,60 @@ export class DatomicClient {
|
|
|
267
265
|
}
|
|
268
266
|
// ===== Convenience wrapper for a specific database =====
|
|
269
267
|
export class AnyDBClient {
|
|
270
|
-
constructor(client, storageAlias, dbName) {
|
|
268
|
+
constructor(client, storageAlias, dbName, schemas) {
|
|
271
269
|
this.client = client;
|
|
272
270
|
this.storageAlias = storageAlias;
|
|
273
271
|
this.dbName = dbName;
|
|
272
|
+
this.schemas = schemas;
|
|
273
|
+
this.initialized = false;
|
|
274
|
+
this.initPromise = null;
|
|
275
|
+
// Eagerly initialize schemas
|
|
276
|
+
this.initPromise = this.initializeSchemas();
|
|
274
277
|
}
|
|
275
|
-
async
|
|
276
|
-
if (
|
|
277
|
-
|
|
278
|
-
|
|
278
|
+
async initializeSchemas() {
|
|
279
|
+
if (this.initialized)
|
|
280
|
+
return;
|
|
281
|
+
const allSchemas = this.schemas.flat();
|
|
282
|
+
if (allSchemas.length === 0) {
|
|
283
|
+
this.initialized = true;
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
// Batch transact all schemas to Datomic
|
|
287
|
+
if (allSchemas.length > 0) {
|
|
288
|
+
try {
|
|
289
|
+
await this.client.transact(this.storageAlias, this.dbName, allSchemas);
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
throw new SchemaError("Failed to initialize schemas", { error, schemaCount: allSchemas.length });
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
this.initialized = true;
|
|
296
|
+
}
|
|
297
|
+
async ensureInitialized() {
|
|
298
|
+
if (this.initPromise) {
|
|
299
|
+
await this.initPromise;
|
|
279
300
|
}
|
|
301
|
+
}
|
|
302
|
+
// Expose identifiers for consumers that need deterministic naming
|
|
303
|
+
getDbName() {
|
|
304
|
+
return this.dbName;
|
|
305
|
+
}
|
|
306
|
+
getStorageAlias() {
|
|
307
|
+
return this.storageAlias;
|
|
308
|
+
}
|
|
309
|
+
async info() {
|
|
310
|
+
await this.ensureInitialized();
|
|
280
311
|
// Exercise connectivity/auth via a trivial query; propagate errors to caller.
|
|
281
312
|
const q = { find: [sym("?e")], where: [[sym("?e"), kw("db/id"), 0]] };
|
|
282
313
|
await this.query(q);
|
|
283
314
|
return { "basis-t": -1, "db/alias": `${this.storageAlias}/${this.dbName}` };
|
|
284
315
|
}
|
|
285
316
|
async transact(txData) {
|
|
286
|
-
|
|
287
|
-
const backend = new DataScriptBackend(this.storageAlias, this.dbName);
|
|
288
|
-
return backend.transact(txData);
|
|
289
|
-
}
|
|
317
|
+
await this.ensureInitialized();
|
|
290
318
|
return this.client.transact(this.storageAlias, this.dbName, txData);
|
|
291
319
|
}
|
|
292
320
|
async query(q, ...args) {
|
|
293
|
-
|
|
294
|
-
const backend = new DataScriptBackend(this.storageAlias, this.dbName);
|
|
295
|
-
validateSymbolicQuery(q);
|
|
296
|
-
const ednQuery = [
|
|
297
|
-
kw("find"),
|
|
298
|
-
...q.find,
|
|
299
|
-
...(q.in && q.in.length ? [kw("in"), ...q.in] : []),
|
|
300
|
-
kw("where"),
|
|
301
|
-
...q.where,
|
|
302
|
-
];
|
|
303
|
-
return backend.query(ednQuery, ...args);
|
|
304
|
-
}
|
|
321
|
+
await this.ensureInitialized();
|
|
305
322
|
// Server path: use /api/query with db descriptor as first arg
|
|
306
323
|
const dbDescriptor = {
|
|
307
324
|
"db/alias": `${this.storageAlias}/${this.dbName}`,
|
|
@@ -309,10 +326,6 @@ export class AnyDBClient {
|
|
|
309
326
|
return this.client.querySymbolic(q, [dbDescriptor, ...args]);
|
|
310
327
|
}
|
|
311
328
|
async entity(entityId, basisT = "-", options) {
|
|
312
|
-
if (shouldUseDataScript(this.storageAlias)) {
|
|
313
|
-
const backend = new DataScriptBackend(this.storageAlias, this.dbName);
|
|
314
|
-
return backend.entity(entityId);
|
|
315
|
-
}
|
|
316
329
|
// Use the query API to retrieve entity via pull
|
|
317
330
|
const q = {
|
|
318
331
|
find: [[sym("pull"), sym("?e"), [kw("db/id")]]],
|
|
@@ -323,16 +336,6 @@ export class AnyDBClient {
|
|
|
323
336
|
return first || { "db/id": entityId };
|
|
324
337
|
}
|
|
325
338
|
async datoms(index, options = {}) {
|
|
326
|
-
if (shouldUseDataScript(this.storageAlias)) {
|
|
327
|
-
const backend = new DataScriptBackend(this.storageAlias, this.dbName);
|
|
328
|
-
return backend.datoms(index, {
|
|
329
|
-
e: options.e,
|
|
330
|
-
a: options.a,
|
|
331
|
-
v: options.v,
|
|
332
|
-
limit: options.limit,
|
|
333
|
-
offset: options.offset,
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
339
|
// Server exposes datoms via query; implement minimal AVET example
|
|
337
340
|
const clauses = [];
|
|
338
341
|
const e = options.e !== undefined ? options.e : sym("?e");
|
|
@@ -351,8 +354,8 @@ export class AnyDBClient {
|
|
|
351
354
|
export function createDatomicClient(config) {
|
|
352
355
|
return new DatomicClient(config);
|
|
353
356
|
}
|
|
354
|
-
export function createAnyDBClient(client, storageAlias, dbName) {
|
|
355
|
-
return new AnyDBClient(client, storageAlias, dbName);
|
|
357
|
+
export function createAnyDBClient(client, storageAlias, dbName, schemas = []) {
|
|
358
|
+
return new AnyDBClient(client, storageAlias, dbName, schemas);
|
|
356
359
|
}
|
|
357
360
|
// Convenience
|
|
358
361
|
export function pluckFirstColumn(rows) {
|
|
@@ -360,65 +363,4 @@ export function pluckFirstColumn(rows) {
|
|
|
360
363
|
return [];
|
|
361
364
|
return rows.map((r) => (Array.isArray(r) ? r[0] : r));
|
|
362
365
|
}
|
|
363
|
-
// Re-export DataScript helpers for consumers that need feature-detection
|
|
364
|
-
export { shouldUseDataScript, isDataScriptAvailable };
|
|
365
366
|
export default DatomicClient;
|
|
366
|
-
// ===== Schema helpers (idempotent ensure) =====
|
|
367
|
-
const __ensuredIdentsCache = new Map();
|
|
368
|
-
function getDbAliasCached(db) {
|
|
369
|
-
return db.info().then((info) => info["db/alias"]);
|
|
370
|
-
}
|
|
371
|
-
function extractIdentName(entity) {
|
|
372
|
-
const identVal = entity["db/ident"];
|
|
373
|
-
if (!identVal)
|
|
374
|
-
return null;
|
|
375
|
-
if (typeof identVal === "string")
|
|
376
|
-
return identVal.startsWith(":") ? identVal.slice(1) : identVal;
|
|
377
|
-
if (typeof identVal === "object" && identVal && identVal._type === "keyword") {
|
|
378
|
-
return identVal.value;
|
|
379
|
-
}
|
|
380
|
-
return null;
|
|
381
|
-
}
|
|
382
|
-
export async function ensureAttributes(db, schemaEntities) {
|
|
383
|
-
const alias = await getDbAliasCached(db);
|
|
384
|
-
const cached = __ensuredIdentsCache.get(alias) || new Set();
|
|
385
|
-
// Compute desired idents
|
|
386
|
-
const desired = schemaEntities
|
|
387
|
-
.map(extractIdentName)
|
|
388
|
-
.filter((v) => Boolean(v));
|
|
389
|
-
// Filter out those already ensured in this process
|
|
390
|
-
const toCheck = desired.filter((n) => !cached.has(n));
|
|
391
|
-
if (toCheck.length === 0)
|
|
392
|
-
return;
|
|
393
|
-
// Determine which exist already in DB
|
|
394
|
-
const existing = new Set();
|
|
395
|
-
for (const ident of toCheck) {
|
|
396
|
-
try {
|
|
397
|
-
const q = {
|
|
398
|
-
find: [sym("?e")],
|
|
399
|
-
where: [
|
|
400
|
-
[sym("?e"), kw("db/ident"), kw(ident)],
|
|
401
|
-
],
|
|
402
|
-
};
|
|
403
|
-
const rows = await db.query(q);
|
|
404
|
-
if (Array.isArray(rows) && rows.length > 0)
|
|
405
|
-
existing.add(ident);
|
|
406
|
-
}
|
|
407
|
-
catch (_) {
|
|
408
|
-
// ignore transient errors
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
const missing = toCheck.filter((n) => !existing.has(n));
|
|
412
|
-
if (missing.length > 0) {
|
|
413
|
-
const tx = schemaEntities.filter((e) => {
|
|
414
|
-
const n = extractIdentName(e);
|
|
415
|
-
return n != null && missing.includes(n);
|
|
416
|
-
});
|
|
417
|
-
if (tx.length > 0) {
|
|
418
|
-
await db.transact(tx);
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
// Update cache
|
|
422
|
-
const updated = new Set([...cached, ...toCheck]);
|
|
423
|
-
__ensuredIdentsCache.set(alias, updated);
|
|
424
|
-
}
|
|
@@ -4,7 +4,17 @@ import type { AnyDBClient } from "./client";
|
|
|
4
4
|
* Creates NextAuth cookie configuration with proper security settings
|
|
5
5
|
* Uses __Host- prefix and secure cookies in production, regular cookies in development
|
|
6
6
|
*/
|
|
7
|
-
export declare function createNextAuthCookies(cookieIdentifier: string
|
|
7
|
+
export declare function createNextAuthCookies(cookieIdentifier: string, opts?: {
|
|
8
|
+
style?: "token" | "cookie";
|
|
9
|
+
}): Record<string, any>;
|
|
10
|
+
/**
|
|
11
|
+
* Derive cookie names from the connected database, enabling SSO across apps
|
|
12
|
+
* that share the same auth database. If multiple apps point to the same DB name,
|
|
13
|
+
* they will share the same cookie namespace and thus sessions.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createNextAuthCookiesFromDb(db: AnyDBClient, opts?: {
|
|
16
|
+
style?: "token" | "cookie";
|
|
17
|
+
}): Record<string, any>;
|
|
8
18
|
/**
|
|
9
19
|
* Creates a NextAuth.js adapter for AnyDB/Datomic
|
|
10
20
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nextauth-adapter.d.ts","sourceRoot":"","sources":["../src/nextauth-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAkB,MAAM,oBAAoB,CAAC;AAClE,OAAO,KAAK,EAAE,WAAW,EAAiB,MAAM,UAAU,CAAC;AAI3D;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,gBAAgB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"nextauth-adapter.d.ts","sourceRoot":"","sources":["../src/nextauth-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAkB,MAAM,oBAAoB,CAAC;AAClE,OAAO,KAAK,EAAE,WAAW,EAAiB,MAAM,UAAU,CAAC;AAI3D;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,gBAAgB,EAAE,MAAM,EACxB,IAAI,CAAC,EAAE;IAAE,KAAK,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAA;CAAE,GACpC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAgDrB;AAED;;;;GAIG;AACH,wBAAgB,2BAA2B,CACzC,EAAE,EAAE,WAAW,EACf,IAAI,CAAC,EAAE;IAAE,KAAK,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAA;CAAE,GACpC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAGrB;AA2DD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CA6WrD"}
|
package/dist/nextauth-adapter.js
CHANGED
|
@@ -4,13 +4,25 @@ import { v4 as uuidv4 } from "uuid";
|
|
|
4
4
|
* Creates NextAuth cookie configuration with proper security settings
|
|
5
5
|
* Uses __Host- prefix and secure cookies in production, regular cookies in development
|
|
6
6
|
*/
|
|
7
|
-
export function createNextAuthCookies(cookieIdentifier) {
|
|
7
|
+
export function createNextAuthCookies(cookieIdentifier, opts) {
|
|
8
8
|
const isProduction = process.env.NODE_ENV === "production";
|
|
9
9
|
const prefix = isProduction ? "__Host-" : "";
|
|
10
10
|
const secure = isProduction;
|
|
11
|
+
const style = opts?.style ?? "token";
|
|
12
|
+
const names = style === "cookie"
|
|
13
|
+
? {
|
|
14
|
+
session: `${prefix}${cookieIdentifier}.session.cookie`,
|
|
15
|
+
callback: `${prefix}${cookieIdentifier}.callback.cookie`,
|
|
16
|
+
csrf: `${prefix}${cookieIdentifier}.csrf.cookie`,
|
|
17
|
+
}
|
|
18
|
+
: {
|
|
19
|
+
session: `${prefix}${cookieIdentifier}.session-token`,
|
|
20
|
+
callback: `${prefix}${cookieIdentifier}.callback-url`,
|
|
21
|
+
csrf: `${prefix}${cookieIdentifier}.csrf-token`,
|
|
22
|
+
};
|
|
11
23
|
return {
|
|
12
24
|
sessionToken: {
|
|
13
|
-
name:
|
|
25
|
+
name: names.session,
|
|
14
26
|
options: {
|
|
15
27
|
httpOnly: true,
|
|
16
28
|
sameSite: "lax",
|
|
@@ -19,7 +31,7 @@ export function createNextAuthCookies(cookieIdentifier) {
|
|
|
19
31
|
},
|
|
20
32
|
},
|
|
21
33
|
callbackUrl: {
|
|
22
|
-
name:
|
|
34
|
+
name: names.callback,
|
|
23
35
|
options: {
|
|
24
36
|
httpOnly: true,
|
|
25
37
|
sameSite: "lax",
|
|
@@ -28,7 +40,7 @@ export function createNextAuthCookies(cookieIdentifier) {
|
|
|
28
40
|
},
|
|
29
41
|
},
|
|
30
42
|
csrfToken: {
|
|
31
|
-
name:
|
|
43
|
+
name: names.csrf,
|
|
32
44
|
options: {
|
|
33
45
|
httpOnly: true,
|
|
34
46
|
sameSite: "lax",
|
|
@@ -38,6 +50,15 @@ export function createNextAuthCookies(cookieIdentifier) {
|
|
|
38
50
|
},
|
|
39
51
|
};
|
|
40
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Derive cookie names from the connected database, enabling SSO across apps
|
|
55
|
+
* that share the same auth database. If multiple apps point to the same DB name,
|
|
56
|
+
* they will share the same cookie namespace and thus sessions.
|
|
57
|
+
*/
|
|
58
|
+
export function createNextAuthCookiesFromDb(db, opts) {
|
|
59
|
+
const id = db.getDbName ? db.getDbName() : "anydb";
|
|
60
|
+
return createNextAuthCookies(id, opts);
|
|
61
|
+
}
|
|
41
62
|
// Datomic schema idents
|
|
42
63
|
const USER = {
|
|
43
64
|
id: "anydb.auth.user.v1/id", // uuid identity
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anysoftinc/anydb-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "AnyDB TypeScript SDK for querying and transacting with Datomic databases",
|
|
5
5
|
"main": "dist/client.js",
|
|
6
6
|
"types": "dist/client.d.ts",
|
|
@@ -27,15 +27,13 @@
|
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"scripts": {
|
|
30
|
-
"build": "
|
|
31
|
-
"build:cljs": "npx shadow-cljs compile datascript",
|
|
30
|
+
"build": "tsc",
|
|
32
31
|
"dev": "tsc --watch",
|
|
33
|
-
"
|
|
34
|
-
"prepublishOnly": "npm run build:cljs && tsc && node scripts/pack-cljs.js",
|
|
32
|
+
"prepublishOnly": "tsc",
|
|
35
33
|
"test": "jest",
|
|
36
34
|
"test:watch": "jest --watch",
|
|
37
35
|
"test:coverage": "jest --coverage",
|
|
38
|
-
"clean": "rm -rf dist
|
|
36
|
+
"clean": "rm -rf dist"
|
|
39
37
|
},
|
|
40
38
|
"dependencies": {
|
|
41
39
|
"edn-data": "^1.1.2",
|
|
@@ -54,7 +52,6 @@
|
|
|
54
52
|
"@types/node": "^20",
|
|
55
53
|
"jest": "^29.7.0",
|
|
56
54
|
"next-auth": "^4.24.11",
|
|
57
|
-
"shadow-cljs": "^2.25.10",
|
|
58
55
|
"ts-jest": "^29.1.4",
|
|
59
56
|
"typescript": "^5"
|
|
60
57
|
},
|