Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

Auto-sync ACCOUNTS_JSON secret + script.bat launcher

valzora38abb869277
diff --git a/backend/package-lock.json b/backend/package-lock.json
index 0b0ac0c..53fb571 100644
--- a/backend/package-lock.json
+++ b/backend/package-lock.json
@@ -10,6 +10,7 @@
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
+ "libsodium-wrappers": "^0.8.2",
"node-fetch": "^2.7.0"
},
"devDependencies": {
@@ -655,6 +656,21 @@
"node": ">=0.12.0"
}
},
+ "node_modules/libsodium": {
+ "version": "0.8.2",
+ "resolved": "https://registry.npmjs.org/libsodium/-/libsodium-0.8.2.tgz",
+ "integrity": "sha512-TsnGYMoZtpweT+kR+lOv5TVsnJ/9U0FZOsLFzFOMWmxqOAYXjX3fsrPAW+i1LthgDKXJnI9A8dWEanT1tnJKIw==",
+ "license": "ISC"
+ },
+ "node_modules/libsodium-wrappers": {
+ "version": "0.8.2",
+ "resolved": "https://registry.npmjs.org/libsodium-wrappers/-/libsodium-wrappers-0.8.2.tgz",
+ "integrity": "sha512-VFLmfxkxo+U9q60tjcnSomQBRx2UzlRjKWJqvB4K1pUqsMQg4cu3QXA2nrcsj9A1qRsnJBbi2Ozx1hsiDoCkhw==",
+ "license": "ISC",
+ "dependencies": {
+ "libsodium": "^0.8.0"
+ }
+ },
"node_modules/math-intrinsics": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
diff --git a/backend/package.json b/backend/package.json
index 10b4e35..376d581 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -10,9 +10,10 @@
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
+ "libsodium-wrappers": "^0.8.2",
"node-fetch": "^2.7.0"
},
"devDependencies": {
"nodemon": "^3.0.2"
}
-}
\ No newline at end of file
+}
diff --git a/backend/server.js b/backend/server.js
index 72e77a8..2ce745d 100644
--- a/backend/server.js
+++ b/backend/server.js
@@ -3,6 +3,7 @@ const cors = require('cors');
const fs = require('fs').promises;
const path = require('path');
const fetch = require('node-fetch');
+const sodium = require('libsodium-wrappers');
const app = express();
const PORT = 3001;
@@ -158,6 +159,59 @@ async function pullScheduledPostsFromGitHub() {
}
}
+// ─── GitHub Accounts Secret Sync ─────────────────────────────────────────────
+// Automatically updates ACCOUNTS_JSON secret in GitHub Actions whenever accounts change.
+// This means you never have to manually regenerate the base64 secret again.
+
+async function syncAccountsSecretToGitHub() {
+ try {
+ const keys = await readJSON(API_KEYS_FILE) || {};
+ const gh = keys.github;
+ if (!gh?.token || !gh?.repo) return;
+
+ const repo = gh.repo.replace(/^https?:\/\/github\.com\//, '').replace(/\.git$/, '').replace(/\/$/, '');
+ const accounts = await readJSON(ACCOUNTS_FILE) || [];
+ const secretValue = Buffer.from(JSON.stringify(accounts, null, 2)).toString('base64');
+
+ // Step 1: Get the repo's public key for encrypting secrets
+ const pkRes = await fetch(`https://api.github.com/repos/${repo}/actions/secrets/public-key`, {
+ headers: { Authorization: `token ${gh.token}`, 'User-Agent': 'SocialMediaDashboard' }
+ });
+ if (!pkRes.ok) {
+ console.error(' ☁️ Failed to get GitHub public key:', (await pkRes.json()).message);
+ return;
+ }
+ const { key: publicKey, key_id } = await pkRes.json();
+
+ // Step 2: Encrypt the secret using libsodium sealed box
+ await sodium.ready;
+ const binKey = sodium.from_base64(publicKey, sodium.base64_variants.ORIGINAL);
+ const binSecret = sodium.from_string(secretValue);
+ const encrypted = sodium.crypto_box_seal(binSecret, binKey);
+ const encryptedB64 = sodium.to_base64(encrypted, sodium.base64_variants.ORIGINAL);
+
+ // Step 3: Update the secret
+ const res = await fetch(`https://api.github.com/repos/${repo}/actions/secrets/ACCOUNTS_JSON`, {
+ method: 'PUT',
+ headers: {
+ Authorization: `token ${gh.token}`,
+ 'Content-Type': 'application/json',
+ 'User-Agent': 'SocialMediaDashboard'
+ },
+ body: JSON.stringify({ encrypted_value: encryptedB64, key_id }),
+ });
+
+ if (res.status === 201 || res.status === 204) {
+ console.log(' ☁️ Synced ACCOUNTS_JSON secret to GitHub');
+ } else {
+ const err = await res.json();
+ console.error(' ☁️ GitHub secret sync error:', err.message);
+ }
+ } catch (err) {
+ console.error(' ☁️ GitHub secret sync error:', err.message);
+ }
+}
+
async function getAPIKey() {
const keys = await readJSON(API_KEYS_FILE);
return keys?.youtube || '';
@@ -1042,6 +1096,7 @@ app.post('/api/accounts', async (req, res) => {
accounts.push(newAccount);
await writeJSON(ACCOUNTS_FILE, accounts);
+ syncAccountsSecretToGitHub().catch(() => { });
res.json({ success: true, account: newAccount });
} catch (err) {
@@ -1113,6 +1168,7 @@ app.post('/api/accounts/instagram', async (req, res) => {
accounts.push(newAccount);
await writeJSON(ACCOUNTS_FILE, accounts);
+ syncAccountsSecretToGitHub().catch(() => { });
// Mask token in response
const safeAccount = { ...newAccount };
@@ -1138,6 +1194,7 @@ app.post('/api/accounts/:id/refresh-ig-token', async (req, res) => {
account.accessToken = refreshed.accessToken;
account.tokenExpiresAt = new Date(Date.now() + (refreshed.expiresIn || 5184000) * 1000).toISOString();
await writeJSON(ACCOUNTS_FILE, accounts);
+ syncAccountsSecretToGitHub().catch(() => { });
res.json({ success: true, message: 'Token refreshed for another 60 days', expiresAt: account.tokenExpiresAt });
} catch (err) {
res.status(400).json({ success: false, message: err.message });
@@ -1152,6 +1209,7 @@ app.delete('/api/accounts/:id', async (req, res) => {
if (!account) return res.status(404).json({ success: false, message: 'Not found' });
const filtered = accounts.filter(a => a.id !== req.params.id);
await writeJSON(ACCOUNTS_FILE, filtered);
+ syncAccountsSecretToGitHub().catch(() => { });
// Clean caches
const cache = await readJSON(VIDEOS_CACHE_FILE);
diff --git a/script.bat b/script.bat
new file mode 100644
index 0000000..4815980
--- /dev/null
+++ b/script.bat
@@ -0,0 +1,19 @@
+@echo off
+setlocal
+cd /d "%~dp0"
+
+if not exist "backend\package.json" (
+ echo [ERROR] backend\package.json not found.
+ exit /b 1
+)
+
+if not exist "frontend\package.json" (
+ echo [ERROR] frontend\package.json not found.
+ exit /b 1
+)
+
+start "Backend - API" cmd /k "cd /d ""%~dp0backend"" && npm run dev"
+start "Frontend - Vite" cmd /k "cd /d ""%~dp0frontend"" && npm run dev"
+
+echo Backend and frontend are launching in separate windows.
+echo Press Ctrl+C in each window to stop the servers.