better-auth-studio 1.0.20-beta.2 → 1.0.20-beta.5

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/routes.js CHANGED
@@ -1,10 +1,11 @@
1
1
  import { Router } from 'express';
2
- import { getAuthData } from './data.js';
3
- import { getAuthAdapter, createMockUser, createMockSession, createMockAccount, createMockVerification } from './auth-adapter.js';
2
+ import { existsSync, readFileSync } from 'fs';
4
3
  import { createJiti } from 'jiti';
5
- import { readFileSync, existsSync } from 'fs';
6
- import { join, dirname } from 'path';
4
+ import { dirname, join } from 'path';
7
5
  import { pathToFileURL } from 'url';
6
+ import { createMockAccount, createMockSession, createMockUser, createMockVerification, getAuthAdapter, } from './auth-adapter.js';
7
+ import { getAuthData } from './data.js';
8
+ import { initializeGeoService, resolveIPLocation, setGeoDbPath } from './geo-service.js';
8
9
  function resolveModuleWithExtensions(id, parent) {
9
10
  if (!id.startsWith('./') && !id.startsWith('../')) {
10
11
  return id;
@@ -54,7 +55,7 @@ export async function safeImportAuthConfig(authConfigPath) {
54
55
  join(authConfigDir, importName, 'index.ts'),
55
56
  join(authConfigDir, importName, 'index.js'),
56
57
  join(authConfigDir, importName, 'index.mjs'),
57
- join(authConfigDir, importName, 'index.cjs')
58
+ join(authConfigDir, importName, 'index.cjs'),
58
59
  ];
59
60
  for (const path of possiblePaths) {
60
61
  if (existsSync(path)) {
@@ -68,7 +69,7 @@ export async function safeImportAuthConfig(authConfigPath) {
68
69
  fsCache: true,
69
70
  moduleCache: true,
70
71
  interopDefault: true,
71
- alias: aliases
72
+ alias: aliases,
72
73
  });
73
74
  try {
74
75
  return await jiti.import(authConfigPath);
@@ -78,9 +79,9 @@ export async function safeImportAuthConfig(authConfigPath) {
78
79
  return {
79
80
  auth: {
80
81
  options: {
81
- _content: content
82
- }
83
- }
82
+ _content: content,
83
+ },
84
+ },
84
85
  };
85
86
  }
86
87
  }
@@ -168,7 +169,7 @@ async function findAuthConfigPath() {
168
169
  'src/auth.js',
169
170
  'src/auth.ts',
170
171
  'lib/auth.js',
171
- 'lib/auth.ts'
172
+ 'lib/auth.ts',
172
173
  ];
173
174
  for (const path of possiblePaths) {
174
175
  const fullPath = join(process.cwd(), path);
@@ -178,9 +179,13 @@ async function findAuthConfigPath() {
178
179
  }
179
180
  return null;
180
181
  }
181
- export function createRoutes(authConfig, configPath) {
182
+ // @ts-nocheck
183
+ export function createRoutes(authConfig, configPath, geoDbPath) {
182
184
  const router = Router();
183
- // Store the config path for use in adapter functions
185
+ if (geoDbPath) {
186
+ setGeoDbPath(geoDbPath);
187
+ }
188
+ initializeGeoService().catch(console.error);
184
189
  const getAuthAdapterWithConfig = () => getAuthAdapter(configPath);
185
190
  router.get('/api/health', (req, res) => {
186
191
  const uptime = process.uptime();
@@ -199,13 +204,43 @@ export function createRoutes(authConfig, configPath) {
199
204
  memory: {
200
205
  used: Math.round(process.memoryUsage().heapUsed / 1024 / 1024),
201
206
  total: Math.round(process.memoryUsage().heapTotal / 1024 / 1024),
202
- external: Math.round(process.memoryUsage().external / 1024 / 1024)
207
+ external: Math.round(process.memoryUsage().external / 1024 / 1024),
203
208
  },
204
209
  pid: process.pid,
205
- cwd: process.cwd()
206
- }
210
+ cwd: process.cwd(),
211
+ },
207
212
  });
208
213
  });
214
+ // IP Geolocation endpoint
215
+ router.post('/api/geo/resolve', (req, res) => {
216
+ try {
217
+ const { ipAddress } = req.body;
218
+ if (!ipAddress) {
219
+ return res.status(400).json({
220
+ success: false,
221
+ error: 'IP address is required',
222
+ });
223
+ }
224
+ const location = resolveIPLocation(ipAddress);
225
+ if (!location) {
226
+ return res.status(404).json({
227
+ success: false,
228
+ error: 'Location not found for IP address',
229
+ });
230
+ }
231
+ res.json({
232
+ success: true,
233
+ location,
234
+ });
235
+ }
236
+ catch (error) {
237
+ console.error('Error resolving IP location:', error);
238
+ res.status(500).json({
239
+ success: false,
240
+ error: 'Failed to resolve IP location',
241
+ });
242
+ }
243
+ });
209
244
  router.get('/api/config', async (req, res) => {
210
245
  let databaseType = 'unknown';
211
246
  const configPath = await findAuthConfigPath();
@@ -238,13 +273,13 @@ export function createRoutes(authConfig, configPath) {
238
273
  dialect: authConfig.database?.dialect || authConfig.database?.provider || 'unknown',
239
274
  casing: authConfig.database?.casing || 'camel',
240
275
  debugLogs: authConfig.database?.debugLogs || false,
241
- url: authConfig.database?.url
276
+ url: authConfig.database?.url,
242
277
  },
243
278
  emailVerification: {
244
279
  sendOnSignUp: authConfig.emailVerification?.sendOnSignUp || false,
245
280
  sendOnSignIn: authConfig.emailVerification?.sendOnSignIn || false,
246
281
  autoSignInAfterVerification: authConfig.emailVerification?.autoSignInAfterVerification || false,
247
- expiresIn: authConfig.emailVerification?.expiresIn || 3600
282
+ expiresIn: authConfig.emailVerification?.expiresIn || 3600,
248
283
  },
249
284
  emailAndPassword: {
250
285
  enabled: authConfig.emailAndPassword?.enabled ?? false,
@@ -254,26 +289,26 @@ export function createRoutes(authConfig, configPath) {
254
289
  minPasswordLength: authConfig.emailAndPassword?.minPasswordLength ?? 8,
255
290
  resetPasswordTokenExpiresIn: authConfig.emailAndPassword?.resetPasswordTokenExpiresIn ?? 3600,
256
291
  autoSignIn: authConfig.emailAndPassword?.autoSignIn ?? true, // defaults to true
257
- revokeSessionsOnPasswordReset: authConfig.emailAndPassword?.revokeSessionsOnPasswordReset ?? false
292
+ revokeSessionsOnPasswordReset: authConfig.emailAndPassword?.revokeSessionsOnPasswordReset ?? false,
258
293
  },
259
- socialProviders: authConfig.socialProviders ?
260
- Object.entries(authConfig.socialProviders).map(([provider, config]) => ({
294
+ socialProviders: authConfig.socialProviders
295
+ ? Object.entries(authConfig.socialProviders).map(([provider, config]) => ({
261
296
  type: provider,
262
297
  clientId: config.clientId,
263
298
  clientSecret: config.clientSecret,
264
299
  redirectUri: config.redirectUri,
265
- ...config
266
- })) :
267
- (authConfig.providers || []),
300
+ ...config,
301
+ }))
302
+ : authConfig.providers || [],
268
303
  user: {
269
304
  modelName: authConfig.user?.modelName || 'user',
270
305
  changeEmail: {
271
- enabled: authConfig.user?.changeEmail?.enabled || false
306
+ enabled: authConfig.user?.changeEmail?.enabled || false,
272
307
  },
273
308
  deleteUser: {
274
309
  enabled: authConfig.user?.deleteUser?.enabled || false,
275
- deleteTokenExpiresIn: authConfig.user?.deleteUser?.deleteTokenExpiresIn || 86400
276
- }
310
+ deleteTokenExpiresIn: authConfig.user?.deleteUser?.deleteTokenExpiresIn || 86400,
311
+ },
277
312
  },
278
313
  session: {
279
314
  modelName: authConfig.session?.modelName || 'session',
@@ -284,9 +319,9 @@ export function createRoutes(authConfig, configPath) {
284
319
  preserveSessionInDatabase: authConfig.session?.preserveSessionInDatabase || false,
285
320
  cookieCache: {
286
321
  enabled: authConfig.session?.cookieCache?.enabled || false,
287
- maxAge: authConfig.session?.cookieCache?.maxAge || 300
322
+ maxAge: authConfig.session?.cookieCache?.maxAge || 300,
288
323
  },
289
- freshAge: authConfig.session?.freshAge || 86400
324
+ freshAge: authConfig.session?.freshAge || 86400,
290
325
  },
291
326
  account: {
292
327
  modelName: authConfig.account?.modelName || 'account',
@@ -296,13 +331,13 @@ export function createRoutes(authConfig, configPath) {
296
331
  trustedProviders: authConfig.account?.accountLinking?.trustedProviders || [],
297
332
  allowDifferentEmails: authConfig.account?.accountLinking?.allowDifferentEmails || false,
298
333
  allowUnlinkingAll: authConfig.account?.accountLinking?.allowUnlinkingAll || false,
299
- updateUserInfoOnLink: authConfig.account?.accountLinking?.updateUserInfoOnLink || false
334
+ updateUserInfoOnLink: authConfig.account?.accountLinking?.updateUserInfoOnLink || false,
300
335
  },
301
- encryptOAuthTokens: authConfig.account?.encryptOAuthTokens || false
336
+ encryptOAuthTokens: authConfig.account?.encryptOAuthTokens || false,
302
337
  },
303
338
  verification: {
304
339
  modelName: authConfig.verification?.modelName || 'verification',
305
- disableCleanup: authConfig.verification?.disableCleanup || false
340
+ disableCleanup: authConfig.verification?.disableCleanup || false,
306
341
  },
307
342
  trustedOrigins: Array.isArray(authConfig.trustedOrigins) ? authConfig.trustedOrigins : [],
308
343
  rateLimit: {
@@ -310,39 +345,39 @@ export function createRoutes(authConfig, configPath) {
310
345
  window: authConfig.rateLimit?.window || 10,
311
346
  max: authConfig.rateLimit?.max || 100,
312
347
  storage: authConfig.rateLimit?.storage || 'memory',
313
- modelName: authConfig.rateLimit?.modelName || 'rateLimit'
348
+ modelName: authConfig.rateLimit?.modelName || 'rateLimit',
314
349
  },
315
350
  advanced: {
316
351
  ipAddress: {
317
352
  ipAddressHeaders: authConfig.advanced?.ipAddress?.ipAddressHeaders || [],
318
- disableIpTracking: authConfig.advanced?.ipAddress?.disableIpTracking || false
353
+ disableIpTracking: authConfig.advanced?.ipAddress?.disableIpTracking || false,
319
354
  },
320
355
  useSecureCookies: authConfig.advanced?.useSecureCookies || false,
321
356
  disableCSRFCheck: authConfig.advanced?.disableCSRFCheck || false,
322
357
  crossSubDomainCookies: {
323
358
  enabled: authConfig.advanced?.crossSubDomainCookies?.enabled || false,
324
359
  additionalCookies: authConfig.advanced?.crossSubDomainCookies?.additionalCookies || [],
325
- domain: authConfig.advanced?.crossSubDomainCookies?.domain
360
+ domain: authConfig.advanced?.crossSubDomainCookies?.domain,
326
361
  },
327
362
  cookies: authConfig.advanced?.cookies || {},
328
363
  defaultCookieAttributes: authConfig.advanced?.defaultCookieAttributes || {},
329
364
  cookiePrefix: authConfig.advanced?.cookiePrefix,
330
365
  database: {
331
366
  defaultFindManyLimit: authConfig.advanced?.database?.defaultFindManyLimit || 100,
332
- useNumberId: authConfig.advanced?.database?.useNumberId || false
333
- }
367
+ useNumberId: authConfig.advanced?.database?.useNumberId || false,
368
+ },
334
369
  },
335
370
  disabledPaths: authConfig.disabledPaths || [],
336
371
  telemetry: {
337
372
  enabled: authConfig.telemetry?.enabled ?? false,
338
- debug: authConfig.telemetry?.debug || false
373
+ debug: authConfig.telemetry?.debug || false,
339
374
  },
340
375
  studio: {
341
376
  version: '1.0.0',
342
377
  nodeVersion: process.version,
343
378
  platform: process.platform,
344
- uptime: process.uptime()
345
- }
379
+ uptime: process.uptime(),
380
+ },
346
381
  };
347
382
  res.json(config);
348
383
  });
@@ -395,7 +430,7 @@ export function createRoutes(authConfig, configPath) {
395
430
  res.json({
396
431
  users: userCount,
397
432
  sessions: sessionCount,
398
- organizations: organizationCount
433
+ organizations: organizationCount,
399
434
  });
400
435
  }
401
436
  catch (error) {
@@ -432,7 +467,7 @@ export function createRoutes(authConfig, configPath) {
432
467
  const users = await adapter.findMany({
433
468
  model: 'user',
434
469
  where: [{ field: 'id', value: userId }],
435
- limit: 1
470
+ limit: 1,
436
471
  });
437
472
  const user = users && users.length > 0 ? users[0] : null;
438
473
  if (!user) {
@@ -456,7 +491,7 @@ export function createRoutes(authConfig, configPath) {
456
491
  const user = await adapter.update({
457
492
  model: 'user',
458
493
  where: [{ field: 'id', value: userId }],
459
- update: { name, email }
494
+ update: { name, email },
460
495
  });
461
496
  res.json({ success: true, user });
462
497
  }
@@ -489,27 +524,29 @@ export function createRoutes(authConfig, configPath) {
489
524
  }
490
525
  const [memberships, organizations] = await Promise.all([
491
526
  adapter.findMany({ model: 'member', limit: 10000 }),
492
- adapter.findMany({ model: 'organization', limit: 10000 })
527
+ adapter.findMany({ model: 'organization', limit: 10000 }),
493
528
  ]);
494
529
  const userMemberships = memberships.filter((membership) => membership.userId === userId);
495
530
  const formattedMemberships = userMemberships.map((membership) => {
496
531
  const organization = organizations.find((org) => org.id === membership.organizationId);
497
532
  return {
498
533
  id: membership.id,
499
- organization: organization ? {
500
- id: organization.id,
501
- name: organization.name || 'Unknown Organization',
502
- slug: organization.slug || 'unknown',
503
- image: organization.image,
504
- createdAt: organization.createdAt
505
- } : {
506
- id: membership.organizationId,
507
- name: 'Unknown Organization',
508
- slug: 'unknown',
509
- createdAt: membership.createdAt
510
- },
534
+ organization: organization
535
+ ? {
536
+ id: organization.id,
537
+ name: organization.name || 'Unknown Organization',
538
+ slug: organization.slug || 'unknown',
539
+ image: organization.image,
540
+ createdAt: organization.createdAt,
541
+ }
542
+ : {
543
+ id: membership.organizationId,
544
+ name: 'Unknown Organization',
545
+ slug: 'unknown',
546
+ createdAt: membership.createdAt,
547
+ },
511
548
  role: membership.role || 'member',
512
- joinedAt: membership.createdAt
549
+ joinedAt: membership.createdAt,
513
550
  };
514
551
  });
515
552
  res.json({ memberships: formattedMemberships });
@@ -529,27 +566,33 @@ export function createRoutes(authConfig, configPath) {
529
566
  const [memberships, teams, organizations] = await Promise.all([
530
567
  adapter.findMany({ model: 'teamMember', limit: 10000 }),
531
568
  adapter.findMany({ model: 'team', limit: 10000 }),
532
- adapter.findMany({ model: 'organization', limit: 10000 })
569
+ adapter.findMany({ model: 'organization', limit: 10000 }),
533
570
  ]);
534
571
  const userMemberships = memberships.filter((membership) => membership.userId === userId);
535
572
  const formattedMemberships = userMemberships.map((membership) => {
536
573
  const team = teams.find((t) => t.id === membership.teamId);
537
- const organization = team ? organizations.find((org) => org.id === team.organizationId) : null;
574
+ const organization = team
575
+ ? organizations.find((org) => org.id === team.organizationId)
576
+ : null;
538
577
  return {
539
578
  id: membership.id,
540
- team: team ? {
541
- id: team.id,
542
- name: team.name || 'Unknown Team',
543
- organizationId: team.organizationId,
544
- organizationName: organization ? organization.name || 'Unknown Organization' : 'Unknown Organization'
545
- } : {
546
- id: membership.teamId,
547
- name: 'Unknown Team',
548
- organizationId: 'unknown',
549
- organizationName: 'Unknown Organization'
550
- },
579
+ team: team
580
+ ? {
581
+ id: team.id,
582
+ name: team.name || 'Unknown Team',
583
+ organizationId: team.organizationId,
584
+ organizationName: organization
585
+ ? organization.name || 'Unknown Organization'
586
+ : 'Unknown Organization',
587
+ }
588
+ : {
589
+ id: membership.teamId,
590
+ name: 'Unknown Team',
591
+ organizationId: 'unknown',
592
+ organizationName: 'Unknown Organization',
593
+ },
551
594
  role: membership.role || 'member',
552
- joinedAt: membership.createdAt
595
+ joinedAt: membership.createdAt,
553
596
  };
554
597
  });
555
598
  res.json({ memberships: formattedMemberships });
@@ -599,7 +642,7 @@ export function createRoutes(authConfig, configPath) {
599
642
  const user = await adapter.update({
600
643
  model: 'user',
601
644
  id: userId,
602
- data: { banned: true }
645
+ data: { banned: true },
603
646
  });
604
647
  res.json({ success: true, user });
605
648
  }
@@ -617,7 +660,7 @@ export function createRoutes(authConfig, configPath) {
617
660
  }
618
661
  const sessions = await adapter.findMany({
619
662
  model: 'session',
620
- limit: 10000
663
+ limit: 10000,
621
664
  });
622
665
  const userSessions = sessions.filter((session) => session.userId === userId);
623
666
  const formattedSessions = userSessions.map((session) => ({
@@ -629,7 +672,7 @@ export function createRoutes(authConfig, configPath) {
629
672
  activeOrganizationId: session.activeOrganizationId,
630
673
  activeTeamId: session.activeTeamId,
631
674
  createdAt: session.createdAt,
632
- updatedAt: session.updatedAt
675
+ updatedAt: session.updatedAt,
633
676
  }));
634
677
  res.json({ sessions: formattedSessions });
635
678
  }
@@ -663,7 +706,7 @@ export function createRoutes(authConfig, configPath) {
663
706
  const teams = await adapter.findMany({
664
707
  model: 'team',
665
708
  where: [{ field: 'id', value: teamId }],
666
- limit: 1
709
+ limit: 1,
667
710
  });
668
711
  const team = teams && teams.length > 0 ? teams[0] : null;
669
712
  if (!team) {
@@ -675,7 +718,7 @@ export function createRoutes(authConfig, configPath) {
675
718
  const orgs = await adapter.findMany({
676
719
  model: 'organization',
677
720
  where: [{ field: 'id', value: team.organizationId }],
678
- limit: 1
721
+ limit: 1,
679
722
  });
680
723
  organization = orgs && orgs.length > 0 ? orgs[0] : null;
681
724
  }
@@ -690,10 +733,12 @@ export function createRoutes(authConfig, configPath) {
690
733
  createdAt: team.createdAt,
691
734
  updatedAt: team.updatedAt,
692
735
  memberCount: team.memberCount || 0,
693
- organization: organization ? {
694
- id: organization.id,
695
- name: organization.name
696
- } : null
736
+ organization: organization
737
+ ? {
738
+ id: organization.id,
739
+ name: organization.name,
740
+ }
741
+ : null,
697
742
  };
698
743
  res.json({ success: true, team: transformedTeam });
699
744
  }
@@ -712,7 +757,7 @@ export function createRoutes(authConfig, configPath) {
712
757
  const organizations = await adapter.findMany({
713
758
  model: 'organization',
714
759
  where: [{ field: 'id', value: orgId }],
715
- limit: 1
760
+ limit: 1,
716
761
  });
717
762
  const organization = organizations && organizations.length > 0 ? organizations[0] : null;
718
763
  if (!organization) {
@@ -818,12 +863,14 @@ export function createRoutes(authConfig, configPath) {
818
863
  });
819
864
  router.get('/api/plugins', async (req, res) => {
820
865
  try {
821
- const authConfigPath = configPath ? join(process.cwd(), configPath) : await findAuthConfigPath();
866
+ const authConfigPath = configPath
867
+ ? join(process.cwd(), configPath)
868
+ : await findAuthConfigPath();
822
869
  if (!authConfigPath) {
823
870
  return res.json({
824
871
  plugins: [],
825
872
  error: 'No auth config found',
826
- configPath: null
873
+ configPath: null,
827
874
  });
828
875
  }
829
876
  try {
@@ -838,9 +885,9 @@ export function createRoutes(authConfig, configPath) {
838
885
  auth: {
839
886
  options: {
840
887
  _content: content,
841
- plugins: []
842
- }
843
- }
888
+ plugins: [],
889
+ },
890
+ },
844
891
  };
845
892
  }
846
893
  const auth = authModule.auth || authModule.default;
@@ -848,7 +895,7 @@ export function createRoutes(authConfig, configPath) {
848
895
  return res.json({
849
896
  plugins: [],
850
897
  error: 'No auth export found',
851
- configPath: authConfigPath
898
+ configPath: authConfigPath,
852
899
  });
853
900
  }
854
901
  const plugins = auth.options?.plugins || [];
@@ -856,12 +903,12 @@ export function createRoutes(authConfig, configPath) {
856
903
  id: plugin.id,
857
904
  name: plugin.name || plugin.id,
858
905
  description: plugin.description || `${plugin.id} plugin for Better Auth`,
859
- enabled: true
906
+ enabled: true,
860
907
  }));
861
908
  res.json({
862
909
  plugins: pluginInfo,
863
910
  configPath: authConfigPath,
864
- totalPlugins: pluginInfo.length
911
+ totalPlugins: pluginInfo.length,
865
912
  });
866
913
  }
867
914
  catch (error) {
@@ -877,13 +924,13 @@ export function createRoutes(authConfig, configPath) {
877
924
  name: plugin.name || plugin.id || 'unknown',
878
925
  version: plugin.version || 'unknown',
879
926
  description: plugin.description || `${plugin.id || 'unknown'} plugin for Better Auth`,
880
- enabled: true
927
+ enabled: true,
881
928
  }));
882
929
  return res.json({
883
930
  plugins: pluginInfo,
884
931
  configPath: authConfigPath,
885
932
  totalPlugins: pluginInfo.length,
886
- fallback: true
933
+ fallback: true,
887
934
  });
888
935
  }
889
936
  }
@@ -893,7 +940,7 @@ export function createRoutes(authConfig, configPath) {
893
940
  res.json({
894
941
  plugins: [],
895
942
  error: 'Failed to load auth config - import failed and regex extraction unavailable',
896
- configPath: authConfigPath
943
+ configPath: authConfigPath,
897
944
  });
898
945
  }
899
946
  }
@@ -904,12 +951,12 @@ export function createRoutes(authConfig, configPath) {
904
951
  });
905
952
  router.get('/api/database/info', async (req, res) => {
906
953
  try {
907
- const authConfigPath = configPath || await findAuthConfigPath();
954
+ const authConfigPath = configPath || (await findAuthConfigPath());
908
955
  if (!authConfigPath) {
909
956
  return res.json({
910
957
  database: null,
911
958
  error: 'No auth config found',
912
- configPath: null
959
+ configPath: null,
913
960
  });
914
961
  }
915
962
  try {
@@ -919,13 +966,13 @@ export function createRoutes(authConfig, configPath) {
919
966
  return res.json({
920
967
  database: null,
921
968
  error: 'No auth export found',
922
- configPath: authConfigPath
969
+ configPath: authConfigPath,
923
970
  });
924
971
  }
925
972
  const database = auth.options?.database;
926
973
  res.json({
927
974
  database: database,
928
- configPath: authConfigPath
975
+ configPath: authConfigPath,
929
976
  });
930
977
  }
931
978
  catch (error) {
@@ -939,7 +986,7 @@ export function createRoutes(authConfig, configPath) {
939
986
  return res.json({
940
987
  database: config.database,
941
988
  configPath: authConfigPath,
942
- fallback: true
989
+ fallback: true,
943
990
  });
944
991
  }
945
992
  }
@@ -949,7 +996,7 @@ export function createRoutes(authConfig, configPath) {
949
996
  res.json({
950
997
  database: null,
951
998
  error: 'Failed to load auth config - import failed and regex extraction unavailable',
952
- configPath: authConfigPath
999
+ configPath: authConfigPath,
953
1000
  });
954
1001
  }
955
1002
  }
@@ -960,12 +1007,12 @@ export function createRoutes(authConfig, configPath) {
960
1007
  });
961
1008
  router.get('/api/plugins/teams/status', async (req, res) => {
962
1009
  try {
963
- const authConfigPath = configPath || await findAuthConfigPath();
1010
+ const authConfigPath = configPath || (await findAuthConfigPath());
964
1011
  if (!authConfigPath) {
965
1012
  return res.json({
966
1013
  enabled: false,
967
1014
  error: 'No auth config found',
968
- configPath: null
1015
+ configPath: null,
969
1016
  });
970
1017
  }
971
1018
  try {
@@ -980,9 +1027,9 @@ export function createRoutes(authConfig, configPath) {
980
1027
  auth: {
981
1028
  options: {
982
1029
  _content: content,
983
- plugins: []
984
- }
985
- }
1030
+ plugins: [],
1031
+ },
1032
+ },
986
1033
  };
987
1034
  }
988
1035
  const auth = authModule.auth || authModule.default;
@@ -990,15 +1037,15 @@ export function createRoutes(authConfig, configPath) {
990
1037
  return res.json({
991
1038
  enabled: false,
992
1039
  error: 'No auth export found',
993
- configPath: authConfigPath
1040
+ configPath: authConfigPath,
994
1041
  });
995
1042
  }
996
- const organizationPlugin = auth.options?.plugins?.find((plugin) => plugin.id === "organization");
1043
+ const organizationPlugin = auth.options?.plugins?.find((plugin) => plugin.id === 'organization');
997
1044
  const teamsEnabled = organizationPlugin?.teams?.enabled === true;
998
1045
  res.json({
999
1046
  enabled: teamsEnabled,
1000
1047
  configPath: authConfigPath,
1001
- organizationPlugin: organizationPlugin || null
1048
+ organizationPlugin: organizationPlugin || null,
1002
1049
  });
1003
1050
  }
1004
1051
  catch (error) {
@@ -1009,13 +1056,13 @@ export function createRoutes(authConfig, configPath) {
1009
1056
  const { extractBetterAuthConfig } = await import('./config');
1010
1057
  const config = extractBetterAuthConfig(content);
1011
1058
  if (config && config.plugins) {
1012
- const organizationPlugin = config.plugins.find((plugin) => plugin.id === "organization");
1059
+ const organizationPlugin = config.plugins.find((plugin) => plugin.id === 'organization');
1013
1060
  const teamsEnabled = organizationPlugin?.teams?.enabled === true;
1014
1061
  return res.json({
1015
1062
  enabled: teamsEnabled,
1016
1063
  configPath: authConfigPath,
1017
1064
  organizationPlugin: organizationPlugin || null,
1018
- fallback: true
1065
+ fallback: true,
1019
1066
  });
1020
1067
  }
1021
1068
  }
@@ -1025,7 +1072,7 @@ export function createRoutes(authConfig, configPath) {
1025
1072
  res.json({
1026
1073
  enabled: false,
1027
1074
  error: 'Failed to load auth config - import failed and regex extraction unavailable',
1028
- configPath: authConfigPath
1075
+ configPath: authConfigPath,
1029
1076
  });
1030
1077
  }
1031
1078
  }
@@ -1044,7 +1091,7 @@ export function createRoutes(authConfig, configPath) {
1044
1091
  model: 'invitation',
1045
1092
  where: [
1046
1093
  { field: 'organizationId', value: orgId },
1047
- { field: 'status', value: 'pending' }
1094
+ { field: 'status', value: 'pending' },
1048
1095
  ],
1049
1096
  });
1050
1097
  const transformedInvitations = (invitations || []).map((invitation) => ({
@@ -1056,7 +1103,7 @@ export function createRoutes(authConfig, configPath) {
1056
1103
  teamId: invitation.teamId,
1057
1104
  inviterId: invitation.inviterId,
1058
1105
  expiresAt: invitation.expiresAt,
1059
- createdAt: invitation.createdAt
1106
+ createdAt: invitation.createdAt,
1060
1107
  }));
1061
1108
  res.json({ success: true, invitations: transformedInvitations });
1062
1109
  return;
@@ -1081,7 +1128,7 @@ export function createRoutes(authConfig, configPath) {
1081
1128
  const members = await adapter.findMany({
1082
1129
  model: 'member',
1083
1130
  where: [{ field: 'organizationId', value: orgId }],
1084
- limit: 10000
1131
+ limit: 10000,
1085
1132
  });
1086
1133
  const membersWithUsers = await Promise.all((members || []).map(async (member) => {
1087
1134
  try {
@@ -1089,7 +1136,7 @@ export function createRoutes(authConfig, configPath) {
1089
1136
  const users = await adapter.findMany({
1090
1137
  model: 'user',
1091
1138
  where: [{ field: 'id', value: member.userId }],
1092
- limit: 1
1139
+ limit: 1,
1093
1140
  });
1094
1141
  const user = users?.[0];
1095
1142
  return {
@@ -1098,13 +1145,15 @@ export function createRoutes(authConfig, configPath) {
1098
1145
  organizationId: member.organizationId,
1099
1146
  role: member.role || 'member',
1100
1147
  joinedAt: member.joinedAt || member.createdAt,
1101
- user: user ? {
1102
- id: user.id,
1103
- name: user.name,
1104
- email: user.email,
1105
- image: user.image,
1106
- emailVerified: user.emailVerified
1107
- } : null
1148
+ user: user
1149
+ ? {
1150
+ id: user.id,
1151
+ name: user.name,
1152
+ email: user.email,
1153
+ image: user.image,
1154
+ emailVerified: user.emailVerified,
1155
+ }
1156
+ : null,
1108
1157
  };
1109
1158
  }
1110
1159
  return null;
@@ -1114,7 +1163,7 @@ export function createRoutes(authConfig, configPath) {
1114
1163
  return null;
1115
1164
  }
1116
1165
  }));
1117
- const validMembers = membersWithUsers.filter(member => member && member.user);
1166
+ const validMembers = membersWithUsers.filter((member) => member && member.user);
1118
1167
  res.json({ success: true, members: validMembers });
1119
1168
  return;
1120
1169
  }
@@ -1159,21 +1208,21 @@ export function createRoutes(authConfig, configPath) {
1159
1208
  email,
1160
1209
  emailVerified: false,
1161
1210
  createdAt: new Date(),
1162
- updatedAt: new Date()
1211
+ updatedAt: new Date(),
1163
1212
  };
1164
1213
  const user = await adapter.create({
1165
1214
  model: 'user',
1166
- data: userData
1215
+ data: userData,
1167
1216
  });
1168
1217
  const memberData = {
1169
1218
  organizationId: orgId,
1170
1219
  userId: user.id,
1171
1220
  role: 'member',
1172
- createdAt: new Date()
1221
+ createdAt: new Date(),
1173
1222
  };
1174
1223
  await adapter.create({
1175
1224
  model: 'member',
1176
- data: memberData
1225
+ data: memberData,
1177
1226
  });
1178
1227
  results.push({
1179
1228
  success: true,
@@ -1181,22 +1230,22 @@ export function createRoutes(authConfig, configPath) {
1181
1230
  userId: user.id,
1182
1231
  user: {
1183
1232
  name,
1184
- email
1185
- }
1186
- }
1233
+ email,
1234
+ },
1235
+ },
1187
1236
  });
1188
1237
  }
1189
1238
  catch (error) {
1190
1239
  results.push({
1191
1240
  success: false,
1192
- error: error instanceof Error ? error.message : 'Unknown error'
1241
+ error: error instanceof Error ? error.message : 'Unknown error',
1193
1242
  });
1194
1243
  }
1195
1244
  }
1196
1245
  res.json({
1197
1246
  success: true,
1198
- message: `Added ${results.filter(r => r.success).length} members`,
1199
- results
1247
+ message: `Added ${results.filter((r) => r.success).length} members`,
1248
+ results,
1200
1249
  });
1201
1250
  }
1202
1251
  catch (error) {
@@ -1224,7 +1273,16 @@ export function createRoutes(authConfig, configPath) {
1224
1273
  return result;
1225
1274
  };
1226
1275
  const teamNames = [
1227
- 'Engineering', 'Design', 'Marketing', 'Sales', 'Support', 'Product', 'Operations', 'Finance', 'HR', 'Legal'
1276
+ 'Engineering',
1277
+ 'Design',
1278
+ 'Marketing',
1279
+ 'Sales',
1280
+ 'Support',
1281
+ 'Product',
1282
+ 'Operations',
1283
+ 'Finance',
1284
+ 'HR',
1285
+ 'Legal',
1228
1286
  ];
1229
1287
  const results = [];
1230
1288
  for (let i = 0; i < count; i++) {
@@ -1235,31 +1293,31 @@ export function createRoutes(authConfig, configPath) {
1235
1293
  name: teamName,
1236
1294
  organizationId: orgId,
1237
1295
  createdAt: new Date(),
1238
- updatedAt: new Date()
1296
+ updatedAt: new Date(),
1239
1297
  };
1240
1298
  const team = await adapter.create({
1241
1299
  model: 'team',
1242
- data: teamData
1300
+ data: teamData,
1243
1301
  });
1244
1302
  results.push({
1245
1303
  success: true,
1246
1304
  team: {
1247
1305
  id: team.id,
1248
- name: teamName
1249
- }
1306
+ name: teamName,
1307
+ },
1250
1308
  });
1251
1309
  }
1252
1310
  catch (error) {
1253
1311
  results.push({
1254
1312
  success: false,
1255
- error: error instanceof Error ? error.message : 'Unknown error'
1313
+ error: error instanceof Error ? error.message : 'Unknown error',
1256
1314
  });
1257
1315
  }
1258
1316
  }
1259
1317
  res.json({
1260
1318
  success: true,
1261
- message: `Created ${results.filter(r => r.success).length} teams`,
1262
- results
1319
+ message: `Created ${results.filter((r) => r.success).length} teams`,
1320
+ results,
1263
1321
  });
1264
1322
  }
1265
1323
  catch (error) {
@@ -1279,7 +1337,7 @@ export function createRoutes(authConfig, configPath) {
1279
1337
  }
1280
1338
  await adapter.delete({
1281
1339
  model: 'member',
1282
- where: [{ field: 'id', value: id }]
1340
+ where: [{ field: 'id', value: id }],
1283
1341
  });
1284
1342
  res.json({ success: true });
1285
1343
  }
@@ -1303,8 +1361,8 @@ export function createRoutes(authConfig, configPath) {
1303
1361
  where: [{ field: 'id', value: id }],
1304
1362
  update: {
1305
1363
  expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days from now
1306
- updatedAt: new Date().toISOString()
1307
- }
1364
+ updatedAt: new Date().toISOString(),
1365
+ },
1308
1366
  });
1309
1367
  res.json({ success: true });
1310
1368
  }
@@ -1328,8 +1386,8 @@ export function createRoutes(authConfig, configPath) {
1328
1386
  where: [{ field: 'id', value: id }],
1329
1387
  update: {
1330
1388
  status: 'cancelled',
1331
- updatedAt: new Date().toISOString()
1332
- }
1389
+ updatedAt: new Date().toISOString(),
1390
+ },
1333
1391
  });
1334
1392
  res.json({ success: true });
1335
1393
  }
@@ -1356,11 +1414,11 @@ export function createRoutes(authConfig, configPath) {
1356
1414
  status: 'pending',
1357
1415
  expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days
1358
1416
  createdAt: new Date(),
1359
- inviterId: inviterId
1417
+ inviterId: inviterId,
1360
1418
  };
1361
1419
  const invitation = {
1362
1420
  id: `inv_${Date.now()}`,
1363
- ...invitationData
1421
+ ...invitationData,
1364
1422
  };
1365
1423
  if (!adapter.create) {
1366
1424
  return res.status(500).json({ error: 'Adapter create method not available' });
@@ -1375,7 +1433,7 @@ export function createRoutes(authConfig, configPath) {
1375
1433
  inviterId: invitationData.inviterId,
1376
1434
  expiresAt: invitationData.expiresAt,
1377
1435
  createdAt: invitationData.createdAt,
1378
- }
1436
+ },
1379
1437
  });
1380
1438
  res.json({ success: true, invitation });
1381
1439
  }
@@ -1393,7 +1451,7 @@ export function createRoutes(authConfig, configPath) {
1393
1451
  const teams = await adapter.findMany({
1394
1452
  model: 'team',
1395
1453
  where: [{ field: 'organizationId', value: orgId }],
1396
- limit: 10000
1454
+ limit: 10000,
1397
1455
  });
1398
1456
  const transformedTeams = await Promise.all((teams || []).map(async (team) => {
1399
1457
  if (!adapter.findMany) {
@@ -1402,7 +1460,7 @@ export function createRoutes(authConfig, configPath) {
1402
1460
  const teamMembers = await adapter.findMany({
1403
1461
  model: 'teamMember',
1404
1462
  where: [{ field: 'teamId', value: team.id }],
1405
- limit: 10000
1463
+ limit: 10000,
1406
1464
  });
1407
1465
  return {
1408
1466
  id: team.id,
@@ -1411,7 +1469,7 @@ export function createRoutes(authConfig, configPath) {
1411
1469
  metadata: team.metadata,
1412
1470
  createdAt: team.createdAt,
1413
1471
  updatedAt: team.updatedAt,
1414
- memberCount: teamMembers ? teamMembers.length : 0
1472
+ memberCount: teamMembers ? teamMembers.length : 0,
1415
1473
  };
1416
1474
  }));
1417
1475
  res.json({ success: true, teams: transformedTeams });
@@ -1441,11 +1499,11 @@ export function createRoutes(authConfig, configPath) {
1441
1499
  organizationId: orgId,
1442
1500
  createdAt: new Date(),
1443
1501
  updatedAt: new Date(),
1444
- memberCount: 0
1502
+ memberCount: 0,
1445
1503
  };
1446
1504
  const team = {
1447
1505
  id: `team_${Date.now()}`,
1448
- ...teamData
1506
+ ...teamData,
1449
1507
  };
1450
1508
  if (!adapter.create) {
1451
1509
  return res.status(500).json({ error: 'Adapter create method not available' });
@@ -1457,7 +1515,7 @@ export function createRoutes(authConfig, configPath) {
1457
1515
  organizationId: teamData.organizationId,
1458
1516
  createdAt: teamData.createdAt,
1459
1517
  updatedAt: teamData.updatedAt,
1460
- }
1518
+ },
1461
1519
  });
1462
1520
  res.json({ success: true, team });
1463
1521
  }
@@ -1475,7 +1533,7 @@ export function createRoutes(authConfig, configPath) {
1475
1533
  const teamMembers = await adapter.findMany({
1476
1534
  model: 'teamMember',
1477
1535
  where: [{ field: 'teamId', value: teamId }],
1478
- limit: 10000
1536
+ limit: 10000,
1479
1537
  });
1480
1538
  const membersWithUsers = await Promise.all((teamMembers || []).map(async (member) => {
1481
1539
  try {
@@ -1483,7 +1541,7 @@ export function createRoutes(authConfig, configPath) {
1483
1541
  const users = await adapter.findMany({
1484
1542
  model: 'user',
1485
1543
  where: [{ field: 'id', value: member.userId }],
1486
- limit: 1
1544
+ limit: 1,
1487
1545
  });
1488
1546
  const user = users?.[0];
1489
1547
  return {
@@ -1492,13 +1550,15 @@ export function createRoutes(authConfig, configPath) {
1492
1550
  teamId: member.teamId,
1493
1551
  role: member.role || 'member',
1494
1552
  joinedAt: member.joinedAt || member.createdAt,
1495
- user: user ? {
1496
- id: user.id,
1497
- name: user.name,
1498
- email: user.email,
1499
- image: user.image,
1500
- emailVerified: user.emailVerified
1501
- } : null
1553
+ user: user
1554
+ ? {
1555
+ id: user.id,
1556
+ name: user.name,
1557
+ email: user.email,
1558
+ image: user.image,
1559
+ emailVerified: user.emailVerified,
1560
+ }
1561
+ : null,
1502
1562
  };
1503
1563
  }
1504
1564
  return null;
@@ -1508,7 +1568,7 @@ export function createRoutes(authConfig, configPath) {
1508
1568
  return null;
1509
1569
  }
1510
1570
  }));
1511
- const validMembers = membersWithUsers.filter(member => member && member.user);
1571
+ const validMembers = membersWithUsers.filter((member) => member && member.user);
1512
1572
  res.json({ success: true, members: validMembers });
1513
1573
  return;
1514
1574
  }
@@ -1543,8 +1603,8 @@ export function createRoutes(authConfig, configPath) {
1543
1603
  teamId,
1544
1604
  userId,
1545
1605
  role: 'member',
1546
- createdAt: new Date()
1547
- }
1606
+ createdAt: new Date(),
1607
+ },
1548
1608
  });
1549
1609
  results.push({ success: true, userId });
1550
1610
  }
@@ -1552,14 +1612,14 @@ export function createRoutes(authConfig, configPath) {
1552
1612
  results.push({
1553
1613
  success: false,
1554
1614
  userId,
1555
- error: error instanceof Error ? error.message : 'Unknown error'
1615
+ error: error instanceof Error ? error.message : 'Unknown error',
1556
1616
  });
1557
1617
  }
1558
1618
  }
1559
1619
  res.json({
1560
1620
  success: true,
1561
- message: `Added ${results.filter(r => r.success).length} members`,
1562
- results
1621
+ message: `Added ${results.filter((r) => r.success).length} members`,
1622
+ results,
1563
1623
  });
1564
1624
  }
1565
1625
  catch (error) {
@@ -1576,7 +1636,7 @@ export function createRoutes(authConfig, configPath) {
1576
1636
  }
1577
1637
  await adapter.delete({
1578
1638
  model: 'teamMember',
1579
- where: [{ field: 'id', value: id }]
1639
+ where: [{ field: 'id', value: id }],
1580
1640
  });
1581
1641
  res.json({ success: true });
1582
1642
  }
@@ -1605,7 +1665,7 @@ export function createRoutes(authConfig, configPath) {
1605
1665
  where: [{ field: 'id', value: id }],
1606
1666
  update: {
1607
1667
  name: updatedTeam.name,
1608
- }
1668
+ },
1609
1669
  });
1610
1670
  res.json({ success: true, team: updatedTeam });
1611
1671
  }
@@ -1637,30 +1697,28 @@ export function createRoutes(authConfig, configPath) {
1637
1697
  });
1638
1698
  router.get('/api/plugins/organization/status', async (req, res) => {
1639
1699
  try {
1640
- const authConfigPath = configPath || await findAuthConfigPath();
1700
+ const authConfigPath = configPath || (await findAuthConfigPath());
1641
1701
  if (!authConfigPath) {
1642
1702
  return res.json({
1643
1703
  enabled: false,
1644
1704
  error: 'No auth config found',
1645
- configPath: null
1705
+ configPath: null,
1646
1706
  });
1647
1707
  }
1648
1708
  try {
1649
- // Use the same logic as the /api/plugins endpoint
1650
1709
  let authModule;
1651
1710
  try {
1652
1711
  authModule = await safeImportAuthConfig(authConfigPath);
1653
1712
  }
1654
1713
  catch (importError) {
1655
- // Fallback: read file content directly
1656
1714
  const content = readFileSync(authConfigPath, 'utf-8');
1657
1715
  authModule = {
1658
1716
  auth: {
1659
1717
  options: {
1660
1718
  _content: content,
1661
- plugins: []
1662
- }
1663
- }
1719
+ plugins: [],
1720
+ },
1721
+ },
1664
1722
  };
1665
1723
  }
1666
1724
  const auth = authModule.auth || authModule.default;
@@ -1668,16 +1726,16 @@ export function createRoutes(authConfig, configPath) {
1668
1726
  return res.json({
1669
1727
  enabled: false,
1670
1728
  error: 'No auth export found',
1671
- configPath: authConfigPath
1729
+ configPath: authConfigPath,
1672
1730
  });
1673
1731
  }
1674
1732
  const plugins = auth.options?.plugins || [];
1675
- const hasOrganizationPlugin = plugins.find((plugin) => plugin.id === "organization");
1733
+ const hasOrganizationPlugin = plugins.find((plugin) => plugin.id === 'organization');
1676
1734
  res.json({
1677
1735
  enabled: !!hasOrganizationPlugin,
1678
1736
  configPath: authConfigPath,
1679
1737
  availablePlugins: plugins.map((p) => p.id) || [],
1680
- organizationPlugin: hasOrganizationPlugin || null
1738
+ organizationPlugin: hasOrganizationPlugin || null,
1681
1739
  });
1682
1740
  }
1683
1741
  catch (error) {
@@ -1688,13 +1746,13 @@ export function createRoutes(authConfig, configPath) {
1688
1746
  const { extractBetterAuthConfig } = await import('./config');
1689
1747
  const config = extractBetterAuthConfig(content);
1690
1748
  if (config && config.plugins) {
1691
- const hasOrganizationPlugin = config.plugins.find((plugin) => plugin.id === "organization");
1749
+ const hasOrganizationPlugin = config.plugins.find((plugin) => plugin.id === 'organization');
1692
1750
  return res.json({
1693
1751
  enabled: !!hasOrganizationPlugin,
1694
1752
  configPath: authConfigPath,
1695
1753
  availablePlugins: config.plugins.map((p) => p.id) || [],
1696
1754
  organizationPlugin: hasOrganizationPlugin || null,
1697
- fallback: true
1755
+ fallback: true,
1698
1756
  });
1699
1757
  }
1700
1758
  }
@@ -1704,7 +1762,7 @@ export function createRoutes(authConfig, configPath) {
1704
1762
  res.json({
1705
1763
  enabled: false,
1706
1764
  error: 'Failed to load auth config - import failed and regex extraction unavailable',
1707
- configPath: authConfigPath
1765
+ configPath: authConfigPath,
1708
1766
  });
1709
1767
  }
1710
1768
  }
@@ -1752,7 +1810,7 @@ export function createRoutes(authConfig, configPath) {
1752
1810
  slug: 'acme-corp',
1753
1811
  metadata: { status: 'active' },
1754
1812
  createdAt: new Date().toISOString(),
1755
- updatedAt: new Date().toISOString()
1813
+ updatedAt: new Date().toISOString(),
1756
1814
  },
1757
1815
  {
1758
1816
  id: 'org_2',
@@ -1760,8 +1818,8 @@ export function createRoutes(authConfig, configPath) {
1760
1818
  slug: 'tech-solutions',
1761
1819
  metadata: { status: 'active' },
1762
1820
  createdAt: new Date().toISOString(),
1763
- updatedAt: new Date().toISOString()
1764
- }
1821
+ updatedAt: new Date().toISOString(),
1822
+ },
1765
1823
  ];
1766
1824
  res.json({ organizations: mockOrganizations });
1767
1825
  }
@@ -1778,7 +1836,10 @@ export function createRoutes(authConfig, configPath) {
1778
1836
  }
1779
1837
  const orgData = req.body;
1780
1838
  if (!orgData.slug && orgData.name) {
1781
- orgData.slug = orgData.name.toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
1839
+ orgData.slug = orgData.name
1840
+ .toLowerCase()
1841
+ .replace(/\s+/g, '-')
1842
+ .replace(/[^a-z0-9-]/g, '');
1782
1843
  }
1783
1844
  const organization = await adapter.createOrganization(orgData);
1784
1845
  res.json({ success: true, organization });
@@ -1797,19 +1858,20 @@ export function createRoutes(authConfig, configPath) {
1797
1858
  return res.status(500).json({ error: 'Auth adapter not available' });
1798
1859
  }
1799
1860
  if (orgData.name && !orgData.slug) {
1800
- orgData.slug = orgData.name.toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
1861
+ orgData.slug = orgData.name
1862
+ .toLowerCase()
1863
+ .replace(/\s+/g, '-')
1864
+ .replace(/[^a-z0-9-]/g, '');
1801
1865
  }
1802
1866
  const updatedOrganization = {
1803
1867
  id,
1804
1868
  ...orgData,
1805
- updatedAt: new Date().toISOString()
1869
+ updatedAt: new Date().toISOString(),
1806
1870
  };
1807
1871
  const updatedOrg = await adapter.update({
1808
1872
  model: 'organization',
1809
- where: [
1810
- { field: 'id', value: id }
1811
- ],
1812
- update: updatedOrganization
1873
+ where: [{ field: 'id', value: id }],
1874
+ update: updatedOrganization,
1813
1875
  });
1814
1876
  res.json({ success: true, organization: updatedOrg });
1815
1877
  }
@@ -1827,9 +1889,7 @@ export function createRoutes(authConfig, configPath) {
1827
1889
  }
1828
1890
  const deletedOrg = await adapter.delete({
1829
1891
  model: 'organization',
1830
- where: [
1831
- { field: 'id', value: id }
1832
- ]
1892
+ where: [{ field: 'id', value: id }],
1833
1893
  });
1834
1894
  res.json({ success: true, organization: deletedOrg });
1835
1895
  }
@@ -1887,21 +1947,21 @@ export function createRoutes(authConfig, configPath) {
1887
1947
  name: user.name,
1888
1948
  emailVerified: user.emailVerified,
1889
1949
  image: user.image,
1890
- createdAt: user.createdAt
1891
- }
1950
+ createdAt: user.createdAt,
1951
+ },
1892
1952
  });
1893
1953
  }
1894
1954
  catch (error) {
1895
1955
  results.push({
1896
1956
  success: false,
1897
- error: error instanceof Error ? error.message : 'Unknown error'
1957
+ error: error instanceof Error ? error.message : 'Unknown error',
1898
1958
  });
1899
1959
  }
1900
1960
  }
1901
1961
  res.json({
1902
1962
  success: true,
1903
- message: `Seeded ${results.filter(r => r.success).length} users`,
1904
- results
1963
+ message: `Seeded ${results.filter((r) => r.success).length} users`,
1964
+ results,
1905
1965
  });
1906
1966
  }
1907
1967
  catch (error) {
@@ -1937,21 +1997,21 @@ export function createRoutes(authConfig, configPath) {
1937
1997
  userId: session.userId,
1938
1998
  expires: session.expires,
1939
1999
  sessionToken: session.sessionToken,
1940
- createdAt: session.createdAt
1941
- }
2000
+ createdAt: session.createdAt,
2001
+ },
1942
2002
  });
1943
2003
  }
1944
2004
  catch (error) {
1945
2005
  results.push({
1946
2006
  success: false,
1947
- error: error instanceof Error ? error.message : 'Unknown error'
2007
+ error: error instanceof Error ? error.message : 'Unknown error',
1948
2008
  });
1949
2009
  }
1950
2010
  }
1951
2011
  res.json({
1952
2012
  success: true,
1953
- message: `Seeded ${results.filter(r => r.success).length} sessions`,
1954
- results
2013
+ message: `Seeded ${results.filter((r) => r.success).length} sessions`,
2014
+ results,
1955
2015
  });
1956
2016
  }
1957
2017
  catch (error) {
@@ -1959,6 +2019,61 @@ export function createRoutes(authConfig, configPath) {
1959
2019
  res.status(500).json({ error: 'Failed to seed sessions' });
1960
2020
  }
1961
2021
  });
2022
+ router.post('/api/users/:userId/seed-sessions', async (req, res) => {
2023
+ try {
2024
+ const { userId } = req.params;
2025
+ const { count = 3 } = req.body;
2026
+ const adapter = await getAuthAdapterWithConfig();
2027
+ if (!adapter) {
2028
+ return res.status(500).json({ error: 'Auth adapter not available' });
2029
+ }
2030
+ // @ts-expect-error
2031
+ const user = await adapter.findOne({
2032
+ model: 'user',
2033
+ where: [{ field: 'id', value: userId }],
2034
+ });
2035
+ if (!user) {
2036
+ return res.status(404).json({ error: 'User not found' });
2037
+ }
2038
+ const results = [];
2039
+ for (let i = 0; i < count; i++) {
2040
+ try {
2041
+ if (typeof adapter.createSession !== 'function') {
2042
+ throw new Error('createSession method not available on adapter');
2043
+ }
2044
+ const session = await createMockSession(adapter, userId, i + 1);
2045
+ results.push({
2046
+ success: true,
2047
+ session: {
2048
+ id: session.id,
2049
+ userId: session.userId,
2050
+ expiresAt: session.expiresAt,
2051
+ token: session.token,
2052
+ ipAddress: session.ipAddress,
2053
+ userAgent: session.userAgent,
2054
+ createdAt: session.createdAt,
2055
+ updatedAt: session.updatedAt,
2056
+ },
2057
+ });
2058
+ }
2059
+ catch (error) {
2060
+ results.push({
2061
+ success: false,
2062
+ error: error instanceof Error ? error.message : 'Unknown error',
2063
+ });
2064
+ }
2065
+ }
2066
+ res.json({
2067
+ success: true,
2068
+ message: `Seeded ${results.filter((r) => r.success).length} sessions for user`,
2069
+ results,
2070
+ });
2071
+ }
2072
+ catch (error) {
2073
+ console.error('Error seeding sessions for user:', error);
2074
+ res.status(500).json({ error: 'Failed to seed sessions for user' });
2075
+ }
2076
+ });
1962
2077
  router.post('/api/seed/accounts', async (req, res) => {
1963
2078
  try {
1964
2079
  const { count = 1 } = req.body;
@@ -1988,21 +2103,21 @@ export function createRoutes(authConfig, configPath) {
1988
2103
  type: account.type,
1989
2104
  provider: account.provider,
1990
2105
  providerAccountId: account.providerAccountId,
1991
- createdAt: account.createdAt
1992
- }
2106
+ createdAt: account.createdAt,
2107
+ },
1993
2108
  });
1994
2109
  }
1995
2110
  catch (error) {
1996
2111
  results.push({
1997
2112
  success: false,
1998
- error: error instanceof Error ? error.message : 'Unknown error'
2113
+ error: error instanceof Error ? error.message : 'Unknown error',
1999
2114
  });
2000
2115
  }
2001
2116
  }
2002
2117
  res.json({
2003
2118
  success: true,
2004
- message: `Seeded ${results.filter(r => r.success).length} accounts`,
2005
- results
2119
+ message: `Seeded ${results.filter((r) => r.success).length} accounts`,
2120
+ results,
2006
2121
  });
2007
2122
  }
2008
2123
  catch (error) {
@@ -2031,21 +2146,21 @@ export function createRoutes(authConfig, configPath) {
2031
2146
  identifier: verification.identifier,
2032
2147
  token: verification.token,
2033
2148
  expires: verification.expires,
2034
- createdAt: verification.createdAt
2035
- }
2149
+ createdAt: verification.createdAt,
2150
+ },
2036
2151
  });
2037
2152
  }
2038
2153
  catch (error) {
2039
2154
  results.push({
2040
2155
  success: false,
2041
- error: error instanceof Error ? error.message : 'Unknown error'
2156
+ error: error instanceof Error ? error.message : 'Unknown error',
2042
2157
  });
2043
2158
  }
2044
2159
  }
2045
2160
  res.json({
2046
2161
  success: true,
2047
- message: `Seeded ${results.filter(r => r.success).length} verifications`,
2048
- results
2162
+ message: `Seeded ${results.filter((r) => r.success).length} verifications`,
2163
+ results,
2049
2164
  });
2050
2165
  }
2051
2166
  catch (error) {
@@ -2078,7 +2193,7 @@ export function createRoutes(authConfig, configPath) {
2078
2193
  slug: generateSlug(organizationName),
2079
2194
  image: `https://api.dicebear.com/7.x/identicon/svg?seed=${randomSuffix}`,
2080
2195
  createdAt: new Date(),
2081
- updatedAt: new Date()
2196
+ updatedAt: new Date(),
2082
2197
  };
2083
2198
  const organization = await adapter.createOrganization(organizationData);
2084
2199
  results.push({
@@ -2088,21 +2203,21 @@ export function createRoutes(authConfig, configPath) {
2088
2203
  name: organization.name,
2089
2204
  slug: organization.slug,
2090
2205
  image: organization.image,
2091
- createdAt: organization.createdAt
2092
- }
2206
+ createdAt: organization.createdAt,
2207
+ },
2093
2208
  });
2094
2209
  }
2095
2210
  catch (error) {
2096
2211
  results.push({
2097
2212
  success: false,
2098
- error: error instanceof Error ? error.message : 'Unknown error'
2213
+ error: error instanceof Error ? error.message : 'Unknown error',
2099
2214
  });
2100
2215
  }
2101
2216
  }
2102
2217
  res.json({
2103
2218
  success: true,
2104
- message: `Seeded ${results.filter(r => r.success).length} organizations`,
2105
- results
2219
+ message: `Seeded ${results.filter((r) => r.success).length} organizations`,
2220
+ results,
2106
2221
  });
2107
2222
  }
2108
2223
  catch (error) {