fix: allow service role auth on migrate-old-books endpoint

This commit is contained in:
Krao Hasanee
2026-05-31 11:19:43 -04:00
parent ab7a6b5a57
commit 35d92d2176
+13 -8
View File
@@ -88,16 +88,21 @@ export default async function handler(req, res) {
if (req.method !== 'POST') return res.status(405).json({ error: 'POST only' });
const authHeader = req.headers.authorization || '';
const serviceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || '';
const supabaseUrl = process.env.VITE_SUPABASE_URL || process.env.SUPABASE_URL;
const supabaseKey = process.env.VITE_SUPABASE_ANON_KEY || process.env.SUPABASE_ANON_KEY;
const sb = createClient(supabaseUrl, supabaseKey, {
auth: { persistSession: false },
global: { headers: { Authorization: authHeader } },
});
const { data: { user } } = await sb.auth.getUser();
if (!user) return res.status(401).json({ error: 'Unauthorized' });
const { data: profile } = await sb.from('profiles').select('role').eq('id', user.id).single();
if (profile?.role !== 'team') return res.status(403).json({ error: 'Team only' });
const isServiceRole = serviceKey && authHeader === `Bearer ${serviceKey}`;
if (!isServiceRole) {
const sb = createClient(supabaseUrl, supabaseKey, {
auth: { persistSession: false },
global: { headers: { Authorization: authHeader } },
});
const { data: { user } } = await sb.auth.getUser();
if (!user) return res.status(401).json({ error: 'Unauthorized' });
const { data: profile } = await sb.from('profiles').select('role').eq('id', user.id).single();
if (profile?.role !== 'team') return res.status(403).json({ error: 'Team only' });
}
const sb = createClient(supabaseUrl, serviceKey || supabaseKey, { auth: { persistSession: false } });
const config = getConfig();
if (!config.configured) return res.status(500).json({ error: 'FileBrowser not configured' });