@shivaedev/identity 0.39.0 → 0.41.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 (100) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +37 -28
  3. package/dist/accountPreferenceHeaders.d.ts +5 -0
  4. package/dist/accountPreferenceHeaders.d.ts.map +1 -0
  5. package/dist/accountPreferenceHeaders.js +5 -0
  6. package/dist/accountPreferenceHeaders.js.map +1 -0
  7. package/dist/client/preferencesPlugin.d.ts +17 -0
  8. package/dist/client/preferencesPlugin.d.ts.map +1 -0
  9. package/dist/{server/better-auth/preferencesClient.js → client/preferencesPlugin.js} +7 -7
  10. package/dist/client/preferencesPlugin.js.map +1 -0
  11. package/dist/server/account-data/OwnedModelsUncollected.d.ts +7 -0
  12. package/dist/server/account-data/OwnedModelsUncollected.d.ts.map +1 -0
  13. package/dist/server/account-data/OwnedModelsUncollected.js +7 -0
  14. package/dist/server/account-data/OwnedModelsUncollected.js.map +1 -0
  15. package/dist/server/account-data/OwnedTableCycle.d.ts +9 -0
  16. package/dist/server/account-data/OwnedTableCycle.d.ts.map +1 -0
  17. package/dist/server/account-data/OwnedTableCycle.js +9 -0
  18. package/dist/server/account-data/OwnedTableCycle.js.map +1 -0
  19. package/dist/server/account-data/UncoveredUserColumns.d.ts +19 -0
  20. package/dist/server/account-data/UncoveredUserColumns.d.ts.map +1 -0
  21. package/dist/server/account-data/UncoveredUserColumns.js +11 -0
  22. package/dist/server/account-data/UncoveredUserColumns.js.map +1 -0
  23. package/dist/server/account-data/checkCoverage.d.ts +8 -0
  24. package/dist/server/account-data/checkCoverage.d.ts.map +1 -0
  25. package/dist/server/account-data/checkCoverage.js +25 -0
  26. package/dist/server/account-data/checkCoverage.js.map +1 -0
  27. package/dist/server/account-data/eraseOrder.d.ts +7 -0
  28. package/dist/server/account-data/eraseOrder.d.ts.map +1 -0
  29. package/dist/server/account-data/eraseOrder.js +24 -0
  30. package/dist/server/account-data/eraseOrder.js.map +1 -0
  31. package/dist/server/account-data/eraseOwnedRows.d.ts +5 -0
  32. package/dist/server/account-data/eraseOwnedRows.d.ts.map +1 -0
  33. package/dist/server/account-data/eraseOwnedRows.js +27 -0
  34. package/dist/server/account-data/eraseOwnedRows.js.map +1 -0
  35. package/dist/server/account-data/exportOwnedRows.d.ts +7 -0
  36. package/dist/server/account-data/exportOwnedRows.d.ts.map +1 -0
  37. package/dist/server/account-data/exportOwnedRows.js +36 -0
  38. package/dist/server/account-data/exportOwnedRows.js.map +1 -0
  39. package/dist/server/account-data/foreignKeys.d.ts +27 -0
  40. package/dist/server/account-data/foreignKeys.d.ts.map +1 -0
  41. package/dist/server/account-data/foreignKeys.js +32 -0
  42. package/dist/server/account-data/foreignKeys.js.map +1 -0
  43. package/dist/server/account-data/ownedTables.d.ts +11 -0
  44. package/dist/server/account-data/ownedTables.d.ts.map +1 -0
  45. package/dist/server/account-data/ownedTables.js +41 -0
  46. package/dist/server/account-data/ownedTables.js.map +1 -0
  47. package/dist/server/account-preferences/Rows.d.ts.map +1 -1
  48. package/dist/server/account-preferences/Rows.js +2 -1
  49. package/dist/server/account-preferences/Rows.js.map +1 -1
  50. package/dist/server/better-auth/accountData.d.ts +42 -0
  51. package/dist/server/better-auth/accountData.d.ts.map +1 -0
  52. package/dist/server/better-auth/accountData.js +24 -0
  53. package/dist/server/better-auth/accountData.js.map +1 -0
  54. package/dist/server/better-auth/adapters/accountPreferenceOptions.d.ts.map +1 -1
  55. package/dist/server/better-auth/adapters/accountPreferenceOptions.js +3 -2
  56. package/dist/server/better-auth/adapters/accountPreferenceOptions.js.map +1 -1
  57. package/dist/server/better-auth/profileExport.d.ts +32 -0
  58. package/dist/server/better-auth/profileExport.d.ts.map +1 -0
  59. package/dist/server/better-auth/profileExport.js +53 -0
  60. package/dist/server/better-auth/profileExport.js.map +1 -0
  61. package/package.json +10 -10
  62. package/src/accountPreferenceHeaders.ts +4 -0
  63. package/src/client/preferencesPlugin.ts +40 -0
  64. package/src/server/account-data/OwnedModelsUncollected.ts +7 -0
  65. package/src/server/account-data/OwnedTableCycle.ts +9 -0
  66. package/src/server/account-data/UncoveredUserColumns.ts +14 -0
  67. package/src/server/account-data/checkCoverage.ts +28 -0
  68. package/src/server/account-data/eraseOrder.ts +36 -0
  69. package/src/server/account-data/eraseOwnedRows.ts +28 -0
  70. package/src/server/account-data/exportOwnedRows.ts +45 -0
  71. package/src/server/account-data/foreignKeys.ts +35 -0
  72. package/src/server/account-data/ownedTables.ts +43 -0
  73. package/src/server/account-preferences/Rows.ts +2 -1
  74. package/src/server/better-auth/accountData.ts +26 -0
  75. package/src/server/better-auth/adapters/accountPreferenceOptions.ts +3 -2
  76. package/src/server/better-auth/profileExport.ts +55 -0
  77. package/dist/server/account-export/check.d.ts +0 -15
  78. package/dist/server/account-export/check.d.ts.map +0 -1
  79. package/dist/server/account-export/check.js +0 -33
  80. package/dist/server/account-export/check.js.map +0 -1
  81. package/dist/server/accountExport.d.ts +0 -14
  82. package/dist/server/accountExport.d.ts.map +0 -1
  83. package/dist/server/accountExport.js +0 -15
  84. package/dist/server/accountExport.js.map +0 -1
  85. package/dist/server/better-auth/preferencesClient.d.ts +0 -3
  86. package/dist/server/better-auth/preferencesClient.d.ts.map +0 -1
  87. package/dist/server/better-auth/preferencesClient.js.map +0 -1
  88. package/dist/server/better-auth/userDataExport.d.ts +0 -38
  89. package/dist/server/better-auth/userDataExport.d.ts.map +0 -1
  90. package/dist/server/better-auth/userDataExport.js +0 -55
  91. package/dist/server/better-auth/userDataExport.js.map +0 -1
  92. package/dist/server/userDataExport.d.ts +0 -13
  93. package/dist/server/userDataExport.d.ts.map +0 -1
  94. package/dist/server/userDataExport.js +0 -2
  95. package/dist/server/userDataExport.js.map +0 -1
  96. package/src/server/account-export/check.ts +0 -38
  97. package/src/server/accountExport.ts +0 -40
  98. package/src/server/better-auth/preferencesClient.ts +0 -25
  99. package/src/server/better-auth/userDataExport.ts +0 -57
  100. package/src/server/userDataExport.ts +0 -15
@@ -0,0 +1,35 @@
1
+ import { Effect, Schema } from "effect";
2
+ import { SqlClient, SqlSchema } from "effect/sql";
3
+
4
+ const ForeignKeyColumn = Schema.Struct({
5
+ column: Schema.String,
6
+ onDelete: Schema.String,
7
+ schema: Schema.String,
8
+ table: Schema.String,
9
+ target: Schema.Struct({ column: Schema.String, schema: Schema.String, table: Schema.String }),
10
+ });
11
+
12
+ export type ForeignKeyColumn = typeof ForeignKeyColumn.Type;
13
+
14
+ // One row per column pair, so a composite key such as (id, owner_id) shows which column points at a person.
15
+ export const foreignKeys = Effect.fn("Identity.foreignKeys")(function* () {
16
+ const sql = (yield* SqlClient.SqlClient).withoutTransforms();
17
+ return yield* SqlSchema.findAll({
18
+ execute: () => sql`
19
+ select source_schema.nspname as schema, source.relname as table, source_column.attname as column,
20
+ fk.confdeltype::text as "onDelete",
21
+ json_build_object('schema', target_schema.nspname, 'table', target.relname, 'column', target_column.attname) as target
22
+ from pg_catalog.pg_constraint fk
23
+ cross join lateral unnest(fk.conkey, fk.confkey) as pair(source_key, target_key)
24
+ join pg_catalog.pg_class source on source.oid = fk.conrelid
25
+ join pg_catalog.pg_namespace source_schema on source_schema.oid = source.relnamespace
26
+ join pg_catalog.pg_attribute source_column on source_column.attrelid = source.oid and source_column.attnum = pair.source_key
27
+ join pg_catalog.pg_class target on target.oid = fk.confrelid
28
+ join pg_catalog.pg_namespace target_schema on target_schema.oid = target.relnamespace
29
+ join pg_catalog.pg_attribute target_column on target_column.attrelid = target.oid and target_column.attnum = pair.target_key
30
+ where fk.contype = 'f'
31
+ order by schema, "table", "column"`,
32
+ Request: Schema.Void,
33
+ Result: ForeignKeyColumn,
34
+ })(undefined);
35
+ });
@@ -0,0 +1,43 @@
1
+ import { Context, Effect, Option, Schema } from "effect";
2
+ import { SqlClient, SqlSchema } from "effect/sql";
3
+ import { RepositoryModels } from "@shivaedev/sql/RepositoryModels.ts";
4
+ import { OwnedModelsUncollected } from "#server/account-data/OwnedModelsUncollected.ts";
5
+
6
+ const ResolvedTable = Schema.Struct({ name: Schema.String, schema: Schema.String, table: Schema.String });
7
+
8
+ // Quoted the way a repository's statement names its table, so the catalog resolves the same relation through the search path.
9
+ function quotedName(name: string) {
10
+ return name
11
+ .split(".")
12
+ .map((part) => `"${part.replaceAll('"', '""')}"`)
13
+ .join(".");
14
+ }
15
+
16
+ export const ownedTables = Effect.fn("Identity.ownedTables")(function* () {
17
+ const models = yield* RepositoryModels;
18
+ // Without a collector the Reference holds its default, which records nothing, so an empty list would read as "nothing owned".
19
+ if (Option.getOrUndefined(Context.getOption(Context.empty(), RepositoryModels)) === models) {
20
+ return yield* new OwnedModelsUncollected();
21
+ }
22
+ const owned = yield* models.owned;
23
+ const names = [...new Set(owned.map(({ table }) => table))];
24
+ if (names.length === 0) {
25
+ return [];
26
+ }
27
+ const sql = (yield* SqlClient.SqlClient).withoutTransforms();
28
+ const resolved = yield* SqlSchema.findAll({
29
+ execute: () => sql`
30
+ select owned.name, namespace.nspname as schema, relation.relname as table
31
+ from (values ${sql.csv(names.map((name) => sql`(${name}, ${quotedName(name)})`))}) as owned(name, quoted)
32
+ join pg_catalog.pg_class relation on relation.oid = to_regclass(owned.quoted)
33
+ join pg_catalog.pg_namespace namespace on namespace.oid = relation.relnamespace`,
34
+ Request: Schema.Void,
35
+ Result: ResolvedTable,
36
+ })(undefined);
37
+ return owned.flatMap((model) => {
38
+ const table = resolved.find(({ name }) => name === model.table);
39
+ return table === undefined
40
+ ? []
41
+ : [{ ...model, ownerKey: `${table.schema}.${table.table}.${model.owner.column}`, relation: `${table.schema}.${table.table}` }];
42
+ });
43
+ });
@@ -1,11 +1,12 @@
1
1
  import { Schema } from "effect";
2
2
  import { Model } from "effect/schema";
3
3
  import { defineRepository } from "@shivaedev/sql/defineRepository.ts";
4
+ import { owner } from "@shivaedev/sql/owner.ts";
4
5
  import { makeRepository } from "@shivaedev/sql/repository.ts";
5
6
  import { AccountPreferenceValues } from "#accountPreferenceValues.ts";
6
7
 
7
8
  class StoredPreferences extends Model.Class<StoredPreferences>("AccountPreferences")({
8
- id: Schema.String,
9
+ id: owner(Schema.String),
9
10
  ...AccountPreferenceValues.fields,
10
11
  }) {}
11
12
 
@@ -0,0 +1,26 @@
1
+ import { getAuthTables } from "better-auth/db";
2
+ import { Effect } from "effect";
3
+ import { checkCoverage } from "#server/account-data/checkCoverage.ts";
4
+ import { eraseOwnedRows } from "#server/account-data/eraseOwnedRows.ts";
5
+ import { exportOwnedRows } from "#server/account-data/exportOwnedRows.ts";
6
+ import type { TableColumn } from "#server/account-data/UncoveredUserColumns.ts";
7
+ import type { AuthSchemaDeclaration } from "#server/better-auth/define.ts";
8
+ import { profileExport } from "#server/better-auth/profileExport.ts";
9
+
10
+ export function accountData(auth: Pick<AuthSchemaDeclaration, "options">, reference?: TableColumn) {
11
+ const tables = getAuthTables(auth.options);
12
+ const user = reference ?? { column: "id", schema: "public", table: tables.user?.modelName ?? "user" };
13
+ const check = checkCoverage(user, new Set(Object.values(tables).map(({ modelName }) => modelName)));
14
+ const exportProfile = profileExport(auth.options, user.schema);
15
+ return {
16
+ check,
17
+ eraseOwnedRows: Effect.fn("AccountData.eraseOwnedRows")(function* (userId: string) {
18
+ yield* check;
19
+ yield* eraseOwnedRows(userId);
20
+ }),
21
+ exportAccount: Effect.fn("AccountData.exportAccount")(function* (userId: string) {
22
+ yield* check;
23
+ return { account: yield* exportProfile(userId), owned: yield* exportOwnedRows(userId) };
24
+ }),
25
+ };
26
+ }
@@ -1,6 +1,7 @@
1
1
  import type { BetterAuthOptions, BetterAuthPlugin } from "better-auth";
2
2
  import { createAuthMiddleware } from "better-auth/api";
3
3
  import { Effect, Option, Schema } from "effect";
4
+ import { accountPreferenceHeaders } from "#accountPreferenceHeaders.ts";
4
5
  import { AccountPreferenceValues } from "#accountPreferenceValues.ts";
5
6
  import { AccountPreferencesStore } from "#server/account-preferences/service.ts";
6
7
 
@@ -10,8 +11,8 @@ export const accountPreferenceOptions = Effect.fnUntraced(function* <TOptions ex
10
11
  const capture = createAuthMiddleware(async (context) => {
11
12
  const session = context.context.newSession ?? context.context.session;
12
13
  const values = Schema.decodeUnknownOption(AccountPreferenceValues)({
13
- locale: context.headers?.get("x-platform-account-locale"),
14
- timezone: context.headers?.get("x-platform-account-timezone"),
14
+ locale: context.headers?.get(accountPreferenceHeaders.locale),
15
+ timezone: context.headers?.get(accountPreferenceHeaders.timezone),
15
16
  units: "metric",
16
17
  });
17
18
  if (session !== null && session !== undefined && Option.isSome(values)) {
@@ -0,0 +1,55 @@
1
+ import type { BetterAuthOptions } from "better-auth";
2
+ import { getAuthTables } from "better-auth/db";
3
+ import { Effect, Schema } from "effect";
4
+ import { SqlClient, SqlSchema } from "effect/sql";
5
+
6
+ const Timestamps = { createdAt: Schema.DateTimeUtcFromDate, updatedAt: Schema.DateTimeUtcFromDate };
7
+ const User = Schema.Struct({
8
+ ...Timestamps,
9
+ email: Schema.String,
10
+ emailVerified: Schema.Boolean,
11
+ id: Schema.String,
12
+ image: Schema.NullOr(Schema.String),
13
+ name: Schema.String,
14
+ });
15
+ const Account = Schema.Struct({ ...Timestamps, accountId: Schema.String, id: Schema.String, providerId: Schema.String, userId: Schema.String });
16
+ const Session = Schema.Struct({
17
+ ...Timestamps,
18
+ expiresAt: Schema.DateTimeUtcFromDate,
19
+ id: Schema.String,
20
+ ipAddress: Schema.NullOr(Schema.String),
21
+ userAgent: Schema.NullOr(Schema.String),
22
+ userId: Schema.String,
23
+ });
24
+
25
+ // Selects each field it exports, so passwords and provider or session tokens stay out of a person's export.
26
+ export function profileExport(options: Omit<BetterAuthOptions, "database">, schema: string) {
27
+ const models = getAuthTables(options);
28
+ return Effect.fn("BetterAuth.profileExport")(function* (userId: string) {
29
+ const sql = (yield* SqlClient.SqlClient).withoutTransforms();
30
+ function select(model: string, fields: readonly string[], owner: string, id: string) {
31
+ const table = models[model];
32
+ function column(name: string) {
33
+ return table?.fields[name]?.fieldName ?? name;
34
+ }
35
+ const columns = sql.csv(fields.map((name) => sql`${sql(column(name))} as ${sql(name)}`));
36
+ return sql`select ${columns} from ${sql(`${schema}.${table?.modelName ?? model}`)} where ${sql(column(owner))} = ${id} order by id`;
37
+ }
38
+ const users = yield* SqlSchema.findAll({
39
+ execute: (id) => select("user", Object.keys(User.fields), "id", id),
40
+ Request: Schema.String,
41
+ Result: User,
42
+ })(userId);
43
+ const accounts = yield* SqlSchema.findAll({
44
+ execute: (id) => select("account", Object.keys(Account.fields), "userId", id),
45
+ Request: Schema.String,
46
+ Result: Account,
47
+ })(userId);
48
+ const sessions = yield* SqlSchema.findAll({
49
+ execute: (id) => select("session", Object.keys(Session.fields), "userId", id),
50
+ Request: Schema.String,
51
+ Result: Session,
52
+ })(userId);
53
+ return { accounts, sessions, users };
54
+ });
55
+ }
@@ -1,15 +0,0 @@
1
- import { Effect, Schema } from "effect";
2
- import { SqlClient } from "effect/sql";
3
- import type { UserDataExport, UserExportReference } from "#server/userDataExport.ts";
4
- declare const UnregisteredUserExportTables_base: Schema.Class<UnregisteredUserExportTables, Schema.TaggedStruct<"UnregisteredUserExportTables", {
5
- readonly tables: Schema.$Array<Schema.Struct<{
6
- readonly schema: Schema.String;
7
- readonly table: Schema.String;
8
- }>>;
9
- }>, import("effect/Cause").YieldableError>;
10
- export declare class UnregisteredUserExportTables extends UnregisteredUserExportTables_base {
11
- get message(): string;
12
- }
13
- export declare const checkUserDataExports: (registrations: Readonly<Record<string, UserDataExport>>, reference: UserExportReference) => Effect.Effect<undefined, Schema.SchemaError | import("effect/sql/SqlError").SqlError | UnregisteredUserExportTables, SqlClient.SqlClient>;
14
- export {};
15
- //# sourceMappingURL=check.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"check.d.ts","sourceRoot":"","sources":["../../../src/server/account-export/check.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,SAAS,EAAa,MAAM,YAAY,CAAC;AAClD,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;;;;;;;AAErF,qBAAa,4BAA6B,SAAQ,iCAEhD;IACD,IAAa,OAAO,WAEnB;CACD;AAED,eAAO,MAAM,oBAAoB,wOAyB/B,CAAC"}
@@ -1,33 +0,0 @@
1
- import { Effect, Schema } from "effect";
2
- import { SqlClient, SqlSchema } from "effect/sql";
3
- export class UnregisteredUserExportTables extends Schema.TaggedError()("UnregisteredUserExportTables", {
4
- tables: Schema.Array(Schema.Struct({ schema: Schema.String, table: Schema.String })),
5
- }) {
6
- get message() {
7
- return `User data export registrations are missing for: ${this.tables.map(({ schema, table }) => `${schema}.${table}`).join(", ")}`;
8
- }
9
- }
10
- export const checkUserDataExports = Effect.fn("Identity.checkUserDataExports")(function* (registrations, reference) {
11
- const sql = (yield* SqlClient.SqlClient).withoutTransforms();
12
- const tables = yield* SqlSchema.findAll({
13
- execute: () => sql `
14
- select distinct source_schema.nspname as schema, source.relname as table
15
- from pg_catalog.pg_constraint fk
16
- join pg_catalog.pg_class source on source.oid = fk.conrelid
17
- join pg_catalog.pg_namespace source_schema on source_schema.oid = source.relnamespace
18
- join pg_catalog.pg_class target on target.oid = fk.confrelid
19
- join pg_catalog.pg_namespace target_schema on target_schema.oid = target.relnamespace
20
- join pg_catalog.pg_attribute target_column on target_column.attrelid = target.oid and target_column.attnum = any(fk.confkey)
21
- where fk.contype = 'f' and target_schema.nspname = ${reference.schema}
22
- and target.relname = ${reference.table} and target_column.attname = ${reference.column}
23
- order by schema, "table"`,
24
- Request: Schema.Void,
25
- Result: Schema.Struct({ schema: Schema.String, table: Schema.String }),
26
- })(undefined);
27
- const registered = Object.values(registrations).flatMap((registration) => registration.tables);
28
- const missing = tables.filter((table) => !registered.some((entry) => entry.schema === table.schema && entry.table === table.table));
29
- if (missing.length > 0) {
30
- return yield* new UnregisteredUserExportTables({ tables: missing });
31
- }
32
- });
33
- //# sourceMappingURL=check.js.map
@@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AK,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;KACtE,CAAC,CAAC,SAAS,CAAC,CAAC;IACd,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;IAC/F,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,KAAK,KAAK,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;IACpI,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxB,OAAO,KAAK,CAAC,CAAC,IAAI,4BAA4B,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;IACrE,CAAC;AACF,CAAC,CAAC,CAAC"}
@@ -1,14 +0,0 @@
1
- import { Effect } from "effect";
2
- import { checkUserDataExports } from "#server/account-export/check.ts";
3
- import type { UserDataExport, UserExportReference } from "#server/userDataExport.ts";
4
- type ExportEffects<T extends Readonly<Record<string, UserDataExport>>> = ReturnType<T[keyof T]["export"]>;
5
- type ExportCoverage = ReturnType<typeof checkUserDataExports>;
6
- type ExportData<T extends Readonly<Record<string, UserDataExport>>> = {
7
- readonly [TDomain in keyof T]: Effect.Success<ReturnType<T[TDomain]["export"]>>;
8
- };
9
- export declare function accountExport<const T extends Readonly<Record<string, UserDataExport>>>(registrations: T, reference?: UserExportReference): {
10
- readonly check: ExportCoverage;
11
- readonly exportUser: (userId: string) => Effect.Effect<ExportData<T>, Effect.Error<ExportEffects<T>> | Effect.Error<ExportCoverage>, Effect.Services<ExportEffects<T>> | Effect.Services<ExportCoverage>>;
12
- };
13
- export {};
14
- //# sourceMappingURL=accountExport.d.ts.map
@@ -1 +0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G,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAC,CACnE,CAAC;CACF,CAAC"}
@@ -1,15 +0,0 @@
1
- import { Effect } from "effect";
2
- import { checkUserDataExports } from "#server/account-export/check.ts";
3
- import { userReference } from "#server/better-auth/userReference.ts";
4
- export function accountExport(registrations, reference = { ...userReference, schema: "public" }) {
5
- const check = checkUserDataExports(registrations, reference);
6
- return {
7
- check,
8
- exportUser: Effect.fn("Identity.exportUser")(function* (userId) {
9
- yield* check;
10
- const entries = yield* Effect.forEach(Object.entries(registrations), ([domain, registration]) => registration.export(userId).pipe(Effect.map((data) => [domain, data])));
11
- return Object.fromEntries(entries);
12
- }),
13
- };
14
- }
15
- //# sourceMappingURL=accountExport.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"accountExport.js","sourceRoot":"","sources":["../../src/server/accountExport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAsBrE,MAAM,UAAU,aAAa,CAC5B,aAAuD,EACvD,SAAS,GAAwB,EAAE,GAAG,aAAa,EAAE,MAAM,EAAE,QAAQ,EAAE;IAEvE,MAAM,KAAK,GAAG,oBAAoB,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;IAC7D,OAAO;QACN,KAAK;QACL,UAAU,EAAE,MAAM,CAAC,EAAE,CAAC,qBAAqB,CAAC,CAAC,QAAQ,CAAC,EAAE,MAAc;YACrE,KAAK,CAAC,CAAC,KAAK,CAAC;YACb,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,YAAY,CAAC,EAAE,EAAE,CAC/F,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,IAAI,CAAU,CAAC,CAAC,CAC/E,CAAC;YACF,OAAO,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QACpC,CAAC,CAAC;KACF,CAAC;AACH,CAAC"}
@@ -1,3 +0,0 @@
1
- import type { BetterAuthClientPlugin } from "better-auth/client";
2
- export declare function preferencesClient(): Pick<BetterAuthClientPlugin, "id" | "fetchPlugins">;
3
- //# sourceMappingURL=preferencesClient.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"preferencesClient.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/preferencesClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAEjE,wBAAgB,iBAAiB,IAAI,IAAI,CAAC,sBAAsB,EAAE,IAAI,GAAG,cAAc,CAAC,CAsBvF"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"preferencesClient.js","sourceRoot":"","sources":["../../../src/server/better-auth/preferencesClient.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,iBAAiB;IAChC,OAAO;QACN,YAAY,EAAE;YACb;gBACC,KAAK,EAAE;oBACN,SAAS,CAAC,OAAO;wBAChB,IAAI,CAAC,CAAC,QAAQ,IAAI,UAAU,CAAC,EAAE,CAAC;4BAC/B,OAAO,OAAO,CAAC;wBAChB,CAAC;wBACD,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC,eAAe,EAAE,CAAC;wBACzE,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;wBAC7C,OAAO,CAAC,GAAG,CAAC,6BAA6B,EAAE,QAAQ,CAAC,CAAC;wBACrD,OAAO,CAAC,GAAG,CAAC,2BAA2B,EAAE,MAAM,CAAC,CAAC;wBACjD,OAAO,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE,CAAC;oBAChC,CAAC;iBACD;gBACD,EAAE,EAAE,8BAA8B;gBAClC,IAAI,EAAE,8BAA8B;aACpC;SACD;QACD,EAAE,EAAE,8BAA8B;KAClC,CAAC;AACH,CAAC"}
@@ -1,38 +0,0 @@
1
- import type { BetterAuthOptions } from "better-auth";
2
- import { Effect, Schema } from "effect";
3
- import { SqlClient } from "effect/sql";
4
- export declare function betterAuthUserDataExport(options?: BetterAuthOptions, schema?: string): {
5
- export: (userId: string) => Effect.Effect<{
6
- accounts: {
7
- readonly createdAt: import("effect/DateTime").Utc;
8
- readonly updatedAt: import("effect/DateTime").Utc;
9
- readonly accountId: string;
10
- readonly id: string;
11
- readonly providerId: string;
12
- readonly userId: string;
13
- }[];
14
- sessions: {
15
- readonly createdAt: import("effect/DateTime").Utc;
16
- readonly updatedAt: import("effect/DateTime").Utc;
17
- readonly expiresAt: import("effect/DateTime").Utc;
18
- readonly id: string;
19
- readonly ipAddress: string | null;
20
- readonly userAgent: string | null;
21
- readonly userId: string;
22
- }[];
23
- users: {
24
- readonly createdAt: import("effect/DateTime").Utc;
25
- readonly updatedAt: import("effect/DateTime").Utc;
26
- readonly email: string;
27
- readonly emailVerified: boolean;
28
- readonly id: string;
29
- readonly image: string | null;
30
- readonly name: string;
31
- }[];
32
- }, Schema.SchemaError | import("effect/sql/SqlError").SqlError, SqlClient.SqlClient>;
33
- tables: {
34
- schema: string;
35
- table: string;
36
- }[];
37
- };
38
- //# sourceMappingURL=userDataExport.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"userDataExport.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/userDataExport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,SAAS,EAAa,MAAM,YAAY,CAAC;AAqBlD,wBAAgB,wBAAwB,CAAC,OAAO,GAAE,iBAAsB,EAAE,MAAM,SAAW;IAGzF,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA2BN,MAAM;;;;EAEP"}
@@ -1,55 +0,0 @@
1
- import { getAuthTables } from "better-auth/db";
2
- import { Effect, Schema } from "effect";
3
- import { SqlClient, SqlSchema } from "effect/sql";
4
- const Timestamps = { createdAt: Schema.DateTimeUtcFromDate, updatedAt: Schema.DateTimeUtcFromDate };
5
- const User = Schema.Struct({
6
- ...Timestamps,
7
- email: Schema.String,
8
- emailVerified: Schema.Boolean,
9
- id: Schema.String,
10
- image: Schema.NullOr(Schema.String),
11
- name: Schema.String,
12
- });
13
- const Account = Schema.Struct({ ...Timestamps, accountId: Schema.String, id: Schema.String, providerId: Schema.String, userId: Schema.String });
14
- const Session = Schema.Struct({
15
- ...Timestamps,
16
- expiresAt: Schema.DateTimeUtcFromDate,
17
- id: Schema.String,
18
- ipAddress: Schema.NullOr(Schema.String),
19
- userAgent: Schema.NullOr(Schema.String),
20
- userId: Schema.String,
21
- });
22
- export function betterAuthUserDataExport(options = {}, schema = "public") {
23
- const models = getAuthTables(options);
24
- return {
25
- export: Effect.fn("BetterAuth.export")(function* (userId) {
26
- const sql = (yield* SqlClient.SqlClient).withoutTransforms();
27
- function select(model, fields, owner, id) {
28
- const table = models[model];
29
- function column(name) {
30
- return table?.fields[name]?.fieldName ?? name;
31
- }
32
- const columns = sql.csv(fields.map((name) => sql `${sql(column(name))} as ${sql(name)}`));
33
- return sql `select ${columns} from ${sql(`${schema}.${table?.modelName ?? model}`)} where ${sql(column(owner))} = ${id} order by id`;
34
- }
35
- const users = yield* SqlSchema.findAll({
36
- execute: (id) => select("user", Object.keys(User.fields), "id", id),
37
- Request: Schema.String,
38
- Result: User,
39
- })(userId);
40
- const accounts = yield* SqlSchema.findAll({
41
- execute: (id) => select("account", Object.keys(Account.fields), "userId", id),
42
- Request: Schema.String,
43
- Result: Account,
44
- })(userId);
45
- const sessions = yield* SqlSchema.findAll({
46
- execute: (id) => select("session", Object.keys(Session.fields), "userId", id),
47
- Request: Schema.String,
48
- Result: Session,
49
- })(userId);
50
- return { accounts, sessions, users };
51
- }),
52
- tables: ["user", "account", "session"].map((model) => ({ schema, table: models[model]?.modelName ?? model })),
53
- };
54
- }
55
- //# sourceMappingURL=userDataExport.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"userDataExport.js","sourceRoot":"","sources":["../../../src/server/better-auth/userDataExport.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAElD,MAAM,UAAU,GAAG,EAAE,SAAS,EAAE,MAAM,CAAC,mBAAmB,EAAE,SAAS,EAAE,MAAM,CAAC,mBAAmB,EAAE,CAAC;AACpG,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC;IAC1B,GAAG,UAAU;IACb,KAAK,EAAE,MAAM,CAAC,MAAM;IACpB,aAAa,EAAE,MAAM,CAAC,OAAO;IAC7B,EAAE,EAAE,MAAM,CAAC,MAAM;IACjB,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC;IACnC,IAAI,EAAE,MAAM,CAAC,MAAM;CACnB,CAAC,CAAC;AACH,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;AAChJ,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC;IAC7B,GAAG,UAAU;IACb,SAAS,EAAE,MAAM,CAAC,m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AC;KAC7G,CAAC;AACH,CAAC"}
@@ -1,13 +0,0 @@
1
- import type { Effect } from "effect";
2
- export interface UserExportTable {
3
- readonly schema: string;
4
- readonly table: string;
5
- }
6
- export interface UserExportReference extends UserExportTable {
7
- readonly column: string;
8
- }
9
- export interface UserDataExport {
10
- readonly export: (userId: string) => Effect.Effect<unknown, unknown, unknown>;
11
- readonly tables: readonly UserExportTable[];
12
- }
13
- //# sourceMappingURL=userDataExport.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"userDataExport.d.ts","sourceRoot":"","sources":["../../src/server/userDataExport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,mBAAoB,SAAQ,eAAe;IAC3D,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,cAAc;IAC9B,QAAQ,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IAC9E,QAAQ,CAAC,MAAM,EAAE,SAAS,eAAe,EAAE,CAAC;CAC5C"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=userDataExport.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"userDataExport.js","sourceRoot":"","sources":["../../src/server/userDataExport.ts"],"names":[],"mappings":""}
@@ -1,38 +0,0 @@
1
- import { Effect, Schema } from "effect";
2
- import { SqlClient, SqlSchema } from "effect/sql";
3
- import type { UserDataExport, UserExportReference } from "#server/userDataExport.ts";
4
-
5
- export class UnregisteredUserExportTables extends Schema.TaggedError<UnregisteredUserExportTables>()("UnregisteredUserExportTables", {
6
- tables: Schema.Array(Schema.Struct({ schema: Schema.String, table: Schema.String })),
7
- }) {
8
- override get message() {
9
- return `User data export registrations are missing for: ${this.tables.map(({ schema, table }) => `${schema}.${table}`).join(", ")}`;
10
- }
11
- }
12
-
13
- export const checkUserDataExports = Effect.fn("Identity.checkUserDataExports")(function* (
14
- registrations: Readonly<Record<string, UserDataExport>>,
15
- reference: UserExportReference,
16
- ) {
17
- const sql = (yield* SqlClient.SqlClient).withoutTransforms();
18
- const tables = yield* SqlSchema.findAll({
19
- execute: () => sql`
20
- select distinct source_schema.nspname as schema, source.relname as table
21
- from pg_catalog.pg_constraint fk
22
- join pg_catalog.pg_class source on source.oid = fk.conrelid
23
- join pg_catalog.pg_namespace source_schema on source_schema.oid = source.relnamespace
24
- join pg_catalog.pg_class target on target.oid = fk.confrelid
25
- join pg_catalog.pg_namespace target_schema on target_schema.oid = target.relnamespace
26
- join pg_catalog.pg_attribute target_column on target_column.attrelid = target.oid and target_column.attnum = any(fk.confkey)
27
- where fk.contype = 'f' and target_schema.nspname = ${reference.schema}
28
- and target.relname = ${reference.table} and target_column.attname = ${reference.column}
29
- order by schema, "table"`,
30
- Request: Schema.Void,
31
- Result: Schema.Struct({ schema: Schema.String, table: Schema.String }),
32
- })(undefined);
33
- const registered = Object.values(registrations).flatMap((registration) => registration.tables);
34
- const missing = tables.filter((table) => !registered.some((entry) => entry.schema === table.schema && entry.table === table.table));
35
- if (missing.length > 0) {
36
- return yield* new UnregisteredUserExportTables({ tables: missing });
37
- }
38
- });
@@ -1,40 +0,0 @@
1
- import { Effect } from "effect";
2
- import { checkUserDataExports } from "#server/account-export/check.ts";
3
- import { userReference } from "#server/better-auth/userReference.ts";
4
- import type { UserDataExport, UserExportReference } from "#server/userDataExport.ts";
5
-
6
- type ExportEffects<T extends Readonly<Record<string, UserDataExport>>> = ReturnType<T[keyof T]["export"]>;
7
- type ExportCoverage = ReturnType<typeof checkUserDataExports>;
8
- type ExportData<T extends Readonly<Record<string, UserDataExport>>> = {
9
- readonly [TDomain in keyof T]: Effect.Success<ReturnType<T[TDomain]["export"]>>;
10
- };
11
-
12
- export function accountExport<const T extends Readonly<Record<string, UserDataExport>>>(
13
- registrations: T,
14
- reference?: UserExportReference,
15
- ): {
16
- readonly check: ExportCoverage;
17
- readonly exportUser: (
18
- userId: string,
19
- ) => Effect.Effect<
20
- ExportData<T>,
21
- Effect.Error<ExportEffects<T>> | Effect.Error<ExportCoverage>,
22
- Effect.Services<ExportEffects<T>> | Effect.Services<ExportCoverage>
23
- >;
24
- };
25
- export function accountExport(
26
- registrations: Readonly<Record<string, UserDataExport>>,
27
- reference: UserExportReference = { ...userReference, schema: "public" },
28
- ) {
29
- const check = checkUserDataExports(registrations, reference);
30
- return {
31
- check,
32
- exportUser: Effect.fn("Identity.exportUser")(function* (userId: string) {
33
- yield* check;
34
- const entries = yield* Effect.forEach(Object.entries(registrations), ([domain, registration]) =>
35
- registration.export(userId).pipe(Effect.map((data) => [domain, data] as const)),
36
- );
37
- return Object.fromEntries(entries);
38
- }),
39
- };
40
- }
@@ -1,25 +0,0 @@
1
- import type { BetterAuthClientPlugin } from "better-auth/client";
2
-
3
- export function preferencesClient(): Pick<BetterAuthClientPlugin, "id" | "fetchPlugins"> {
4
- return {
5
- fetchPlugins: [
6
- {
7
- hooks: {
8
- onRequest(context) {
9
- if (!("window" in globalThis)) {
10
- return context;
11
- }
12
- const { locale, timeZone } = new Intl.DateTimeFormat().resolvedOptions();
13
- const headers = new Headers(context.headers);
14
- headers.set("x-platform-account-timezone", timeZone);
15
- headers.set("x-platform-account-locale", locale);
16
- return { ...context, headers };
17
- },
18
- },
19
- id: "identity-account-preferences",
20
- name: "identity-account-preferences",
21
- },
22
- ],
23
- id: "identity-account-preferences",
24
- };
25
- }
@@ -1,57 +0,0 @@
1
- import type { BetterAuthOptions } from "better-auth";
2
- import { getAuthTables } from "better-auth/db";
3
- import { Effect, Schema } from "effect";
4
- import { SqlClient, SqlSchema } from "effect/sql";
5
-
6
- const Timestamps = { createdAt: Schema.DateTimeUtcFromDate, updatedAt: Schema.DateTimeUtcFromDate };
7
- const User = Schema.Struct({
8
- ...Timestamps,
9
- email: Schema.String,
10
- emailVerified: Schema.Boolean,
11
- id: Schema.String,
12
- image: Schema.NullOr(Schema.String),
13
- name: Schema.String,
14
- });
15
- const Account = Schema.Struct({ ...Timestamps, accountId: Schema.String, id: Schema.String, providerId: Schema.String, userId: Schema.String });
16
- const Session = Schema.Struct({
17
- ...Timestamps,
18
- expiresAt: Schema.DateTimeUtcFromDate,
19
- id: Schema.String,
20
- ipAddress: Schema.NullOr(Schema.String),
21
- userAgent: Schema.NullOr(Schema.String),
22
- userId: Schema.String,
23
- });
24
-
25
- export function betterAuthUserDataExport(options: BetterAuthOptions = {}, schema = "public") {
26
- const models = getAuthTables(options);
27
- return {
28
- export: Effect.fn("BetterAuth.export")(function* (userId: string) {
29
- const sql = (yield* SqlClient.SqlClient).withoutTransforms();
30
- function select(model: string, fields: readonly string[], owner: string, id: string) {
31
- const table = models[model];
32
- function column(name: string) {
33
- return table?.fields[name]?.fieldName ?? name;
34
- }
35
- const columns = sql.csv(fields.map((name) => sql`${sql(column(name))} as ${sql(name)}`));
36
- return sql`select ${columns} from ${sql(`${schema}.${table?.modelName ?? model}`)} where ${sql(column(owner))} = ${id} order by id`;
37
- }
38
- const users = yield* SqlSchema.findAll({
39
- execute: (id) => select("user", Object.keys(User.fields), "id", id),
40
- Request: Schema.String,
41
- Result: User,
42
- })(userId);
43
- const accounts = yield* SqlSchema.findAll({
44
- execute: (id) => select("account", Object.keys(Account.fields), "userId", id),
45
- Request: Schema.String,
46
- Result: Account,
47
- })(userId);
48
- const sessions = yield* SqlSchema.findAll({
49
- execute: (id) => select("session", Object.keys(Session.fields), "userId", id),
50
- Request: Schema.String,
51
- Result: Session,
52
- })(userId);
53
- return { accounts, sessions, users };
54
- }),
55
- tables: ["user", "account", "session"].map((model) => ({ schema, table: models[model]?.modelName ?? model })),
56
- };
57
- }
@@ -1,15 +0,0 @@
1
- import type { Effect } from "effect";
2
-
3
- export interface UserExportTable {
4
- readonly schema: string;
5
- readonly table: string;
6
- }
7
-
8
- export interface UserExportReference extends UserExportTable {
9
- readonly column: string;
10
- }
11
-
12
- export interface UserDataExport {
13
- readonly export: (userId: string) => Effect.Effect<unknown, unknown, unknown>;
14
- readonly tables: readonly UserExportTable[];
15
- }