Skip to content

Commit

Permalink
fix: CORS fix for supabase (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
irensaltali authored Dec 23, 2024
1 parent a5a0b13 commit cf1f394
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
13 changes: 12 additions & 1 deletion src/cors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
function setCorsHeaders(request, response, corsConfig) {
const origin = request.headers.get('Origin');
const matchingOrigin = corsConfig.allow_origins.find((allowedOrigin) => allowedOrigin === origin);
console.log('Origin:', origin);
const matchingOrigin = corsConfig.allow_origins.find((allowedOrigin) => {
if (allowedOrigin === origin) {
return true;
}
if (allowedOrigin === '*') {
return true;
}
return false;
});

console.log('Matching Origin:', matchingOrigin);

const headers = new Headers(response.headers);
headers.set('Access-Control-Allow-Origin', matchingOrigin || corsConfig.allow_origins[0]);
Expand Down
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ export default {
}
}

console.log('Matched path:', matchedPath.config);

if (matchedPath.config.integration && matchedPath.config.integration.type == IntegrationTypeEnum.HTTP_PROXY) {
const server =
sagContext.apiConfig.servers &&
Expand Down Expand Up @@ -229,14 +227,16 @@ export default {
const phone = requestBody.phone;

if (email) {
return await supabaseEmailOTP(env, email);
const response = await supabaseEmailOTP(env, email)
return setPoweredByHeader(setCorsHeaders(request, response, sagContext.apiConfig.cors));
} else if (phone) {
return await supabasePhoneOTP(env, phone);
const response = await supabasePhoneOTP(env, phone)
return setPoweredByHeader(setCorsHeaders(request, response, sagContext.apiConfig.cors));
} else {
return new Response(safeStringify({ error: 'Missing email or phone', code: 'missing_email_or_phone' }), {
return setPoweredByHeader(setCorsHeaders(new Response(safeStringify({ error: 'Missing email or phone', code: 'missing_email_or_phone' }), {
status: 400,
headers: { 'Content-Type': 'application/json' },
});
})));
}
} else if (matchedPath.config.integration && matchedPath.config.integration.type == IntegrationTypeEnum.SUPABASEPASSWORDLESSVERIFY) {
const requestBody = await request.json();
Expand Down

0 comments on commit cf1f394

Please sign in to comment.