@vexcms/better-auth 0.1.0-alpha.2 → 0.1.0-alpha.21

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/dist/index.js CHANGED
@@ -1,205 +1,13 @@
1
- // src/adapter.ts
2
1
  import {
3
- array,
4
- checkbox,
5
- date,
6
- defineCollection,
7
- number,
8
- select,
9
- text
10
- } from "@vexcms/core";
11
- import { getAuthTables } from "better-auth/db";
12
- var AUTH_COLLECTION_TYPES = {
13
- user: "user",
14
- session: "session",
15
- account: "account",
16
- verification: "verification"
17
- };
18
- var EDITABLE_FIELDS = /* @__PURE__ */ new Set([
19
- "name",
20
- "email",
21
- "image",
22
- "role",
23
- "banned",
24
- "banReason",
25
- "banExpires"
26
- ]);
27
- var HIDDEN_FIELDS = /* @__PURE__ */ new Set([
28
- "hashedPassword",
29
- "password",
30
- "twoFactorSecret",
31
- "twoFactorBackupCodes",
32
- "token",
33
- "secret",
34
- "code"
35
- ]);
36
- function betterAuthAdapter(props) {
37
- const tables = getAuthTables(props?.config ?? {});
38
- const collections = [];
39
- const authCollections = [
40
- { type: AUTH_COLLECTION_TYPES.user, slug: props?.config?.user?.modelName },
41
- {
42
- type: AUTH_COLLECTION_TYPES.session,
43
- slug: props?.config?.session?.modelName
44
- },
45
- {
46
- type: AUTH_COLLECTION_TYPES.account,
47
- slug: props?.config?.account?.modelName
48
- },
49
- {
50
- type: AUTH_COLLECTION_TYPES.verification,
51
- slug: props?.config?.verification?.modelName
52
- }
53
- ];
54
- for (const [tableKey, tableDef] of Object.entries(tables)) {
55
- const slug = tableDef.modelName ?? tableKey;
56
- const fields = {};
57
- addAuthCollectionFields({
58
- attributes: tableDef.fields,
59
- slug,
60
- fields,
61
- config: props?.config,
62
- extractId: true
63
- });
64
- const authCollectionType = authCollections.find(
65
- (ac) => ac.slug === slug
66
- )?.type;
67
- if (authCollectionType) {
68
- switch (authCollectionType) {
69
- case "user": {
70
- const additionalFields = props?.config?.user?.additionalFields;
71
- addAuthCollectionFields({
72
- attributes: additionalFields,
73
- slug,
74
- fields,
75
- config: props?.config
76
- });
77
- break;
78
- }
79
- case "session": {
80
- const additionalFields = props?.config?.session?.additionalFields;
81
- addAuthCollectionFields({
82
- attributes: additionalFields,
83
- slug,
84
- fields,
85
- config: props?.config
86
- });
87
- break;
88
- }
89
- case "account": {
90
- const additionalFields = props?.config?.account?.additionalFields;
91
- addAuthCollectionFields({
92
- attributes: additionalFields,
93
- slug,
94
- fields,
95
- config: props?.config
96
- });
97
- break;
98
- }
99
- case "verification": {
100
- const additionalFields = props?.config?.verification?.additionalFields;
101
- addAuthCollectionFields({
102
- attributes: additionalFields,
103
- slug,
104
- fields,
105
- config: props?.config
106
- });
107
- break;
108
- }
109
- default:
110
- break;
111
- }
112
- }
113
- const isProtected = slug !== "user" && slug !== "users";
114
- collections.push(
115
- defineCollection({
116
- slug,
117
- fields,
118
- meta: isProtected ? { protected: true } : void 0
119
- })
120
- );
121
- }
122
- const userSlug = tables.user?.modelName ?? "user";
123
- return {
124
- name: "better-auth",
125
- collections,
126
- userCollection: userSlug
127
- };
128
- }
129
- function addAuthCollectionFields(props) {
130
- if (!props.attributes) return;
131
- for (const [fieldName, attr] of Object.entries(props.attributes)) {
132
- if (props.extractId === true && fieldName === "id") continue;
133
- const field = betterAuthAttrToVexField(
134
- fieldName,
135
- attr,
136
- props.slug,
137
- props?.config
138
- );
139
- if (field) props.fields[fieldName] = field;
140
- }
141
- }
142
- function betterAuthAttrToVexField(fieldName, attr, tableSlug, authOptions) {
143
- if (fieldName === "id") return null;
144
- const isUserTable = tableSlug === authOptions?.user?.modelName || tableSlug === "user" || tableSlug === "users";
145
- const isEditable = isUserTable && EDITABLE_FIELDS.has(fieldName);
146
- const isHidden = HIDDEN_FIELDS.has(fieldName);
147
- const admin = {};
148
- if (!isEditable) admin.readOnly = true;
149
- if (isHidden) admin.hidden = true;
150
- const baseOptions = {
151
- defaultValue: void 0
152
- };
153
- if (Object.keys(admin).length > 0) baseOptions.admin = admin;
154
- if (!isEditable) baseOptions.meta = { locked: true };
155
- if (attr.required) baseOptions.required = attr.required;
156
- if (attr.defaultValue !== void 0 && typeof attr.defaultValue !== "function") {
157
- baseOptions.defaultValue = attr.defaultValue;
158
- }
159
- if (attr.unique || attr.index) {
160
- baseOptions.index = `by_${fieldName}`;
161
- }
162
- if (attr.references) {
163
- return text(baseOptions);
164
- }
165
- if (Array.isArray(attr.type)) {
166
- return select({
167
- options: attr.type.map((v2) => ({ value: v2, label: v2 })),
168
- ...baseOptions
169
- });
170
- }
171
- switch (attr.type) {
172
- case "string":
173
- return text(baseOptions);
174
- case "string[]":
175
- return array({
176
- ...baseOptions,
177
- items: text(baseOptions)
178
- });
179
- case "boolean":
180
- return checkbox(baseOptions);
181
- case "number":
182
- return number(baseOptions);
183
- case "number[]":
184
- return array({
185
- ...baseOptions,
186
- items: number(baseOptions)
187
- });
188
- case "date":
189
- return date(baseOptions);
190
- case "json":
191
- return text(baseOptions);
192
- default:
193
- return null;
194
- }
195
- }
2
+ betterAuthAdapter
3
+ } from "./chunk-YPIEILO5.js";
196
4
 
197
5
  // src/convex/adapter.ts
198
6
  import { anyApi } from "convex/server";
199
7
  import {
200
8
  createAdapterFactory
201
9
  } from "better-auth/adapters";
202
- import { getAuthTables as getAuthTables2 } from "better-auth/db";
10
+ import { getAuthTables } from "better-auth/db";
203
11
  var parseWhere = (where) => {
204
12
  return where?.map((where2) => {
205
13
  if (where2.value instanceof Date) {
@@ -212,25 +20,25 @@ function convexAdapter(ctx, config = {}) {
212
20
  return createAdapterFactory({
213
21
  adapter: ({ options }) => {
214
22
  options.telemetry = { enabled: false };
215
- const betterAuthSchema = getAuthTables2(options);
23
+ const betterAuthSchema = getAuthTables(options);
216
24
  const betterAuthSchemaJson = JSON.stringify(betterAuthSchema);
217
25
  return {
218
26
  id: "convex",
219
27
  options: { isRunMutationCtx: "runMutation" in ctx },
220
28
  // Use anyApi to reference db operations, call via ctx.runMutation/ctx.runQuery
221
- create: async ({ data, model, select: select2 }) => {
29
+ create: async ({ data, model, select }) => {
222
30
  return await ctx.runMutation(anyApi.auth.db.dbCreate, {
223
31
  betterAuthSchema: betterAuthSchemaJson,
224
32
  data,
225
33
  model,
226
- select: select2
34
+ select
227
35
  });
228
36
  },
229
- findOne: async ({ model, select: select2, where }) => {
37
+ findOne: async ({ model, select, where }) => {
230
38
  return await ctx.runQuery(anyApi.auth.db.dbFindOne, {
231
39
  betterAuthSchema: betterAuthSchemaJson,
232
40
  model,
233
- select: select2,
41
+ select,
234
42
  where: parseWhere(where)
235
43
  });
236
44
  },
@@ -1691,14 +1499,14 @@ var checkUniqueFields = async (ctx, schema, betterAuthSchema, table, input, doc)
1691
1499
  }
1692
1500
  }
1693
1501
  };
1694
- var selectFields = (doc, select2) => {
1502
+ var selectFields = (doc, select) => {
1695
1503
  if (!doc) {
1696
1504
  return null;
1697
1505
  }
1698
- if (!select2?.length) {
1506
+ if (!select?.length) {
1699
1507
  return doc;
1700
1508
  }
1701
- return select2.reduce((acc, field) => {
1509
+ return select.reduce((acc, field) => {
1702
1510
  acc[field] = doc[field];
1703
1511
  return acc;
1704
1512
  }, {});
@@ -1960,7 +1768,7 @@ var create = {
1960
1768
  model: v.string(),
1961
1769
  select: v.optional(v.array(v.string()))
1962
1770
  },
1963
- handler: async (ctx, { betterAuthSchema, data, model, select: select2 }, schema) => {
1771
+ handler: async (ctx, { betterAuthSchema, data, model, select }, schema) => {
1964
1772
  const authSchema = getBetterAuthSchema(betterAuthSchema);
1965
1773
  await checkUniqueFields(ctx, schema, authSchema, model, data);
1966
1774
  const id = await ctx.db.insert(model, data);
@@ -1968,7 +1776,7 @@ var create = {
1968
1776
  if (!doc) {
1969
1777
  throw new Error(`Failed to create ${model}`);
1970
1778
  }
1971
- return selectFields(doc, select2);
1779
+ return selectFields(doc, select);
1972
1780
  }
1973
1781
  };
1974
1782
  var findOne = {
@@ -1978,11 +1786,11 @@ var findOne = {
1978
1786
  select: v.optional(v.array(v.string())),
1979
1787
  where: v.array(v.any())
1980
1788
  },
1981
- handler: async (ctx, { betterAuthSchema, model, select: select2, where }, schema) => {
1789
+ handler: async (ctx, { betterAuthSchema, model, select, where }, schema) => {
1982
1790
  const authSchema = getBetterAuthSchema(betterAuthSchema);
1983
1791
  const result = await listOne(ctx, schema, authSchema, {
1984
1792
  model,
1985
- select: select2,
1793
+ select,
1986
1794
  where
1987
1795
  });
1988
1796
  return result;
@@ -2201,17 +2009,29 @@ function createGetAuth(props) {
2201
2009
  if (user === null) {
2202
2010
  return { user };
2203
2011
  }
2204
- if (!props.resolveOrgs) {
2012
+ if (!props.resolveOrgs || !props.orgCollectionSlug) {
2205
2013
  return { user };
2206
2014
  }
2015
+ const orgCollectionSlug = props.orgCollectionSlug;
2207
2016
  const sessionId = identity.sessionId;
2208
2017
  const session = sessionId ? await ctx.db.get(props.sessionCollectionSlug, sessionId) : null;
2209
2018
  const orgId = session?.activeOrganizationId;
2210
- const organization = orgId ? await ctx.db.get(props.orgCollectionSlug, orgId) ?? void 0 : void 0;
2019
+ const organization = orgId ? await ctx.db.get(orgCollectionSlug, orgId) ?? void 0 : void 0;
2211
2020
  return { user, organization };
2212
2021
  };
2213
2022
  }
2214
2023
 
2024
+ // src/convex/anonRole.ts
2025
+ function anonRoleDatabaseHook(role) {
2026
+ return {
2027
+ create: {
2028
+ before: async (user) => ({
2029
+ data: user.isAnonymous === true ? { ...user, roles: [role] } : user
2030
+ })
2031
+ }
2032
+ };
2033
+ }
2034
+
2215
2035
  // src/convex/index.ts
2216
2036
  function authDbApi(options) {
2217
2037
  return {
@@ -2285,6 +2105,7 @@ function createBetterAuthAdapter(ctx) {
2285
2105
  return convexAdapter(ctx);
2286
2106
  }
2287
2107
  export {
2108
+ anonRoleDatabaseHook,
2288
2109
  authDbApi,
2289
2110
  betterAuthAdapter,
2290
2111
  convexAdapter,