/**
* Example Supabase Edge Function
*
* This function demonstrates:
* - Authentication with JWT
* - Database queries
* - Error handling
* - CORS headers
* - Environment variables
*
* Deploy with the MCP server:
* deploy_edge_function({
* name: "get-user-profile",
* code: <file-contents>,
* verify_jwt: true
* })
*/
import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
// CORS headers for cross-origin requests
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}
serve(async (req) => {
// Handle CORS preflight requests
if (req.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders })
}
try {
// Initialize Supabase client
const supabaseClient = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_ANON_KEY') ?? '',
{
global: {
headers: { Authorization: req.headers.get('Authorization')! },
},
}
)
// Get authenticated user
const {
data: { user },
error: userError,
} = await supabaseClient.auth.getUser()
if (userError || !user) {
throw new Error('Unauthorized')
}
// Parse request body (if POST/PUT)
let body = {}
if (req.method === 'POST' || req.method === 'PUT') {
body = await req.json()
}
// Get user profile from database
const { data: profile, error: profileError } = await supabaseClient
.from('profiles')
.select('*')
.eq('id', user.id)
.single()
if (profileError) {
throw profileError
}
// Return success response
return new Response(
JSON.stringify({
success: true,
user: {
id: user.id,
email: user.email,
profile: profile,
},
}),
{
headers: {
...corsHeaders,
'Content-Type': 'application/json',
},
status: 200,
}
)
} catch (error) {
// Error handling
console.error('Edge function error:', error)
return new Response(
JSON.stringify({
success: false,
error: error.message,
}),
{
headers: {
...corsHeaders,
'Content-Type': 'application/json',
},
status: error.message === 'Unauthorized' ? 401 : 400,
}
)
}
})
/* Example usage from client:
// JavaScript/TypeScript
const response = await fetch('https://your-supabase.com/functions/v1/get-user-profile', {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
})
const data = await response.json()
console.log(data)
*/