Skip to content

Commit 3a24c90

Browse files
authored
ENG-625 Construct environment files to access supabase. (#301)
* Construct environment files to access supabase. The base one is for running against the local supabase. It is possible to talk directly against either a supabase branch or the production database. Use cases for either should be extremely rare, and are documented separately. For this, set SUPABASE_USE_DB to 'branch' or 'production' respectively. The branch used will be the current branch, unless overridden by SUPABASE_GIT_BRANCH. * updated documentation
1 parent a401b85 commit 3a24c90

17 files changed

Lines changed: 3846 additions & 1577 deletions

File tree

apps/roam/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"license": "Apache-2.0",
1515
"devDependencies": {
16+
"@repo/database": "*",
1617
"@repo/tailwind-config": "*",
1718
"@types/contrast-color": "^1.0.0",
1819
"@types/react-vertical-timeline-component": "^3.3.3",

apps/roam/scripts/compile.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import esbuild from "esbuild";
22
import fs from "fs";
33
import path from "path";
44
import { z } from "zod";
5+
import { envContents } from "@repo/database/dbDotEnv";
56

67
// https://github.com/evanw/esbuild/issues/337#issuecomment-954633403
78
const importAsGlobals = (
@@ -122,6 +123,7 @@ export const compile = ({
122123
fs.mkdirSync(outdir, { recursive: true });
123124

124125
const buildPromises = [] as Promise<void>[];
126+
const dbEnv = envContents();
125127
buildPromises.push(
126128
builder({
127129
absWorkingDir: process.cwd(),
@@ -130,9 +132,9 @@ export const compile = ({
130132
bundle: true,
131133
format,
132134
define: {
133-
"process.env.SUPABASE_URL": `"${process.env.SUPABASE_URL}"`,
134-
"process.env.SUPABASE_ANON_KEY": `"${process.env.SUPABASE_ANON_KEY}"`,
135-
"process.env.NEXT_API_ROOT": `"${process.env.NEXT_API_ROOT || ""}"`,
135+
"process.env.SUPABASE_URL": `"${dbEnv.SUPABASE_URL}"`,
136+
"process.env.SUPABASE_ANON_KEY": `"${dbEnv.SUPABASE_ANON_KEY}"`,
137+
"process.env.NEXT_API_ROOT": `"${dbEnv.NEXT_API_ROOT || ""}"`,
136138
},
137139
sourcemap: process.env.NODE_ENV === "production" ? undefined : "inline",
138140
minify: process.env.NODE_ENV === "production",

apps/website/app/utils/supabase/client.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { createClient as createSupabaseClient } from "@supabase/supabase-js";
22
import { Database } from "@repo/database/types.gen.ts";
3+
import { envContents } from "@repo/database/dbDotEnv";
34

45
// Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth
56

67
export const createClient = () => {
7-
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
8-
const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
8+
const dbEnv = envContents();
9+
const url = dbEnv.SUPABASE_URL;
10+
const key = dbEnv.SUPABASE_ANON_KEY;
911

1012
if (!url || !key) {
1113
throw new Error("Missing required Supabase environment variables");

apps/website/app/utils/supabase/middleware.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { createServerClient } from "@supabase/ssr";
22
import { NextResponse, type NextRequest } from "next/server";
3+
import { envContents } from "@repo/database/dbDotEnv";
34

45
// Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth
56

67
export const updateSession = async (request: NextRequest) => {
7-
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
8-
const supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
8+
const dbEnv = envContents();
9+
const supabaseUrl = dbEnv.SUPABASE_URL;
10+
const supabaseKey = dbEnv.SUPABASE_ANON_KEY;
911

1012
if (!supabaseUrl || !supabaseKey) {
1113
throw new Error("Missing required Supabase environment variables");

apps/website/app/utils/supabase/server.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { createServerClient, type CookieOptions } from "@supabase/ssr";
22
import { cookies } from "next/headers";
3-
import { Database } from "@repo/database/types.gen.ts";
3+
import { Database } from "@repo/database/types.gen";
4+
import { envContents } from "@repo/database/dbDotEnv";
45

56
// Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth
67

78
export const createClient = async () => {
9+
const dbEnv = envContents();
810
const cookieStore = await cookies();
9-
const supabaseUrl = process.env.SUPABASE_URL;
10-
const supabaseKey = process.env.SUPABASE_ANON_KEY;
11+
const supabaseUrl = dbEnv.SUPABASE_URL;
12+
const supabaseKey = dbEnv.SUPABASE_ANON_KEY;
1113

1214
if (!supabaseUrl || !supabaseKey) {
1315
throw new Error("Missing required Supabase environment variables");

0 commit comments

Comments
 (0)