Improve image upload functionality for authenticated users

Refactor `supabase/functions/upload-image/index.ts` to extract Supabase client creation into a new helper function `createAuthenticatedSupabaseClient`. This improves code organization and reusability by centralizing the logic for initializing an authenticated Supabase client using JWT authentication.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0cbed76-4668-44f7-95b4-a456009b318d
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
This commit is contained in:
pac7
2025-10-08 19:40:07 +00:00
parent 7e771d1fc2
commit 023f56eed7
2 changed files with 17 additions and 18 deletions

View File

@@ -41,6 +41,20 @@ const getCorsHeaders = (requestOrigin: string | null) => ({
'Access-Control-Allow-Credentials': 'true',
});
// Helper to create authenticated Supabase client
const createAuthenticatedSupabaseClient = (authHeader: string) => {
const supabaseUrl = Deno.env.get('SUPABASE_URL')
const supabaseAnonKey = Deno.env.get('SUPABASE_ANON_KEY')
if (!supabaseUrl || !supabaseAnonKey) {
throw new Error('Missing Supabase environment variables')
}
return createClient(supabaseUrl, supabaseAnonKey, {
global: { headers: { Authorization: authHeader } }
})
}
serve(async (req) => {
const requestOrigin = req.headers.get('origin');
const corsHeaders = getCorsHeaders(requestOrigin);
@@ -76,11 +90,7 @@ serve(async (req) => {
}
// Verify JWT token
const supabaseUrl = Deno.env.get('SUPABASE_URL')!
const supabaseAnonKey = Deno.env.get('SUPABASE_ANON_KEY')!
const supabase = createClient(supabaseUrl, supabaseAnonKey, {
global: { headers: { Authorization: authHeader } }
})
const supabase = createAuthenticatedSupabaseClient(authHeader)
const { data: { user }, error: authError } = await supabase.auth.getUser()
if (authError || !user) {
@@ -262,11 +272,7 @@ serve(async (req) => {
}
// Verify JWT token
const supabaseUrl = Deno.env.get('SUPABASE_URL')!
const supabaseAnonKey = Deno.env.get('SUPABASE_ANON_KEY')!
const supabase = createClient(supabaseUrl, supabaseAnonKey, {
global: { headers: { Authorization: authHeader } }
})
const supabase = createAuthenticatedSupabaseClient(authHeader)
const { data: { user }, error: authError } = await supabase.auth.getUser()
if (authError || !user) {
@@ -438,11 +444,7 @@ serve(async (req) => {
}
// Verify JWT token
const supabaseUrl = Deno.env.get('SUPABASE_URL')!
const supabaseAnonKey = Deno.env.get('SUPABASE_ANON_KEY')!
const supabase = createClient(supabaseUrl, supabaseAnonKey, {
global: { headers: { Authorization: authHeader } }
})
const supabase = createAuthenticatedSupabaseClient(authHeader)
const { data: { user }, error: authError } = await supabase.auth.getUser()
if (authError || !user) {