Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,56 @@ const RESEND_FROM_EMAIL = process.env.RESEND_FROM_EMAIL;
const APP_ORIGIN = process.env.APP_ORIGIN;
const ALLOWED_ORIGINS = (process.env.CORS_ORIGIN ?? '').split(',').map((value) => value.trim()).filter(Boolean);

// DIAGNOSTIC: Key metadata (no secrets printed)
if (SUPABASE_SERVICE_ROLE_KEY) {
const trimmed = SUPABASE_SERVICE_ROLE_KEY.trim();
const hasLeadingSpace = SUPABASE_SERVICE_ROLE_KEY !== trimmed;
const hasQuotes = SUPABASE_SERVICE_ROLE_KEY.startsWith('"') || SUPABASE_SERVICE_ROLE_KEY.startsWith("'");
const dotCount = trimmed.split('.').length - 1;
let keyFamily = 'unknown';
let jwtPayload: any = null;

if (trimmed.startsWith('eyJ')) {
keyFamily = 'legacy_jwt';
try {
const parts = trimmed.split('.');
if (parts.length === 3) {
jwtPayload = JSON.parse(Buffer.from(parts[1], 'base64').toString('utf8'));
}
} catch {}
} else if (trimmed.startsWith('sb_secret_')) {
keyFamily = 'sb_secret';
} else if (trimmed.startsWith('sb_publishable_')) {
keyFamily = 'sb_publishable';
}

console.log(`[diagnostic] SUPABASE_SERVICE_ROLE_KEY metadata:`);
console.log(` - Length (trimmed): ${trimmed.length}`);
console.log(` - Family: ${keyFamily}`);
console.log(` - Dot count: ${dotCount}`);
console.log(` - Has leading/trailing space: ${hasLeadingSpace}`);
console.log(` - Wrapped in quotes: ${hasQuotes}`);
if (jwtPayload) {
console.log(` - JWT role: ${jwtPayload.role || 'N/A'}`);
console.log(` - JWT project_ref: ${jwtPayload.sub || 'N/A'}`);
}
console.log(` - Configured SUPABASE_URL: ${SUPABASE_URL}`);

// Test REST API access with the (possibly trimmed/unquoted) key
(async () => {
try {
const testKey = hasQuotes ? trimmed.slice(1, -1) : trimmed;
const res = await fetch(`${SUPABASE_URL}/rest/v1/`, {
method: 'GET',
headers: { apikey: testKey },
});
console.log(` - REST API health check status: ${res.status}`);
} catch (err) {
console.log(` - REST API health check failed: ${(err as Error).message}`);
}
})();
}

if (!GEMINI_API_KEY) console.warn('[server] GEMINI_API_KEY is not configured');
if (!SUPABASE_URL || !SUPABASE_ANON_KEY) console.warn('[server] Supabase public configuration is incomplete');
if (!SUPABASE_SERVICE_ROLE_KEY) console.warn('[server] SUPABASE_SERVICE_ROLE_KEY is not configured');
Expand Down Expand Up @@ -234,3 +284,4 @@ app.use((error: unknown, _req: express.Request, res: express.Response, _next: ex
app.listen(PORT, () => {
console.log(`[server] listening on http://localhost:${PORT}`);
});

Loading