better-auth 0.3.4-beta.1 → 0.3.4-beta.3

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.
@@ -1,8 +1,8 @@
1
- import { A as Adapter } from '../index-DUb6fNdH.js';
1
+ import { A as Adapter } from '../index-C9daNPfT.js';
2
+ import 'zod';
2
3
  import 'kysely';
3
4
  import '../index-CT4RHCuS.js';
4
5
  import 'arctic';
5
- import 'zod';
6
6
  import '../helper-DPDj8Nix.js';
7
7
  import 'better-call';
8
8
  import 'better-sqlite3';
@@ -72,7 +72,8 @@ var getAuthTables = (options) => {
72
72
  defaultValue: () => /* @__PURE__ */ new Date(),
73
73
  required: true
74
74
  },
75
- ...user?.fields
75
+ ...user?.fields,
76
+ ...options.user?.additionalFields
76
77
  },
77
78
  order: 0
78
79
  },
@@ -100,7 +101,8 @@ var getAuthTables = (options) => {
100
101
  },
101
102
  required: true
102
103
  },
103
- ...session?.fields
104
+ ...session?.fields,
105
+ ...options.session?.additionalFields
104
106
  },
105
107
  order: 1
106
108
  },
@@ -1,9 +1,9 @@
1
1
  import { Db } from 'mongodb';
2
- import { W as Where } from '../index-DUb6fNdH.js';
2
+ import { W as Where } from '../index-C9daNPfT.js';
3
+ import 'zod';
3
4
  import 'kysely';
4
5
  import '../index-CT4RHCuS.js';
5
6
  import 'arctic';
6
- import 'zod';
7
7
  import '../helper-DPDj8Nix.js';
8
8
  import 'better-call';
9
9
  import 'better-sqlite3';
@@ -1,8 +1,8 @@
1
- import { A as Adapter } from '../index-DUb6fNdH.js';
1
+ import { A as Adapter } from '../index-C9daNPfT.js';
2
+ import 'zod';
2
3
  import 'kysely';
3
4
  import '../index-CT4RHCuS.js';
4
5
  import 'arctic';
5
- import 'zod';
6
6
  import '../helper-DPDj8Nix.js';
7
7
  import 'better-call';
8
8
  import 'better-sqlite3';
package/dist/api.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export { f as AuthEndpoint, g as AuthMiddleware, n as callbackOAuth, L as changePassword, e as createAuthEndpoint, d as createAuthMiddleware, D as createEmailVerificationToken, V as csrfMiddleware, N as deleteUser, Q as error, y as forgetPassword, z as forgetPasswordCallback, O as getCSRFToken, l as getEndpoints, p as getSession, q as getSessionFromCtx, u as listSessions, T as ok, o as optionsMiddleware, C as resetPassword, v as revokeSession, w as revokeSessions, r as router, E as sendVerificationEmail, t as sessionMiddleware, M as setPassword, m as signInEmail, s as signInOAuth, x as signOut, U as signUpEmail, K as updateUser, J as verifyEmail } from './index-DUb6fNdH.js';
2
- import 'zod';
1
+ export { f as AuthEndpoint, g as AuthMiddleware, u as callbackOAuth, Q as changePassword, e as createAuthEndpoint, d as createAuthMiddleware, L as createEmailVerificationToken, _ as csrfMiddleware, U as deleteUser, X as error, E as forgetPassword, J as forgetPasswordCallback, V as getCSRFToken, q as getEndpoints, v as getSession, w as getSessionFromCtx, y as listSessions, Y as ok, o as optionsMiddleware, K as resetPassword, z as revokeSession, C as revokeSessions, r as router, M as sendVerificationEmail, x as sessionMiddleware, T as setPassword, t as signInEmail, s as signInOAuth, D as signOut, Z as signUpEmail, O as updateUser, N as verifyEmail } from './index-C9daNPfT.js';
3
2
  import './helper-DPDj8Nix.js';
3
+ import 'zod';
4
4
  export { APIError } from 'better-call';
5
5
  import 'kysely';
6
6
  import './index-CT4RHCuS.js';
package/dist/api.js CHANGED
@@ -773,13 +773,14 @@ var oAuthProviderList = Object.keys(oAuthProviders);
773
773
  // src/utils/state.ts
774
774
  import { generateState as generateStateOAuth } from "oslo/oauth2";
775
775
  import { z as z2 } from "zod";
776
- function generateState(callbackURL, currentURL, dontRememberMe) {
776
+ function generateState(callbackURL, currentURL, dontRememberMe, additionalFields) {
777
777
  const code = generateStateOAuth();
778
778
  const state = JSON.stringify({
779
779
  code,
780
780
  callbackURL,
781
781
  currentURL,
782
- dontRememberMe
782
+ dontRememberMe,
783
+ additionalFields
783
784
  });
784
785
  return { state, code };
785
786
  }
@@ -788,7 +789,8 @@ function parseState(state) {
788
789
  code: z2.string(),
789
790
  callbackURL: z2.string().optional(),
790
791
  currentURL: z2.string().optional(),
791
- dontRememberMe: z2.boolean().optional()
792
+ dontRememberMe: z2.boolean().optional(),
793
+ additionalFields: z2.record(z2.string()).optional()
792
794
  }).safeParse(JSON.parse(state));
793
795
  return data;
794
796
  }
@@ -833,35 +835,6 @@ function deleteSessionCookie(ctx) {
833
835
 
834
836
  // src/api/routes/session.ts
835
837
  import { z as z3 } from "zod";
836
-
837
- // src/utils/get-request-ip.ts
838
- function getIp(req) {
839
- const testIP = "127.0.0.1";
840
- if (process.env.NODE_ENV === "test") {
841
- return testIP;
842
- }
843
- const headers = [
844
- "x-client-ip",
845
- "x-forwarded-for",
846
- "cf-connecting-ip",
847
- "fastly-client-ip",
848
- "x-real-ip",
849
- "x-cluster-client-ip",
850
- "x-forwarded",
851
- "forwarded-for",
852
- "forwarded"
853
- ];
854
- for (const header of headers) {
855
- const value = req.headers.get(header);
856
- if (typeof value === "string") {
857
- const ip = value.split(",")[0].trim();
858
- if (ip) return ip;
859
- }
860
- }
861
- return null;
862
- }
863
-
864
- // src/api/routes/session.ts
865
838
  var getSession = () => createAuthEndpoint(
866
839
  "/session",
867
840
  {
@@ -1245,6 +1218,40 @@ var verificationSchema = z5.object({
1245
1218
  expiresAt: z5.date(),
1246
1219
  identifier: z5.string()
1247
1220
  });
1221
+ function getAllFields(options, table) {
1222
+ let schema = {
1223
+ ...table === "user" ? options.user?.additionalFields : {},
1224
+ ...table === "session" ? options.session?.additionalFields : {}
1225
+ };
1226
+ for (const plugin of options.plugins || []) {
1227
+ if (plugin.schema && plugin.schema[table]) {
1228
+ schema = {
1229
+ ...schema,
1230
+ ...plugin.schema[table].fields
1231
+ };
1232
+ }
1233
+ }
1234
+ return schema;
1235
+ }
1236
+ function parseInputData(data, schema) {
1237
+ const fields = schema.fields;
1238
+ const parsedData = {};
1239
+ for (const key in fields) {
1240
+ if (key in data) {
1241
+ parsedData[key] = data[key];
1242
+ continue;
1243
+ }
1244
+ if (fields[key].defaultValue) {
1245
+ parsedData[key] = fields[key].defaultValue;
1246
+ continue;
1247
+ }
1248
+ }
1249
+ return parsedData;
1250
+ }
1251
+ function parseUserInput(options, user) {
1252
+ const schema = getAllFields(options, "user");
1253
+ return parseInputData(user || {}, { fields: schema });
1254
+ }
1248
1255
 
1249
1256
  // src/crypto/random.ts
1250
1257
  function byteToBinary(byte) {
@@ -2177,7 +2184,7 @@ var ok = createAuthEndpoint(
2177
2184
  // src/api/routes/sign-up.ts
2178
2185
  import { z as z11 } from "zod";
2179
2186
  import { APIError as APIError9 } from "better-call";
2180
- var signUpEmail = createAuthEndpoint(
2187
+ var signUpEmail = () => createAuthEndpoint(
2181
2188
  "/sign-up/email",
2182
2189
  {
2183
2190
  method: "POST",
@@ -2189,7 +2196,8 @@ var signUpEmail = createAuthEndpoint(
2189
2196
  email: z11.string(),
2190
2197
  password: z11.string(),
2191
2198
  image: z11.string().optional(),
2192
- callbackURL: z11.string().optional()
2199
+ callbackURL: z11.string().optional(),
2200
+ additionalFields: z11.record(z11.string(), z11.any()).optional()
2193
2201
  })
2194
2202
  },
2195
2203
  async (ctx) => {
@@ -2199,6 +2207,7 @@ var signUpEmail = createAuthEndpoint(
2199
2207
  });
2200
2208
  }
2201
2209
  const { name, email, password, image } = ctx.body;
2210
+ const additionalFields = ctx.body.additionalFields;
2202
2211
  const isValidEmail = z11.string().email().safeParse(email);
2203
2212
  if (!isValidEmail.success) {
2204
2213
  throw new APIError9("BAD_REQUEST", {
@@ -2225,6 +2234,10 @@ var signUpEmail = createAuthEndpoint(
2225
2234
  message: "User already exists"
2226
2235
  });
2227
2236
  }
2237
+ const additionalData = parseUserInput(
2238
+ ctx.context.options,
2239
+ additionalFields
2240
+ );
2228
2241
  const createdUser = await ctx.context.internalAdapter.createUser({
2229
2242
  id: generateRandomString(32, alphabet("a-z", "0-9", "A-Z")),
2230
2243
  email: email.toLowerCase(),
@@ -2232,7 +2245,8 @@ var signUpEmail = createAuthEndpoint(
2232
2245
  image,
2233
2246
  emailVerified: false,
2234
2247
  createdAt: /* @__PURE__ */ new Date(),
2235
- updatedAt: /* @__PURE__ */ new Date()
2248
+ updatedAt: /* @__PURE__ */ new Date(),
2249
+ ...additionalData
2236
2250
  });
2237
2251
  if (!createdUser) {
2238
2252
  throw new APIError9("BAD_REQUEST", {
@@ -2291,6 +2305,33 @@ var signUpEmail = createAuthEndpoint(
2291
2305
  // src/api/index.ts
2292
2306
  import chalk from "chalk";
2293
2307
 
2308
+ // src/utils/get-request-ip.ts
2309
+ function getIp(req) {
2310
+ const testIP = "127.0.0.1";
2311
+ if (process.env.NODE_ENV === "test") {
2312
+ return testIP;
2313
+ }
2314
+ const headers = [
2315
+ "x-client-ip",
2316
+ "x-forwarded-for",
2317
+ "cf-connecting-ip",
2318
+ "fastly-client-ip",
2319
+ "x-real-ip",
2320
+ "x-cluster-client-ip",
2321
+ "x-forwarded",
2322
+ "forwarded-for",
2323
+ "forwarded"
2324
+ ];
2325
+ for (const header of headers) {
2326
+ const value = req.headers.get(header);
2327
+ if (typeof value === "string") {
2328
+ const ip = value.split(",")[0].trim();
2329
+ if (ip) return ip;
2330
+ }
2331
+ }
2332
+ return null;
2333
+ }
2334
+
2294
2335
  // src/api/rate-limiter.ts
2295
2336
  function shouldRateLimit(max, window2, rateLimitData) {
2296
2337
  const now = Date.now();
@@ -2487,7 +2528,7 @@ function getEndpoints(ctx, options) {
2487
2528
  getCSRFToken,
2488
2529
  getSession: getSession(),
2489
2530
  signOut,
2490
- signUpEmail,
2531
+ signUpEmail: signUpEmail(),
2491
2532
  signInEmail,
2492
2533
  forgetPassword,
2493
2534
  resetPassword,
package/dist/cli.js CHANGED
@@ -345,7 +345,8 @@ var getAuthTables = (options) => {
345
345
  defaultValue: () => /* @__PURE__ */ new Date(),
346
346
  required: true
347
347
  },
348
- ...user?.fields
348
+ ...user?.fields,
349
+ ...options.user?.additionalFields
349
350
  },
350
351
  order: 0
351
352
  },
@@ -373,7 +374,8 @@ var getAuthTables = (options) => {
373
374
  },
374
375
  required: true
375
376
  },
376
- ...session?.fields
377
+ ...session?.fields,
378
+ ...options.session?.additionalFields
377
379
  },
378
380
  order: 1
379
381
  },
@@ -2,18 +2,18 @@ import * as nanostores from 'nanostores';
2
2
  import { A as AccessControl, S as StatementsPrimitive, R as Role } from '../statement-CfnyN34h.js';
3
3
  import * as _better_fetch_fetch from '@better-fetch/fetch';
4
4
  import { BetterFetchOption } from '@better-fetch/fetch';
5
- import { o as organization, f as Organization, M as Member, I as Invitation, u as username, m as magicLink, d as phoneNumber, e as anonymous } from '../index-BToGSxVf.js';
6
- export { g as getPasskeyActions, c as passkeyClient, a as twoFactorClient } from '../index-BToGSxVf.js';
5
+ import { o as organization, f as Organization, M as Member, I as Invitation, u as username, m as magicLink, d as phoneNumber, e as anonymous } from '../index-CUhY6myj.js';
6
+ export { g as getPasskeyActions, c as passkeyClient, a as twoFactorClient } from '../index-CUhY6myj.js';
7
7
  import { P as Prettify } from '../helper-DPDj8Nix.js';
8
+ import { F as FieldAttribute, B as BetterAuthOptions, c as BetterAuthPlugin } from '../index-C9daNPfT.js';
8
9
  import '../index-CT4RHCuS.js';
9
10
  import 'arctic';
10
11
  import 'zod';
11
12
  import 'better-call';
12
- import '../index-DUb6fNdH.js';
13
+ import '@simplewebauthn/types';
13
14
  import 'kysely';
14
15
  import 'better-sqlite3';
15
16
  import 'mysql2';
16
- import '@simplewebauthn/types';
17
17
 
18
18
  interface OrganizationClientOptions {
19
19
  ac: AccessControl;
@@ -161,4 +161,94 @@ declare const anonymousClient: () => {
161
161
  };
162
162
  };
163
163
 
164
- export { anonymousClient, magicLinkClient, organizationClient, phoneNumberClient, usernameClient };
164
+ declare const inferAdditionalFields: <T, S extends {
165
+ user?: {
166
+ [key: string]: FieldAttribute;
167
+ };
168
+ session?: {
169
+ [key: string]: FieldAttribute;
170
+ };
171
+ } = {}>(schema?: S) => {
172
+ id: "additional-fields-client";
173
+ $InferServerPlugin: ((T extends BetterAuthOptions ? T : T extends {
174
+ options: BetterAuthOptions;
175
+ } ? T["options"] : never) extends never ? S extends {
176
+ user?: {
177
+ [key: string]: FieldAttribute;
178
+ };
179
+ session?: {
180
+ [key: string]: FieldAttribute;
181
+ };
182
+ } ? {
183
+ id: "additional-fields-client";
184
+ schema: {
185
+ user: {
186
+ fields: S["user"] extends object ? S["user"] : {};
187
+ };
188
+ session: {
189
+ fields: S["session"] extends object ? S["session"] : {};
190
+ };
191
+ };
192
+ } : never : (T extends BetterAuthOptions ? T : T extends {
193
+ options: BetterAuthOptions;
194
+ } ? T["options"] : never) extends BetterAuthOptions ? {
195
+ id: "additional-fields";
196
+ schema: {
197
+ user: {
198
+ fields: (T extends BetterAuthOptions ? T : T extends {
199
+ options: BetterAuthOptions;
200
+ } ? T["options"] : never)["user"] extends {
201
+ additionalFields: infer U;
202
+ } ? U : {};
203
+ };
204
+ session: {
205
+ fields: (T extends BetterAuthOptions ? T : T extends {
206
+ options: BetterAuthOptions;
207
+ } ? T["options"] : never)["session"] extends {
208
+ additionalFields: infer U;
209
+ } ? U : {};
210
+ };
211
+ };
212
+ } : never) extends BetterAuthPlugin ? (T extends BetterAuthOptions ? T : T extends {
213
+ options: BetterAuthOptions;
214
+ } ? T["options"] : never) extends never ? S extends {
215
+ user?: {
216
+ [key: string]: FieldAttribute;
217
+ };
218
+ session?: {
219
+ [key: string]: FieldAttribute;
220
+ };
221
+ } ? {
222
+ id: "additional-fields-client";
223
+ schema: {
224
+ user: {
225
+ fields: S["user"] extends object ? S["user"] : {};
226
+ };
227
+ session: {
228
+ fields: S["session"] extends object ? S["session"] : {};
229
+ };
230
+ };
231
+ } : never : (T extends BetterAuthOptions ? T : T extends {
232
+ options: BetterAuthOptions;
233
+ } ? T["options"] : never) extends BetterAuthOptions ? {
234
+ id: "additional-fields";
235
+ schema: {
236
+ user: {
237
+ fields: (T extends BetterAuthOptions ? T : T extends {
238
+ options: BetterAuthOptions;
239
+ } ? T["options"] : never)["user"] extends {
240
+ additionalFields: infer U;
241
+ } ? U : {};
242
+ };
243
+ session: {
244
+ fields: (T extends BetterAuthOptions ? T : T extends {
245
+ options: BetterAuthOptions;
246
+ } ? T["options"] : never)["session"] extends {
247
+ additionalFields: infer U;
248
+ } ? U : {};
249
+ };
250
+ };
251
+ } : never : undefined;
252
+ };
253
+
254
+ export { anonymousClient, inferAdditionalFields, magicLinkClient, organizationClient, phoneNumberClient, usernameClient };
@@ -504,9 +504,18 @@ var anonymousClient = () => {
504
504
  }
505
505
  };
506
506
  };
507
+
508
+ // src/plugins/additional-fields/client.ts
509
+ var inferAdditionalFields = (schema) => {
510
+ return {
511
+ id: "additional-fields-client",
512
+ $InferServerPlugin: {}
513
+ };
514
+ };
507
515
  export {
508
516
  anonymousClient,
509
517
  getPasskeyActions,
518
+ inferAdditionalFields,
510
519
  magicLinkClient,
511
520
  organizationClient,
512
521
  passkeyClient,
package/dist/client.d.ts CHANGED
@@ -6,7 +6,7 @@ import { BetterFetch, BetterFetchError, BetterFetchOption } from '@better-fetch/
6
6
  import { U as UnionToIntersection, P as Prettify, S as StripEmptyObjects } from './helper-DPDj8Nix.js';
7
7
  import { ClientOptions, InferClientAPI, InferActions, InferAdditionalFromClient, InferSessionFromClient, InferUserFromClient, BetterAuthClientPlugin, IsSignal } from './types.js';
8
8
  export { AtomListener, InferPluginsFromClient } from './types.js';
9
- import './index-DUb6fNdH.js';
9
+ import './index-C9daNPfT.js';
10
10
  import 'kysely';
11
11
  import './index-CT4RHCuS.js';
12
12
  import 'arctic';