Skip to content

Commit a2bf5b1

Browse files
committed
make an automation to set client id for all examples
1 parent 887abf4 commit a2bf5b1

File tree

6 files changed

+46
-40
lines changed

6 files changed

+46
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,4 @@ yarn-error.log
151151
build/
152152
Pods/
153153
.vscode/
154+
.vercel

ENV_SETUP.md

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,5 @@ Custom authentication examples may require additional environment variables such
2626
- `VITE_AUTH0_CLIENT_ID` - Your Auth0 Client ID
2727
- `VITE_GOOGLE_CLIENT_ID` - Your Google OAuth Client ID
2828
- `VITE_FIREBASE_API_KEY` - Your Firebase API Key
29-
- `VITE_FIREBASE_AUTH_DOMAIN` - Your Firebase Auth Domain
30-
- `VITE_FIREBASE_PROJECT_ID` - Your Firebase Project ID
3129

3230
Check the individual '.env.example' files in each example directory for the specific environment variables needed.
33-
34-
## Updated Files
35-
36-
All examples have been updated to use environment variables instead of hardcoded client IDs. The key files that were modified include:
37-
38-
### Quick Starts
39-
- `quick-starts/react-quick-start/src/web3authContext.tsx`
40-
- `quick-starts/vue-quick-start/src/web3authContext.tsx`
41-
- `quick-starts/vue-solana-quick-start/src/web3authContext.tsx`
42-
- `quick-starts/react-solana-quick-start/src/web3authContext.tsx`
43-
- `quick-starts/nextjs-quick-start/components/provider.tsx`
44-
- `quick-starts/angular-quick-start/src/app/app.component.ts`
45-
- `quick-starts/vanillajs-quick-start/script.js`
46-
47-
### Other Examples
48-
- `other/bitcoin-example/src/web3authContext.tsx`
49-
- (All other examples have .env.example files)
50-
51-
### Custom Authentication
52-
- `custom-authentication/single-connection/auth0-implicit-example/src/web3authContext.tsx`
53-
- `custom-authentication/single-connection/auth0-jwt-example/src/index.tsx`
54-
- `custom-authentication/single-connection/google-one-tap-example/src/index.tsx`
55-
- (All other examples have .env.example files)

react-playground/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_WEB3AUTH_CLIENT_ID=YOUR_WEB3AUTH_CLIENT_ID

react-playground/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function App() {
2727

2828
{/* Content */}
2929
<div className="relative z-10 flex flex-col min-h-screen">
30-
<Web3AuthProvider config={{ web3AuthOptions: web3AuthContextConfig }}>
30+
<Web3AuthProvider config={ web3AuthContextConfig }>
3131
<Playground>
3232
<BrowserRouter>
3333
<Routes>
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { walletServicesPlugin, WEB3AUTH_NETWORK, Web3AuthOptions } from "@web3auth/modal";
1+
import { WEB3AUTH_NETWORK } from "@web3auth/modal";
2+
import { Web3AuthContextConfig } from "@web3auth/modal/react";
23

3-
const clientId = "BIpw3vwP0QqF_QecEtEFYxEac6pW7i478ouMUwg-qiWp8ipe-OkD6FUabv99lG0iVO02GWd591bJeiYiM1Sl_Nc";
4+
const clientId = import.meta.env.VITE_WEB3AUTH_CLIENT_ID;
45

5-
const web3AuthOptions: Web3AuthOptions = {
6-
clientId,
7-
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET,
8-
authBuildEnv: "testing",
9-
plugins: [walletServicesPlugin()],
10-
uiConfig: {
11-
mode: "dark",
6+
const web3AuthContextConfig: Web3AuthContextConfig = {
7+
web3AuthOptions: {
8+
clientId,
9+
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
10+
authBuildEnv: "testing",
1211
},
1312
};
1413

15-
export default web3AuthOptions;
14+
export default web3AuthContextConfig;

set-client-id.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ function findProjectDirs(dir, excludes = ['node_modules', '.git']) {
3636
return results;
3737
}
3838

39+
// Find client ID variable from .env.example file
40+
function findClientIdVariable(dir) {
41+
const envExamplePath = path.join(dir, '.env.example');
42+
43+
if (fs.existsSync(envExamplePath)) {
44+
const envExampleContent = fs.readFileSync(envExamplePath, 'utf8');
45+
// Look for lines containing "WEB3AUTH" and "CLIENT_ID"
46+
const lines = envExampleContent.split('\n');
47+
const clientIdLine = lines.find(line =>
48+
line.includes('WEB3AUTH') && line.includes('CLIENT_ID') && !line.trim().startsWith('#')
49+
);
50+
51+
if (clientIdLine) {
52+
const match = clientIdLine.match(/^([^=]+)=/);
53+
if (match && match[1]) {
54+
return match[1].trim();
55+
}
56+
}
57+
}
58+
59+
// Default fallback
60+
return 'VITE_WEB3AUTH_CLIENT_ID';
61+
}
62+
3963
// Update .env file in the given directory with the client ID
4064
function updateEnvFile(dir, clientId) {
4165
const envPath = path.join(dir, '.env');
@@ -46,14 +70,20 @@ function updateEnvFile(dir, clientId) {
4670
envContent = fs.readFileSync(envPath, 'utf8');
4771
}
4872

73+
// Get the appropriate variable name from .env.example
74+
const clientIdVariable = findClientIdVariable(dir);
75+
4976
// Update or add the client ID
50-
const updated = envContent.includes('VITE_WEB3AUTH_CLIENT_ID=')
51-
? envContent.replace(/VITE_WEB3AUTH_CLIENT_ID=.*/g, `VITE_WEB3AUTH_CLIENT_ID=${clientId}`)
52-
: envContent + `\nVITE_WEB3AUTH_CLIENT_ID=${clientId}\n`;
77+
const variablePattern = new RegExp(`${clientIdVariable}=.*`, 'g');
78+
const variableLine = `${clientIdVariable}=${clientId}`;
79+
80+
const updated = envContent.includes(`${clientIdVariable}=`)
81+
? envContent.replace(variablePattern, variableLine)
82+
: envContent + `\n${variableLine}\n`;
5383

5484
// Write back to the file
5585
fs.writeFileSync(envPath, updated);
56-
console.log(`Updated .env with Web3Auth client ID in ${dir}`);
86+
console.log(`Updated .env with Web3Auth client ID (${clientIdVariable}) in ${dir}`);
5787
}
5888

5989
// Main execution

0 commit comments

Comments
 (0)