bolt.diy/app/routes/api.supabase.ts
KevIsDev bc7e2c5821 feat(supabase): add credentials handling for Supabase API keys and URL
This commit introduces the ability to fetch and store Supabase API keys and URL credentials when a project is selected. This enables the application to dynamically configure the Supabase connection environment variables, improving the integration with Supabase services. The changes include updates to the Supabase connection logic, new API endpoints, and modifications to the chat and prompt components to utilize the new credentials.
2025-03-20 11:17:27 +00:00

63 lines
1.8 KiB
TypeScript

import { json } from '@remix-run/node';
import type { ActionFunction } from '@remix-run/node';
import type { SupabaseProject } from '~/types/supabase';
export const action: ActionFunction = async ({ request }) => {
if (request.method !== 'POST') {
return json({ error: 'Method not allowed' }, { status: 405 });
}
try {
const { token } = (await request.json()) as any;
const projectsResponse = await fetch('https://api.supabase.com/v1/projects', {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
});
if (!projectsResponse.ok) {
const errorText = await projectsResponse.text();
console.error('Projects fetch failed:', errorText);
return json({ error: 'Failed to fetch projects' }, { status: 401 });
}
const projects = (await projectsResponse.json()) as SupabaseProject[];
const uniqueProjectsMap = new Map<string, SupabaseProject>();
for (const project of projects) {
if (!uniqueProjectsMap.has(project.id)) {
uniqueProjectsMap.set(project.id, project);
}
}
console.log(
'Unique projects:',
Array.from(uniqueProjectsMap.values()).map((p) => ({ id: p.id, name: p.name })),
);
const uniqueProjects = Array.from(uniqueProjectsMap.values());
uniqueProjects.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
return json({
user: { email: 'Connected', role: 'Admin' },
stats: {
projects: uniqueProjects,
totalProjects: uniqueProjects.length,
},
});
} catch (error) {
console.error('Supabase API error:', error);
return json(
{
error: error instanceof Error ? error.message : 'Authentication failed',
},
{ status: 401 },
);
}
};