better-auth 0.2.8-beta.8 → 0.2.8

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 (44) hide show
  1. package/dist/.DS_Store +0 -0
  2. package/dist/access.js +13 -2
  3. package/dist/adapters/drizzle.d.ts +1 -1
  4. package/dist/adapters/drizzle.js +13 -23
  5. package/dist/adapters/mongodb.d.ts +1 -1
  6. package/dist/adapters/mongodb.js +3 -2
  7. package/dist/adapters/prisma.d.ts +1 -1
  8. package/dist/adapters/prisma.js +3 -280
  9. package/dist/api.d.ts +1 -1
  10. package/dist/api.js +407 -269
  11. package/dist/cli.js +213 -55
  12. package/dist/client/plugins.d.ts +5 -3
  13. package/dist/client/plugins.js +49 -34
  14. package/dist/client.d.ts +3 -1
  15. package/dist/client.js +34 -32
  16. package/dist/{index-CKn-Zrry.d.ts → index-C9S3KShG.d.ts} +50 -63
  17. package/dist/{index-DtRHPoYF.d.ts → index-UOcOxfoL.d.ts} +6 -5
  18. package/dist/index.d.ts +1 -1
  19. package/dist/index.js +501 -372
  20. package/dist/next-js.d.ts +1 -1
  21. package/dist/next-js.js +6 -5
  22. package/dist/node.d.ts +1 -1
  23. package/dist/node.js +5 -5
  24. package/dist/plugins.d.ts +8 -5
  25. package/dist/plugins.js +716 -498
  26. package/dist/react.d.ts +4 -2
  27. package/dist/react.js +37 -33
  28. package/dist/social.js +116 -68
  29. package/dist/solid-start.d.ts +1 -1
  30. package/dist/solid-start.js +3 -2
  31. package/dist/solid.d.ts +2 -1
  32. package/dist/solid.js +35 -32
  33. package/dist/svelte-kit.d.ts +1 -1
  34. package/dist/svelte-kit.js +6 -4
  35. package/dist/svelte.d.ts +2 -1
  36. package/dist/svelte.js +33 -32
  37. package/dist/types.d.ts +2 -2
  38. package/dist/types.js +0 -1
  39. package/dist/vue.d.ts +3 -1
  40. package/dist/vue.js +35 -32
  41. package/package.json +2 -3
  42. package/dist/hide-metadata-DEHJp1rk.d.ts +0 -5
  43. package/dist/utils.d.ts +0 -51
  44. package/dist/utils.js +0 -426
package/dist/utils.js DELETED
@@ -1,426 +0,0 @@
1
- import { TimeSpan } from 'oslo';
2
- import { nanoid } from 'nanoid';
3
- import { createConsola } from 'consola';
4
- import { generateState as generateState$1 } from 'oslo/oauth2';
5
- import { z } from 'zod';
6
-
7
- // src/utils/shim.ts
8
- var shimContext = (originalObject, newContext) => {
9
- const shimmedObj = {};
10
- for (const [key, value] of Object.entries(originalObject)) {
11
- shimmedObj[key] = (ctx) => {
12
- return value({
13
- ...ctx,
14
- context: {
15
- ...newContext,
16
- ...ctx.context
17
- }
18
- });
19
- };
20
- shimmedObj[key].path = value.path;
21
- shimmedObj[key].method = value.method;
22
- shimmedObj[key].options = value.options;
23
- shimmedObj[key].headers = value.headers;
24
- }
25
- return shimmedObj;
26
- };
27
- var shimEndpoint = (ctx, value) => {
28
- return async (context) => {
29
- for (const plugin of ctx.options.plugins || []) {
30
- if (plugin.hooks?.before) {
31
- for (const hook of plugin.hooks.before) {
32
- const match = hook.matcher({
33
- ...context,
34
- ...value
35
- });
36
- if (match) {
37
- const hookRes = await hook.handler(context);
38
- if (hookRes && "context" in hookRes) {
39
- context = {
40
- ...context,
41
- ...hookRes.context,
42
- ...value
43
- };
44
- }
45
- }
46
- }
47
- }
48
- }
49
- const endpointRes = value({
50
- ...context,
51
- context: {
52
- ...ctx,
53
- ...context.context
54
- }
55
- });
56
- let response = endpointRes;
57
- for (const plugin of ctx.options.plugins || []) {
58
- if (plugin.hooks?.after) {
59
- for (const hook of plugin.hooks.after) {
60
- const match = hook.matcher(context);
61
- if (match) {
62
- const obj = Object.assign(context, {
63
- returned: endpointRes
64
- });
65
- const hookRes = await hook.handler(obj);
66
- if (hookRes && "response" in hookRes) {
67
- response = hookRes.response;
68
- }
69
- }
70
- }
71
- }
72
- }
73
- return response;
74
- };
75
- };
76
-
77
- // src/error/better-auth-error.ts
78
- var BetterAuthError = class extends Error {
79
- constructor(message, cause) {
80
- super(message);
81
- this.name = "BetterAuthError";
82
- this.message = message;
83
- this.cause = cause;
84
- this.stack = "";
85
- }
86
- };
87
-
88
- // src/utils/base-url.ts
89
- function checkHasPath(url) {
90
- try {
91
- const parsedUrl = new URL(url);
92
- return parsedUrl.pathname !== "/";
93
- } catch (error) {
94
- throw new BetterAuthError(
95
- `Invalid base URL: ${url}. Please provide a valid base URL.`
96
- );
97
- }
98
- }
99
- function withPath(url, path = "/api/auth") {
100
- const hasPath = checkHasPath(url);
101
- if (hasPath) {
102
- return url;
103
- }
104
- path = path.startsWith("/") ? path : `/${path}`;
105
- return `${url}${path}`;
106
- }
107
- function getBaseURL(url, path) {
108
- if (url) {
109
- return withPath(url, path);
110
- }
111
- const env = process?.env || {};
112
- const fromEnv = env.BETTER_AUTH_URL || env.NEXT_PUBLIC_BETTER_AUTH_URL || env.PUBLIC_BETTER_AUTH_URL || env.NUXT_PUBLIC_BETTER_AUTH_URL || env.NUXT_PUBLIC_AUTH_URL || (env.BASE_URL !== "/" ? env.BASE_URL : void 0);
113
- if (fromEnv) {
114
- return withPath(fromEnv, path);
115
- }
116
- if (typeof window !== "undefined") {
117
- return withPath(window.location.origin, path);
118
- }
119
- return void 0;
120
- }
121
-
122
- // src/utils/clone.ts
123
- var cloneBase = (object, base) => {
124
- for (const key in object) {
125
- if (!object.hasOwnProperty(key)) continue;
126
- const value = object[key];
127
- if (typeof value === "object" && value !== null) {
128
- base[key] = cloneBase(value, value.constructor());
129
- } else {
130
- base[key] = value;
131
- }
132
- }
133
- return base;
134
- };
135
- var clone = (object) => {
136
- return cloneBase(object, object.constructor());
137
- };
138
- function getCookies(options) {
139
- const secure = !!options.advanced?.useSecureCookies || process.env.NODE_ENV !== "development" && process.env.NODE_ENV !== "test";
140
- const secureCookiePrefix = secure ? "__Secure-" : "";
141
- const cookiePrefix = "better-auth";
142
- const sessionMaxAge = new TimeSpan(7, "d").seconds();
143
- return {
144
- sessionToken: {
145
- name: `${secureCookiePrefix}${cookiePrefix}.session_token`,
146
- options: {
147
- httpOnly: true,
148
- sameSite: "lax",
149
- path: "/",
150
- secure: !!secureCookiePrefix,
151
- maxAge: sessionMaxAge
152
- }
153
- },
154
- csrfToken: {
155
- name: `${secureCookiePrefix ? "__Host-" : ""}${cookiePrefix}.csrf_token`,
156
- options: {
157
- httpOnly: true,
158
- sameSite: "lax",
159
- path: "/",
160
- secure: !!secureCookiePrefix,
161
- maxAge: 60 * 60 * 24 * 7
162
- }
163
- },
164
- state: {
165
- name: `${secureCookiePrefix}${cookiePrefix}.state`,
166
- options: {
167
- httpOnly: true,
168
- sameSite: "lax",
169
- path: "/",
170
- secure: !!secureCookiePrefix,
171
- maxAge: 60 * 15
172
- // 15 minutes in seconds
173
- }
174
- },
175
- pkCodeVerifier: {
176
- name: `${secureCookiePrefix}${cookiePrefix}.pk_code_verifier`,
177
- options: {
178
- httpOnly: true,
179
- sameSite: "lax",
180
- path: "/",
181
- secure: !!secureCookiePrefix,
182
- maxAge: 60 * 15
183
- // 15 minutes in seconds
184
- }
185
- },
186
- dontRememberToken: {
187
- name: `${secureCookiePrefix}${cookiePrefix}.dont_remember`,
188
- options: {
189
- httpOnly: true,
190
- sameSite: "lax",
191
- path: "/",
192
- secure: !!secureCookiePrefix
193
- //no max age so it expires when the browser closes
194
- }
195
- },
196
- nonce: {
197
- name: `${secureCookiePrefix}${cookiePrefix}.nonce`,
198
- options: {
199
- httpOnly: true,
200
- sameSite: "lax",
201
- path: "/",
202
- secure: !!secureCookiePrefix,
203
- maxAge: 60 * 15
204
- // 15 minutes in seconds
205
- }
206
- }
207
- };
208
- }
209
- function createCookieGetter(options) {
210
- const secure = !!options.advanced?.useSecureCookies || process.env.NODE_ENV === "production";
211
- const secureCookiePrefix = secure ? "__Secure-" : "";
212
- const cookiePrefix = "better-auth";
213
- function getCookie(cookieName, options2) {
214
- return {
215
- name: process.env.NODE_ENV === "production" ? `${secureCookiePrefix}${cookiePrefix}.${cookieName}` : `${cookiePrefix}.${cookieName}`,
216
- options: {
217
- secure: !!secureCookiePrefix,
218
- sameSite: "lax",
219
- path: "/",
220
- maxAge: 60 * 15,
221
- // 15 minutes in seconds
222
- ...options2
223
- }
224
- };
225
- }
226
- return getCookie;
227
- }
228
- async function setSessionCookie(ctx, sessionToken, dontRememberMe, overrides) {
229
- const options = ctx.context.authCookies.sessionToken.options;
230
- options.maxAge = dontRememberMe ? void 0 : options.maxAge;
231
- await ctx.setSignedCookie(
232
- ctx.context.authCookies.sessionToken.name,
233
- sessionToken,
234
- ctx.context.secret,
235
- options
236
- );
237
- if (dontRememberMe) {
238
- await ctx.setSignedCookie(
239
- ctx.context.authCookies.dontRememberToken.name,
240
- "true",
241
- ctx.context.secret,
242
- ctx.context.authCookies.dontRememberToken.options
243
- );
244
- }
245
- }
246
- function deleteSessionCookie(ctx) {
247
- ctx.setCookie(ctx.context.authCookies.sessionToken.name, "", {
248
- maxAge: 0
249
- });
250
- ctx.setCookie(ctx.context.authCookies.dontRememberToken.name, "", {
251
- maxAge: 0
252
- });
253
- }
254
- function parseSetCookieHeader(header) {
255
- const cookieMap = /* @__PURE__ */ new Map();
256
- const cookies = header.split(", ");
257
- cookies.forEach((cookie) => {
258
- const [nameValue, ...attributes] = cookie.split("; ");
259
- const [name, value] = nameValue.split("=");
260
- const cookieObj = { value };
261
- attributes.forEach((attr) => {
262
- const [attrName, attrValue] = attr.split("=");
263
- cookieObj[attrName.toLowerCase()] = attrValue || true;
264
- });
265
- cookieMap.set(name, cookieObj);
266
- });
267
- return cookieMap;
268
- }
269
-
270
- // src/utils/date.ts
271
- var getDate = (span, unit = "ms") => {
272
- const date = /* @__PURE__ */ new Date();
273
- return new Date(date.getTime() + (unit === "sec" ? span * 1e3 : span));
274
- };
275
-
276
- // src/utils/get-request-ip.ts
277
- function getIp(req) {
278
- const testIP = "127.0.0.1";
279
- if (process.env.NODE_ENV === "test") {
280
- return testIP;
281
- }
282
- const headers = [
283
- "x-client-ip",
284
- "x-forwarded-for",
285
- "cf-connecting-ip",
286
- "fastly-client-ip",
287
- "x-real-ip",
288
- "x-cluster-client-ip",
289
- "x-forwarded",
290
- "forwarded-for",
291
- "forwarded"
292
- ];
293
- for (const header of headers) {
294
- const value = req.headers.get(header);
295
- if (typeof value === "string") {
296
- const ip = value.split(",")[0].trim();
297
- if (ip) return ip;
298
- }
299
- }
300
- return null;
301
- }
302
-
303
- // src/utils/hide-metadata.ts
304
- var HIDE_METADATA = {
305
- isAction: false
306
- };
307
- var generateId = (size) => {
308
- return nanoid(size);
309
- };
310
- var consola = createConsola({
311
- formatOptions: {
312
- date: false,
313
- colors: true,
314
- compact: true
315
- },
316
- defaults: {
317
- tag: "Better Auth"
318
- }
319
- });
320
- var createLogger = (options) => {
321
- return {
322
- log: (...args) => {
323
- !options?.disabled && consola.log("", ...args);
324
- },
325
- error: (...args) => {
326
- !options?.disabled && consola.error("", ...args);
327
- },
328
- warn: (...args) => {
329
- !options?.disabled && consola.warn("", ...args);
330
- },
331
- info: (...args) => {
332
- !options?.disabled && consola.info("", ...args);
333
- },
334
- debug: (...args) => {
335
- !options?.disabled && consola.debug("", ...args);
336
- },
337
- box: (...args) => {
338
- !options?.disabled && consola.box("", ...args);
339
- },
340
- success: (...args) => {
341
- !options?.disabled && consola.success("", ...args);
342
- },
343
- break: (...args) => {
344
- !options?.disabled && console.log("\n");
345
- }
346
- };
347
- };
348
- var logger2 = createLogger();
349
-
350
- // src/utils/merger.ts
351
- var mergeObjects = (target, source) => {
352
- for (const key in source) {
353
- if (!source.hasOwnProperty(key)) continue;
354
- if (key === "constructor" || key === "prototype" || key === "__proto__")
355
- continue;
356
- const value = source[key];
357
- if (isPrimitive(value)) {
358
- if (value !== void 0 || !(key in target)) {
359
- target[key] = value;
360
- }
361
- } else if (!target[key] || isArray(value)) {
362
- target[key] = clone(value);
363
- } else {
364
- target[key] = mergeObjects(target[key], value);
365
- }
366
- }
367
- return target;
368
- };
369
- var isArray = (value) => {
370
- return Array.isArray(value);
371
- };
372
- var isPrimitive = (value) => {
373
- if (value === null) return true;
374
- const type = typeof value;
375
- return type !== "object" && type !== "function";
376
- };
377
- var merge = (objects) => {
378
- const target = clone(objects[0]);
379
- for (let i = 1, l = objects.length; i < l; i++) {
380
- mergeObjects(target, objects[i]);
381
- }
382
- return target;
383
- };
384
-
385
- // src/utils/misc.ts
386
- function capitalizeFirstLetter(str) {
387
- return str.charAt(0).toUpperCase() + str.slice(1);
388
- }
389
-
390
- // src/utils/password.ts
391
- async function validatePassword(ctx, data) {
392
- const accounts = await ctx.context.internalAdapter.findAccounts(data.userId);
393
- const credentialAccount = accounts?.find(
394
- (account) => account.providerId === "credential"
395
- );
396
- const currentPassword = credentialAccount?.password;
397
- if (!credentialAccount || !currentPassword) {
398
- return false;
399
- }
400
- const compare = await ctx.context.password.verify(
401
- currentPassword,
402
- data.password
403
- );
404
- return compare;
405
- }
406
- function generateState(callbackURL, currentURL, dontRememberMe) {
407
- const code = generateState$1();
408
- const state = JSON.stringify({
409
- code,
410
- callbackURL,
411
- currentURL,
412
- dontRememberMe
413
- });
414
- return { state, code };
415
- }
416
- function parseState(state) {
417
- const data = z.object({
418
- code: z.string(),
419
- callbackURL: z.string().optional(),
420
- currentURL: z.string().optional(),
421
- dontRememberMe: z.boolean().optional()
422
- }).safeParse(JSON.parse(state));
423
- return data;
424
- }
425
-
426
- export { HIDE_METADATA, capitalizeFirstLetter, clone, createCookieGetter, createLogger, deleteSessionCookie, generateId, generateState, getBaseURL, getCookies, getDate, getIp, logger2 as logger, merge, parseSetCookieHeader, parseState, setSessionCookie, shimContext, shimEndpoint, validatePassword };