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.

fix: repair auth token verification and add GitHub integration settings

valzorcaf9a2669964
diff --git a/backend/lib/supabase.js b/backend/lib/supabase.js
index dc0545e..0ccb253 100644
--- a/backend/lib/supabase.js
+++ b/backend/lib/supabase.js
@@ -1,11 +1,15 @@
import { createClient } from '@supabase/supabase-js';
+function clean(s) {
+ return s?.replace(/^/, '').trim();
+}
+
// Returns a service-role client that bypasses RLS - used for all DB operations.
export function getSupabase(env) {
- return createClient(env.SUPABASE_URL, env.SUPABASE_SERVICE_KEY, { auth: { persistSession: false } });
+ return createClient(clean(env.SUPABASE_URL), clean(env.SUPABASE_SERVICE_KEY), { auth: { persistSession: false } });
}
// Returns an anon client - used only to verify user JWTs in auth middleware.
export function getSupabaseAuth(env) {
- return createClient(env.SUPABASE_URL, env.SUPABASE_ANON_KEY, { auth: { persistSession: false } });
+ return createClient(clean(env.SUPABASE_URL), clean(env.SUPABASE_ANON_KEY), { auth: { persistSession: false } });
}
diff --git a/backend/routes/settings.js b/backend/routes/settings.js
new file mode 100644
index 0000000..c13a350
--- /dev/null
+++ b/backend/routes/settings.js
@@ -0,0 +1,31 @@
+import { Hono } from 'hono';
+import { getSettings, saveSettings } from '../lib/db.js';
+
+const app = new Hono();
+
+app.get('/settings/github', async (c) => {
+ const supabase = c.get('supabase');
+ const userId = c.get('userId');
+ const settings = await getSettings(supabase, userId);
+ const gh = settings.github || {};
+ return c.json({
+ configured: !!(gh.pat && gh.repoOwner && gh.repoName),
+ repoOwner: gh.repoOwner || '',
+ repoName: gh.repoName || '',
+ branch: gh.branch || 'main',
+ patSet: !!gh.pat,
+ });
+});
+
+app.put('/settings/github', async (c) => {
+ const supabase = c.get('supabase');
+ const userId = c.get('userId');
+ const { pat, repoOwner, repoName, branch } = await c.req.json();
+
+ const current = await getSettings(supabase, userId);
+ const update = { ...current, github: { pat, repoOwner, repoName, branch: branch || 'main' } };
+ await saveSettings(supabase, update, userId);
+ return c.json({ success: true });
+});
+
+export default app;
diff --git a/backend/server.js b/backend/server.js
index 8ac631e..061906f 100644
--- a/backend/server.js
+++ b/backend/server.js
@@ -8,6 +8,7 @@ import accountsRouter from './routes/accounts.js';
import analyticsRouter from './routes/analytics.js';
import publishingRouter from './routes/publishing.js';
import scheduledPostsRouter from './routes/scheduledPosts.js';
+import settingsRouter from './routes/settings.js';
import { urlHandler, callbackHandler } from './routes/instagramAuth.js';
import { processScheduledPosts } from './utils/scheduler.js';
import { autoRefreshInstagramTokens } from './services/instagram.js';
@@ -49,6 +50,7 @@ api.route('/', accountsRouter);
api.route('/', analyticsRouter);
api.route('/', publishingRouter);
api.route('/', scheduledPostsRouter);
+api.route('/', settingsRouter);
// Instagram OAuth URL generator - protected (needs userId to create state token)
api.get('/auth/instagram/url', urlHandler);
diff --git a/frontend/src/features/settings/Settings.jsx b/frontend/src/features/settings/Settings.jsx
index c6b4246..0d2368e 100644
--- a/frontend/src/features/settings/Settings.jsx
+++ b/frontend/src/features/settings/Settings.jsx
@@ -1,12 +1,14 @@
import React, { useState, useEffect } from 'react';
import { Button } from '@/components/ui/button';
+import { Input } from '@/components/ui/input';
+import { Label } from '@/components/ui/label';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { Separator } from '@/components/ui/separator';
import { Skeleton } from '@/components/ui/skeleton';
import { Badge } from '@/components/ui/badge';
import {
RefreshCw, CheckCircle2, Info, AlertTriangle, ShieldCheck, GitBranch,
- Key, ExternalLink, Copy, Check,
+ Key, ExternalLink, Copy, Check, Save,
} from 'lucide-react';
import MainCard from '../../components/MainCard';
import { useAppContext } from '../../context/AppContext';
@@ -46,20 +48,48 @@ export default function Settings() {
const [testResult, setTestResult] = useState(null);
const [loading, setLoading] = useState(true);
+ const [ghPat, setGhPat] = useState('');
+ const [ghRepoOwner, setGhRepoOwner] = useState('');
+ const [ghRepoName, setGhRepoName] = useState('');
+ const [ghBranch, setGhBranch] = useState('main');
+ const [ghPatSet, setGhPatSet] = useState(false);
+ const [ghSaving, setGhSaving] = useState(false);
+
useEffect(() => { loadStatus(); }, []);
const loadStatus = async () => {
setLoading(true);
try {
- const res = await api.getKeys();
- setServerStatus(res);
+ const [keys, gh] = await Promise.all([api.getKeys(), api.getGithubSettings()]);
+ setServerStatus(keys);
+ setGhRepoOwner(gh.repoOwner || '');
+ setGhRepoName(gh.repoName || '');
+ setGhBranch(gh.branch || 'main');
+ setGhPatSet(gh.patSet || false);
} catch {
- // non-critical
+ try {
+ const keys = await api.getKeys();
+ setServerStatus(keys);
+ } catch { /* non-critical */ }
} finally {
setLoading(false);
}
};
+ const handleSaveGithub = async () => {
+ setGhSaving(true);
+ try {
+ await api.saveGithubSettings({ pat: ghPat || undefined, repoOwner: ghRepoOwner, repoName: ghRepoName, branch: ghBranch });
+ showToast('GitHub settings saved');
+ if (ghPat) setGhPatSet(true);
+ setGhPat('');
+ } catch (err) {
+ showToast('Failed to save: ' + err.message, 'error');
+ } finally {
+ setGhSaving(false);
+ }
+ };
+
const handleTest = async () => {
setTesting(true); setTestResult(null);
try {
@@ -122,6 +152,81 @@ export default function Settings() {
</div>
</MainCard>
+ {/* GitHub credentials */}
+ <MainCard title="GitHub Integration">
+ <div className="space-y-4">
+ <div className="flex items-start gap-3 p-3 rounded-lg bg-muted/50 border border-border">
+ <GitBranch className="h-5 w-5 text-foreground mt-0.5 flex-shrink-0" />
+ <div className="space-y-1">
+ <p className="text-sm font-medium">Connect your GitHub repository</p>
+ <p className="text-sm text-muted-foreground">
+ Used for GitHub Actions-based scheduled publishing. The PAT lets the scheduler
+ trigger workflows in your repository.
+ </p>
+ </div>
+ </div>
+
+ <div className="grid grid-cols-2 gap-3">
+ <div className="space-y-1.5">
+ <Label htmlFor="gh-owner" className="text-xs">Repository owner</Label>
+ <Input
+ id="gh-owner"
+ placeholder="spacesdrive"
+ value={ghRepoOwner}
+ onChange={e => setGhRepoOwner(e.target.value)}
+ className="h-8 text-sm"
+ />
+ </div>
+ <div className="space-y-1.5">
+ <Label htmlFor="gh-repo" className="text-xs">Repository name</Label>
+ <Input
+ id="gh-repo"
+ placeholder="twiligent"
+ value={ghRepoName}
+ onChange={e => setGhRepoName(e.target.value)}
+ className="h-8 text-sm"
+ />
+ </div>
+ </div>
+
+ <div className="space-y-1.5">
+ <Label htmlFor="gh-branch" className="text-xs">Branch</Label>
+ <Input
+ id="gh-branch"
+ placeholder="main"
+ value={ghBranch}
+ onChange={e => setGhBranch(e.target.value)}
+ className="h-8 text-sm"
+ />
+ </div>
+
+ <div className="space-y-1.5">
+ <Label htmlFor="gh-pat" className="text-xs">
+ Personal Access Token
+ {ghPatSet && (
+ <Badge variant="secondary" className="ml-2 text-xs font-normal">
+ <CheckCircle2 className="h-3 w-3 mr-1 text-green-500" />saved
+ </Badge>
+ )}
+ </Label>
+ <Input
+ id="gh-pat"
+ type="password"
+ placeholder={ghPatSet ? 'Enter new token to replace saved one' : 'github_pat_...'}
+ value={ghPat}
+ onChange={e => setGhPat(e.target.value)}
+ className="h-8 text-sm font-mono"
+ />
+ <p className="text-xs text-muted-foreground">Leave blank to keep the existing token. See the guide below for how to create one.</p>
+ </div>
+
+ <Button size="sm" className="gap-2" onClick={handleSaveGithub} disabled={ghSaving}>
+ {ghSaving ? <RefreshCw className="h-3.5 w-3.5 animate-spin" /> : <Save className="h-3.5 w-3.5" />}
+ Save GitHub Settings
+ </Button>
+ </div>
+ </MainCard>
+
{/* GitHub guide */}
<MainCard title="GitHub Personal Access Token">
<div className="space-y-4">
diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js
index 93e738a..6745048 100644
--- a/frontend/src/services/api.js
+++ b/frontend/src/services/api.js
@@ -60,6 +60,10 @@ export const api = {
getIGContainerStatus: (containerId, accountId) => request(`/ig-container/${containerId}/status?accountId=${accountId}`),
publishIGContainer: (id, containerId) => request(`/accounts/${id}/ig-media-publish`, { method: 'POST', body: JSON.stringify({ containerId }) }),
+ // GitHub settings
+ getGithubSettings: () => request('/settings/github'),
+ saveGithubSettings: (data) => request('/settings/github', { method: 'PUT', body: JSON.stringify(data) }),
+
// Scheduled Posts
getScheduledPosts: () => request('/scheduled-posts'),
createScheduledPost: (data) => request('/scheduled-posts', { method: 'POST', body: JSON.stringify(data) }),