Skip to content

Commit

Permalink
feat: enable cors, read ACCESS_CONTROL_ALLOW_ORIGIN from env (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaszkowic authored Oct 30, 2023
1 parent 7e69d84 commit 7034496
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0
SUPABASE_URL=http://localhost:54321
SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU
SUPABASE_MAX_ROWS=1000
SUPABASE_MAX_ROWS=10000
# Set to scheme, domain and possibly port (if something else than port 80) of the origin you are expecting
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
ACCESS_CONTROL_ALLOW_ORIGIN=https://giessdenkiez.de
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
SUPABASE_ANON_KEY: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0
SUPABASE_SERVICE_ROLE_KEY: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU
SUPABASE_MAX_ROWS: 10000
ACCESS_CONTROL_ALLOW_ORIGIN: *

runs-on: ubuntu-latest
environment: ci_test
Expand Down
6 changes: 6 additions & 0 deletions _utils/envs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface Envs {
SUPABASE_ANON_KEY: string;
SUPABASE_SERVICE_ROLE_KEY: string;
SUPABASE_MAX_ROWS: number;
ACCESS_CONTROL_ALLOW_ORIGIN: string;
}
export function getEnvs(): Envs {
const SUPABASE_URL = process.env.SUPABASE_URL;
Expand All @@ -22,10 +23,15 @@ export function getEnvs(): Envs {
throw new Error("SUPABASE_MAX_ROWS is not parseable to a number");
const SUPABASE_MAX_ROWS = parseInt(SUPABASE_MAX_ROWS_RAW);

const ACCESS_CONTROL_ALLOW_ORIGIN = process.env.ACCESS_CONTROL_ALLOW_ORIGIN;
if(ACCESS_CONTROL_ALLOW_ORIGIN === undefined)
throw new Error("ACCESS_CONTROL_ALLOW_ORIGIN is undefined")

return {
SUPABASE_URL,
SUPABASE_ANON_KEY,
SUPABASE_SERVICE_ROLE_KEY,
SUPABASE_MAX_ROWS,
ACCESS_CONTROL_ALLOW_ORIGIN
};
}
4 changes: 3 additions & 1 deletion _utils/set-headers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { VercelResponse } from "@vercel/node";
import { getEnvs } from "./envs";
const { ACCESS_CONTROL_ALLOW_ORIGIN } = getEnvs();

export default function setHeaders(
response: VercelResponse,
method: "GET" | "POST" | "DELETE"
): void {
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Origin", ACCESS_CONTROL_ALLOW_ORIGIN);
response.setHeader("Access-Control-Allow-Methods", `${method}, OPTIONS`);
response.setHeader(
"Access-Control-Allow-Headers",
Expand Down

0 comments on commit 7034496

Please sign in to comment.