better-auth-studio 1.0.59-beta.5 → 1.0.59-beta.7

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 +1 @@
1
- {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAOA,OAAO,EAA+B,MAAM,EAAE,MAAM,SAAS,CAAC;AAa9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AA8D9C,wBAAsB,oBAAoB,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,UAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,CAyLhG;AAwBD,wBAAgB,YAAY,CAC1B,UAAU,EAAE,UAAU,EACtB,UAAU,CAAC,EAAE,MAAM,EACnB,SAAS,CAAC,EAAE,MAAM,GACjB,MAAM,CAilLR"}
1
+ {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAeA,OAAO,EAA+B,MAAM,EAAE,MAAM,SAAS,CAAC;AAS9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AA8D9C,wBAAsB,oBAAoB,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,UAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,CAqLhG;AAeD,wBAAgB,YAAY,CAC1B,UAAU,EAAE,UAAU,EACtB,UAAU,CAAC,EAAE,MAAM,EACnB,SAAS,CAAC,EAAE,MAAM,GACjB,MAAM,CAqlLR"}
package/dist/routes.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createHmac, randomBytes } from 'node:crypto';
2
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
2
+ import { existsSync, readFileSync, writeFileSync, } from 'node:fs';
3
3
  import { dirname, join } from 'node:path';
4
4
  import { fileURLToPath, pathToFileURL } from 'node:url';
5
5
  // @ts-expect-error
@@ -7,14 +7,11 @@ import { hex } from '@better-auth/utils/hex';
7
7
  import { scryptAsync } from '@noble/hashes/scrypt.js';
8
8
  import { Router } from 'express';
9
9
  import { createJiti } from 'jiti';
10
- import { execSync } from 'node:child_process';
11
- import { writeFileSync as writeFile, unlinkSync, readFileSync as readFile } from 'node:fs';
12
- import { tmpdir } from 'node:os';
13
- import { join as pathJoin } from 'node:path';
14
10
  import { createMockAccount, createMockSession, createMockUser, createMockVerification, getAuthAdapter, } from './auth-adapter.js';
15
11
  import { getAuthData } from './data.js';
16
12
  import { initializeGeoService, resolveIPLocation, setGeoDbPath } from './geo-service.js';
17
13
  import { detectDatabaseWithDialect } from './utils/database-detection.js';
14
+ import { possiblePaths } from './config.js';
18
15
  const config = {
19
16
  N: 16384,
20
17
  r: 16,
@@ -70,7 +67,6 @@ export async function safeImportAuthConfig(authConfigPath, noCache = false) {
70
67
  if (authConfigPath.endsWith('.ts')) {
71
68
  const aliases = {};
72
69
  const authConfigDir = dirname(authConfigPath);
73
- // Find project root by looking for tsconfig.json
74
70
  let projectDir = authConfigDir;
75
71
  let tsconfigPath = join(projectDir, 'tsconfig.json');
76
72
  while (!existsSync(tsconfigPath) && projectDir !== dirname(projectDir)) {
@@ -78,10 +74,8 @@ export async function safeImportAuthConfig(authConfigPath, noCache = false) {
78
74
  tsconfigPath = join(projectDir, 'tsconfig.json');
79
75
  }
80
76
  const content = readFileSync(authConfigPath, 'utf-8');
81
- // Get path aliases from tsconfig (including extends chain)
82
77
  const { getPathAliases } = await import('./config.js');
83
78
  const tsconfigAliases = getPathAliases(projectDir) || {};
84
- // Handle relative imports
85
79
  const relativeImportRegex = /import\s+.*?\s+from\s+['"](\.\/[^'"]+)['"]/g;
86
80
  const dynamicImportRegex = /import\s*\(\s*['"](\.\/[^'"]+)['"]\s*\)/g;
87
81
  const foundImports = new Set();
@@ -115,7 +109,6 @@ export async function safeImportAuthConfig(authConfigPath, noCache = false) {
115
109
  const pathAliasRegex = /import\s+.*?\s+from\s+['"](\$[^'"]+)['"]/g;
116
110
  while ((match = pathAliasRegex.exec(content)) !== null) {
117
111
  const aliasPath = match[1];
118
- // Check if we have a matching alias
119
112
  const aliasBase = aliasPath.split('/')[0];
120
113
  if (tsconfigAliases[aliasBase]) {
121
114
  const remainingPath = aliasPath.replace(aliasBase, '').replace(/^\//, '');
@@ -226,14 +219,6 @@ export async function safeImportAuthConfig(authConfigPath, noCache = false) {
226
219
  async function findAuthConfigPath() {
227
220
  const { join, dirname } = await import('node:path');
228
221
  const { existsSync } = await import('node:fs');
229
- const possiblePaths = [
230
- 'auth.js',
231
- 'auth.ts',
232
- 'src/auth.js',
233
- 'src/auth.ts',
234
- 'lib/auth.js',
235
- 'lib/auth.ts',
236
- ];
237
222
  for (const path of possiblePaths) {
238
223
  const fullPath = join(process.cwd(), path);
239
224
  if (existsSync(fullPath)) {
@@ -4380,6 +4365,29 @@ ${fields}
4380
4365
  })
4381
4366
  .join(',\n')
4382
4367
  : '';
4368
+ const preserveIndentation = (code, baseIndent) => {
4369
+ if (!code.trim())
4370
+ return '';
4371
+ const lines = code.split('\n');
4372
+ const nonEmptyLines = lines.filter(line => line.trim());
4373
+ if (nonEmptyLines.length === 0)
4374
+ return '';
4375
+ const minIndent = Math.min(...nonEmptyLines.map(line => {
4376
+ const match = line.match(/^(\s*)/);
4377
+ return match ? match[1].length : 0;
4378
+ }));
4379
+ return lines
4380
+ .map(line => {
4381
+ if (!line.trim())
4382
+ return '';
4383
+ const currentIndent = line.match(/^(\s*)/)?.[1] || '';
4384
+ const relativeIndent = Math.max(0, currentIndent.length - minIndent);
4385
+ const content = line.trim();
4386
+ return baseIndent + ' '.repeat(relativeIndent) + content;
4387
+ })
4388
+ .filter(Boolean)
4389
+ .join('\n');
4390
+ };
4383
4391
  const beforeHooks = hooks
4384
4392
  .filter((h) => h.timing === 'before')
4385
4393
  .map((hook) => {
@@ -4399,10 +4407,11 @@ ${fields}
4399
4407
  else {
4400
4408
  matcher = `(ctx) => true`;
4401
4409
  }
4410
+ const formattedHookLogic = preserveIndentation(hook.hookLogic || '// Hook logic here', ' ');
4402
4411
  return ` {
4403
4412
  matcher: ${matcher},
4404
4413
  handler: createAuthMiddleware(async (ctx) => {
4405
- ${hook.hookLogic || '// Hook logic here'}
4414
+ ${formattedHookLogic}
4406
4415
  }),
4407
4416
  }`;
4408
4417
  });
@@ -4425,19 +4434,21 @@ ${fields}
4425
4434
  else {
4426
4435
  matcher = `(ctx) => true`;
4427
4436
  }
4437
+ const formattedHookLogic = preserveIndentation(hook.hookLogic || '// Hook logic here', ' ');
4428
4438
  return ` {
4429
4439
  matcher: ${matcher},
4430
4440
  handler: createAuthMiddleware(async (ctx) => {
4431
- ${hook.hookLogic || '// Hook logic here'}
4441
+ ${formattedHookLogic}
4432
4442
  }),
4433
4443
  }`;
4434
4444
  });
4435
4445
  const middlewareCode = middleware
4436
4446
  .map((mw) => {
4447
+ const formattedMiddlewareLogic = preserveIndentation(mw.middlewareLogic || '// Middleware logic here', ' ');
4437
4448
  return ` {
4438
4449
  path: "${mw.path}",
4439
4450
  middleware: createAuthMiddleware(async (ctx) => {
4440
- ${mw.middlewareLogic || '// Middleware logic here'}
4451
+ ${formattedMiddlewareLogic}
4441
4452
  }),
4442
4453
  }`;
4443
4454
  })
@@ -4449,20 +4460,8 @@ ${fields}
4449
4460
  const sanitizedName = endpointName.replace(/[^a-zA-Z0-9]/g, '');
4450
4461
  const endpointPath = endpoint.path?.trim() || `/${camelCaseName}/${sanitizedName}`;
4451
4462
  const handlerLogic = endpoint.handlerLogic ||
4452
- '// Endpoint handler logic here\n return ctx.json({ success: true });';
4453
- const formattedHandlerLogic = handlerLogic
4454
- .split('\n')
4455
- .map((line) => {
4456
- const trimmed = line.trim();
4457
- if (!trimmed)
4458
- return '';
4459
- if (!line.startsWith(' ')) {
4460
- return ' ' + trimmed;
4461
- }
4462
- return line;
4463
- })
4464
- .filter(Boolean)
4465
- .join('\n');
4463
+ '// Endpoint handler logic here\nreturn ctx.json({ success: true });';
4464
+ const formattedHandlerLogic = preserveIndentation(handlerLogic, ' ');
4466
4465
  return ` ${sanitizedName}: createAuthEndpoint(
4467
4466
  "${endpointPath}",
4468
4467
  {
@@ -4511,33 +4510,6 @@ ${formattedHandlerLogic}
4511
4510
  .replace(/\n{3,}/g, '\n\n')
4512
4511
  .trim();
4513
4512
  };
4514
- const formatCode = (code) => {
4515
- try {
4516
- const tempFile = pathJoin(tmpdir(), `biome-format-${Date.now()}-${Math.random().toString(36).substring(7)}.ts`);
4517
- writeFile(tempFile, code, 'utf-8');
4518
- try {
4519
- execSync(`npx @biomejs/biome format --write ${tempFile}`, {
4520
- stdio: 'pipe',
4521
- cwd: process.cwd(),
4522
- });
4523
- const formatted = readFile(tempFile, 'utf-8');
4524
- unlinkSync(tempFile);
4525
- return formatted;
4526
- }
4527
- catch (formatError) {
4528
- try {
4529
- unlinkSync(tempFile);
4530
- }
4531
- catch {
4532
- // Ignore cleanup errors
4533
- }
4534
- return code;
4535
- }
4536
- }
4537
- catch (error) {
4538
- return code;
4539
- }
4540
- };
4541
4513
  const pluginParts = [];
4542
4514
  if (schemaCode) {
4543
4515
  pluginParts.push(` schema: {\n${schemaCode}\n }`);
@@ -4568,7 +4540,7 @@ ${formattedHandlerLogic}
4568
4540
  const serverPluginBody = pluginParts.length > 0
4569
4541
  ? ` id: "${camelCaseName}",\n${pluginParts.join(',\n')}`
4570
4542
  : ` id: "${camelCaseName}"`;
4571
- const serverPluginCode = formatCode(cleanCode(`import type { BetterAuthPlugin } from "@better-auth/core";
4543
+ const serverPluginCode = cleanCode(`import type { BetterAuthPlugin } from "@better-auth/core";
4572
4544
  ${imports.join('\n')}
4573
4545
 
4574
4546
  ${description ? `/**\n * ${description.replace(/\n/g, '\n * ')}\n */` : ''}
@@ -4577,7 +4549,7 @@ export const ${camelCaseName} = (options?: Record<string, any>) => {
4577
4549
  ${serverPluginBody}
4578
4550
  } satisfies BetterAuthPlugin;
4579
4551
  };
4580
- `));
4552
+ `);
4581
4553
  const pathMethods = endpoints.length > 0
4582
4554
  ? endpoints
4583
4555
  .map((endpoint) => {
@@ -4602,7 +4574,7 @@ ${serverPluginBody}
4602
4574
  const atomListenersCode = sessionAffectingPaths.length > 0
4603
4575
  ? `\n atomListeners: [\n${sessionAffectingPaths.join(',\n')}\n ],`
4604
4576
  : '';
4605
- const clientPluginCode = formatCode(cleanCode(`import type { BetterAuthClientPlugin } from "@better-auth/core";
4577
+ const clientPluginCode = cleanCode(`import type { BetterAuthClientPlugin } from "@better-auth/core";
4606
4578
  import type { ${camelCaseName} } from "..";
4607
4579
 
4608
4580
  export const ${camelCaseName}Client = () => {
@@ -4611,8 +4583,8 @@ export const ${camelCaseName}Client = () => {
4611
4583
  $InferServerPlugin: {} as ReturnType<typeof ${camelCaseName}>,${pathMethods ? `\n pathMethods: {\n${pathMethods}\n },` : ''}${atomListenersCode}
4612
4584
  } satisfies BetterAuthClientPlugin;
4613
4585
  };
4614
- `));
4615
- const serverSetupCode = formatCode(cleanCode(`import { betterAuth } from "@better-auth/core";
4586
+ `);
4587
+ const serverSetupCode = cleanCode(`import { betterAuth } from "@better-auth/core";
4616
4588
  import { ${camelCaseName} } from "./plugin/${camelCaseName}";
4617
4589
 
4618
4590
  export const auth = betterAuth({
@@ -4621,7 +4593,7 @@ export const auth = betterAuth({
4621
4593
  ${camelCaseName}(),
4622
4594
  ],
4623
4595
  });
4624
- `));
4596
+ `);
4625
4597
  const frameworkImportMap = {
4626
4598
  react: 'better-auth/react',
4627
4599
  svelte: 'better-auth/svelte',
@@ -4637,7 +4609,7 @@ export const auth = betterAuth({
4637
4609
  };
4638
4610
  const baseURL = baseURLMap[clientFramework] ||
4639
4611
  'process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "http://localhost:3000"';
4640
- const clientSetupCode = formatCode(cleanCode(`import { createAuthClient } from "${frameworkImport}";
4612
+ const clientSetupCode = cleanCode(`import { createAuthClient } from "${frameworkImport}";
4641
4613
  import { ${camelCaseName}Client } from "./plugin/${camelCaseName}/client";
4642
4614
 
4643
4615
  export const authClient = createAuthClient({
@@ -4646,7 +4618,7 @@ export const authClient = createAuthClient({
4646
4618
  ${camelCaseName}Client(),
4647
4619
  ],
4648
4620
  });
4649
- `));
4621
+ `);
4650
4622
  return res.json({
4651
4623
  success: true,
4652
4624
  plugin: {