Skip to content

Instantly share code, notes, and snippets.

@encima
Created June 13, 2025 07:12
Show Gist options
  • Select an option

  • Save encima/a9c2eefc04d41c04cd2f6e9b58746016 to your computer and use it in GitHub Desktop.

Select an option

Save encima/a9c2eefc04d41c04cd2f6e9b58746016 to your computer and use it in GitHub Desktop.
Supabase User Impersonation Edge Function
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
import { createClient } from "jsr:@supabase/supabase-js@2";
Deno.serve(async (req)=>{
if (req.method === 'OPTIONS') {
return new Response('ok', {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Authorization, Content-Type'
}
});
}
try {
const { email } = await req.json();
const serviceRoleKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY');
const supabaseAdminClient = createClient(Deno.env.get('SUPABASE_URL'), serviceRoleKey);
const { data, error } = await supabaseAdminClient.auth.admin.generateLink({
type: 'magiclink',
email: email
});
const { data: verify, error: verifyError } = await supabaseAdminClient.auth.verifyOtp({
token_hash: data['properties']['hashed_token'],
type: 'email'
});
// Optionally you can set the session here and call the endpoint you like from this function
// const { data, error } = await supabase.auth.setSession({
// access_token,
// refresh_token
// })
return new Response(JSON.stringify(verify), {
headers: {
'Content-Type': 'application/json',
'Connection': 'keep-alive'
}
});
} catch (error) {
return new Response(JSON.stringify({
error: error.message
}), {
headers: {
'Content-Type': 'application/json'
},
status: 400
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment