better-auth-studio 1.0.49-beta.7 → 1.0.49-beta.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.
@@ -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;AAS9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AA8D9C,wBAAsB,oBAAoB,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CA8I/E;AAwBD,wBAAgB,YAAY,CAC1B,UAAU,EAAE,UAAU,EACtB,UAAU,CAAC,EAAE,MAAM,EACnB,SAAS,CAAC,EAAE,MAAM,GACjB,MAAM,CA63JR"}
1
+ {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAOA,OAAO,EAA+B,MAAM,EAAE,MAAM,SAAS,CAAC;AAS9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AA8D9C,wBAAsB,oBAAoB,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CA8I/E;AAwBD,wBAAgB,YAAY,CAC1B,UAAU,EAAE,UAAU,EACtB,UAAU,CAAC,EAAE,MAAM,EACnB,SAAS,CAAC,EAAE,MAAM,GACjB,MAAM,CAgiKR"}
package/dist/routes.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createHmac, randomBytes } from 'node:crypto';
2
- import { existsSync, readFileSync } 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
@@ -4618,6 +4618,152 @@ export function createRoutes(authConfig, configPath, geoDbPath) {
4618
4618
  });
4619
4619
  }
4620
4620
  });
4621
+ router.post('/api/tools/check-env-credentials', async (req, res) => {
4622
+ try {
4623
+ const { provider } = req.body || {};
4624
+ if (!provider) {
4625
+ return res.status(400).json({
4626
+ success: false,
4627
+ message: 'Provider is required',
4628
+ });
4629
+ }
4630
+ const envPath = join(process.cwd(), '.env');
4631
+ const envLocalPath = join(process.cwd(), '.env.local');
4632
+ // Try .env.local first, then .env
4633
+ let targetPath = existsSync(envLocalPath) ? envLocalPath : envPath;
4634
+ let envContent = existsSync(targetPath) ? readFileSync(targetPath, 'utf-8') : '';
4635
+ // Generate environment variable names
4636
+ const providerUpper = provider.toUpperCase();
4637
+ const clientIdKey = `${providerUpper}_CLIENT_ID`;
4638
+ const clientSecretKey = `${providerUpper}_CLIENT_SECRET`;
4639
+ // Parse existing .env file
4640
+ const envLines = envContent.split('\n');
4641
+ const existingCredentials = {};
4642
+ envLines.forEach((line) => {
4643
+ const trimmed = line.trim();
4644
+ if (!trimmed || trimmed.startsWith('#'))
4645
+ return;
4646
+ const match = trimmed.match(/^([^=#]+)=(.*)$/);
4647
+ if (match) {
4648
+ const key = match[1].trim();
4649
+ const value = match[2].trim();
4650
+ if (key === clientIdKey || key === clientSecretKey) {
4651
+ existingCredentials[key] = value;
4652
+ }
4653
+ }
4654
+ });
4655
+ const hasExisting = existingCredentials[clientIdKey] || existingCredentials[clientSecretKey];
4656
+ res.json({
4657
+ success: true,
4658
+ hasExisting,
4659
+ existingCredentials: hasExisting ? existingCredentials : {},
4660
+ path: targetPath,
4661
+ });
4662
+ }
4663
+ catch (error) {
4664
+ res.status(500).json({
4665
+ success: false,
4666
+ message: error instanceof Error ? error.message : 'Failed to check credentials',
4667
+ });
4668
+ }
4669
+ });
4670
+ router.post('/api/tools/write-env-credentials', async (req, res) => {
4671
+ try {
4672
+ const { provider, clientId, clientSecret, action = 'override' } = req.body || {};
4673
+ if (!provider || !clientId || !clientSecret) {
4674
+ return res.status(400).json({
4675
+ success: false,
4676
+ message: 'Provider, Client ID, and Client Secret are required',
4677
+ });
4678
+ }
4679
+ const envPath = join(process.cwd(), '.env');
4680
+ const envLocalPath = join(process.cwd(), '.env.local');
4681
+ let targetPath = existsSync(envLocalPath) ? envLocalPath : envPath;
4682
+ let envContent = existsSync(targetPath) ? readFileSync(targetPath, 'utf-8') : '';
4683
+ const envLines = envContent.split('\n');
4684
+ const envMap = new Map();
4685
+ const newLines = [];
4686
+ envLines.forEach((line, index) => {
4687
+ const trimmed = line.trim();
4688
+ if (!trimmed || trimmed.startsWith('#')) {
4689
+ newLines.push(line);
4690
+ return;
4691
+ }
4692
+ const match = trimmed.match(/^([^=#]+)=(.*)$/);
4693
+ if (match) {
4694
+ const key = match[1].trim();
4695
+ const value = match[2].trim();
4696
+ envMap.set(key, { line, index });
4697
+ newLines.push(line);
4698
+ }
4699
+ else {
4700
+ newLines.push(line);
4701
+ }
4702
+ });
4703
+ const providerUpper = provider.toUpperCase();
4704
+ let clientIdKey = `${providerUpper}_CLIENT_ID`;
4705
+ let clientSecretKey = `${providerUpper}_CLIENT_SECRET`;
4706
+ if (action === 'append') {
4707
+ let suffix = 2;
4708
+ while (envMap.has(clientIdKey) || envMap.has(clientSecretKey)) {
4709
+ clientIdKey = `${providerUpper}_CLIENT_ID_${suffix}`;
4710
+ clientSecretKey = `${providerUpper}_CLIENT_SECRET_${suffix}`;
4711
+ suffix++;
4712
+ }
4713
+ }
4714
+ let updated = false;
4715
+ if (action === 'override' && envMap.has(clientIdKey)) {
4716
+ const existing = envMap.get(clientIdKey);
4717
+ newLines[existing.index] = `${clientIdKey}=${clientId}`;
4718
+ updated = true;
4719
+ }
4720
+ else if (!envMap.has(clientIdKey)) {
4721
+ // Remove trailing empty lines
4722
+ while (newLines.length > 0 && !newLines[newLines.length - 1].trim()) {
4723
+ newLines.pop();
4724
+ }
4725
+ if (newLines.length > 0 && newLines[newLines.length - 1] && !newLines[newLines.length - 1].endsWith('\n')) {
4726
+ if (!newLines[newLines.length - 1].endsWith('\r\n') && !newLines[newLines.length - 1].endsWith('\n')) {
4727
+ newLines.push('');
4728
+ }
4729
+ }
4730
+ newLines.push(`${clientIdKey}=${clientId}`);
4731
+ updated = true;
4732
+ }
4733
+ if (action === 'override' && envMap.has(clientSecretKey)) {
4734
+ const existing = envMap.get(clientSecretKey);
4735
+ newLines[existing.index] = `${clientSecretKey}=${clientSecret}`;
4736
+ updated = true;
4737
+ }
4738
+ else if (!envMap.has(clientSecretKey)) {
4739
+ const clientIdIndex = newLines.findIndex((line) => line.startsWith(`${clientIdKey}=`));
4740
+ if (clientIdIndex >= 0) {
4741
+ newLines.splice(clientIdIndex + 1, 0, `${clientSecretKey}=${clientSecret}`);
4742
+ }
4743
+ else {
4744
+ newLines.push(`${clientSecretKey}=${clientSecret}`);
4745
+ }
4746
+ updated = true;
4747
+ }
4748
+ const newContent = newLines.join('\n');
4749
+ writeFileSync(targetPath, newContent, 'utf-8');
4750
+ res.json({
4751
+ success: true,
4752
+ message: 'OAuth credentials written successfully',
4753
+ path: targetPath,
4754
+ variables: {
4755
+ [clientIdKey]: clientId,
4756
+ [clientSecretKey]: '***',
4757
+ },
4758
+ });
4759
+ }
4760
+ catch (error) {
4761
+ res.status(500).json({
4762
+ success: false,
4763
+ message: error instanceof Error ? error.message : 'Failed to write credentials to .env',
4764
+ });
4765
+ }
4766
+ });
4621
4767
  return router;
4622
4768
  }
4623
4769
  //# sourceMappingURL=routes.js.map