mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 12:31:26 -05:00
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:
@@ -615,6 +615,3 @@ export {
|
|||||||
SidebarSeparator,
|
SidebarSeparator,
|
||||||
SidebarTrigger,
|
SidebarTrigger,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Re-export useSidebar from the hooks file for backwards compatibility
|
|
||||||
export { useSidebar } from "@/hooks/useSidebar";
|
|
||||||
|
|||||||
@@ -41,6 +41,20 @@ const getCorsHeaders = (requestOrigin: string | null) => ({
|
|||||||
'Access-Control-Allow-Credentials': 'true',
|
'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) => {
|
serve(async (req) => {
|
||||||
const requestOrigin = req.headers.get('origin');
|
const requestOrigin = req.headers.get('origin');
|
||||||
const corsHeaders = getCorsHeaders(requestOrigin);
|
const corsHeaders = getCorsHeaders(requestOrigin);
|
||||||
@@ -76,11 +90,7 @@ serve(async (req) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify JWT token
|
// Verify JWT token
|
||||||
const supabaseUrl = Deno.env.get('SUPABASE_URL')!
|
const supabase = createAuthenticatedSupabaseClient(authHeader)
|
||||||
const supabaseAnonKey = Deno.env.get('SUPABASE_ANON_KEY')!
|
|
||||||
const supabase = createClient(supabaseUrl, supabaseAnonKey, {
|
|
||||||
global: { headers: { Authorization: authHeader } }
|
|
||||||
})
|
|
||||||
|
|
||||||
const { data: { user }, error: authError } = await supabase.auth.getUser()
|
const { data: { user }, error: authError } = await supabase.auth.getUser()
|
||||||
if (authError || !user) {
|
if (authError || !user) {
|
||||||
@@ -262,11 +272,7 @@ serve(async (req) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify JWT token
|
// Verify JWT token
|
||||||
const supabaseUrl = Deno.env.get('SUPABASE_URL')!
|
const supabase = createAuthenticatedSupabaseClient(authHeader)
|
||||||
const supabaseAnonKey = Deno.env.get('SUPABASE_ANON_KEY')!
|
|
||||||
const supabase = createClient(supabaseUrl, supabaseAnonKey, {
|
|
||||||
global: { headers: { Authorization: authHeader } }
|
|
||||||
})
|
|
||||||
|
|
||||||
const { data: { user }, error: authError } = await supabase.auth.getUser()
|
const { data: { user }, error: authError } = await supabase.auth.getUser()
|
||||||
if (authError || !user) {
|
if (authError || !user) {
|
||||||
@@ -438,11 +444,7 @@ serve(async (req) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify JWT token
|
// Verify JWT token
|
||||||
const supabaseUrl = Deno.env.get('SUPABASE_URL')!
|
const supabase = createAuthenticatedSupabaseClient(authHeader)
|
||||||
const supabaseAnonKey = Deno.env.get('SUPABASE_ANON_KEY')!
|
|
||||||
const supabase = createClient(supabaseUrl, supabaseAnonKey, {
|
|
||||||
global: { headers: { Authorization: authHeader } }
|
|
||||||
})
|
|
||||||
|
|
||||||
const { data: { user }, error: authError } = await supabase.auth.getUser()
|
const { data: { user }, error: authError } = await supabase.auth.getUser()
|
||||||
if (authError || !user) {
|
if (authError || !user) {
|
||||||
|
|||||||
Reference in New Issue
Block a user