Skip to content

Commit e9c68b0

Browse files
authored
ENG-520: break-out infrastructure from ENG-373 (#244)
This allow us to call upon supabase client in frontend code. It is too early to do so safely, but it is at least possible to add that infrastructure. An unfortunate consequence of this is that turbo now expects SUPABASE_URL and SUPABASE_ANON_KEY to be defined. We will all need to update our .env accordingly. This should be fine with vercel and github actions.
1 parent 5f582b8 commit e9c68b0

File tree

12 files changed

+225
-32
lines changed

12 files changed

+225
-32
lines changed

.github/workflows/roam-pr.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ env:
1616
jobs:
1717
deploy:
1818
runs-on: ubuntu-latest
19+
env:
20+
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
21+
SUPABASE_ANON_KEY: ${{ secrets.SUPABASE_ANON_KEY }}
1922
steps:
2023
- name: Checkout Code
2124
uses: actions/checkout@v4

apps/roam/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
"@octokit/auth-app": "^7.1.4",
2929
"@octokit/core": "^6.1.3",
3030
"@repo/types": "*",
31+
"@supabase/auth-js": "^2.70.0",
32+
"@supabase/supabase-js": "^2.50.0",
3133
"@tldraw/tldraw": "^2.0.0-alpha.12",
3234
"@vercel/blob": "^0.27.0",
3335
"contrast-color": "^1.0.1",

apps/roam/scripts/compile.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ export const compile = ({
129129
outdir,
130130
bundle: true,
131131
format,
132+
define: {
133+
SUPABASE_URL: JSON.stringify(process.env.SUPABASE_URL!),
134+
SUPABASE_ANON_KEY: JSON.stringify(process.env.SUPABASE_ANON_KEY!)
135+
},
132136
sourcemap: process.env.NODE_ENV === "production" ? undefined : "inline",
133137
minify: process.env.NODE_ENV === "production",
134138
entryNames: out,

apps/roam/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"module": "ESNext",
1313
"moduleResolution": "Node",
1414
"forceConsistentCasingInFileNames": true,
15-
1615
"jsx": "react",
1716
"noUncheckedIndexedAccess": false
1817
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { createClient as createSupabaseClient } from "@supabase/supabase-js";
2+
import { Database } from "@repo/database/types.gen.ts";
3+
4+
// Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth
5+
6+
export const createClient = () => {
7+
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
8+
const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
9+
10+
if (!url || !key) {
11+
throw new Error("Missing required Supabase environment variables");
12+
}
13+
return createSupabaseClient<Database, "public", Database["public"]>(url, key);
14+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { createServerClient } from "@supabase/ssr";
2+
import { NextResponse, type NextRequest } from "next/server";
3+
4+
// Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth
5+
6+
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;
9+
10+
if (!supabaseUrl || !supabaseKey) {
11+
throw new Error("Missing required Supabase environment variables");
12+
}
13+
14+
let supabaseResponse = NextResponse.next({ request });
15+
16+
const supabase = createServerClient(supabaseUrl, supabaseKey, {
17+
cookies: {
18+
getAll() {
19+
return request.cookies.getAll();
20+
},
21+
setAll(cookiesToSet) {
22+
cookiesToSet.forEach(({ name, value }) =>
23+
request.cookies.set(name, value),
24+
);
25+
supabaseResponse = NextResponse.next({
26+
request,
27+
});
28+
cookiesToSet.forEach(({ name, value, options }) =>
29+
supabaseResponse.cookies.set(name, value, options),
30+
);
31+
},
32+
},
33+
});
34+
35+
// Do not run code between createServerClient and
36+
// supabase.auth.getUser(). A simple mistake could make it very hard to debug
37+
// issues with users being randomly logged out.
38+
39+
// IMPORTANT: DO NOT REMOVE auth.getUser()
40+
41+
const {
42+
data: { user },
43+
} = await supabase.auth.getUser();
44+
45+
if (
46+
!user &&
47+
!request.nextUrl.pathname.startsWith("/login") &&
48+
!request.nextUrl.pathname.startsWith("/auth")
49+
) {
50+
// no user, potentially respond by redirecting the user to the login page
51+
const url = request.nextUrl.clone();
52+
url.pathname = "/auth/login";
53+
return NextResponse.redirect(url);
54+
}
55+
56+
// IMPORTANT: You *must* return the supabaseResponse object as it is.
57+
// If you're creating a new response object with NextResponse.next() make sure to:
58+
// 1. Pass the request in it, like so:
59+
// const myNewResponse = NextResponse.next({ request })
60+
// 2. Copy over the cookies, like so:
61+
// myNewResponse.cookies.setAll(supabaseResponse.cookies.getAll())
62+
// 3. Change the myNewResponse object to fit your needs, but avoid changing
63+
// the cookies!
64+
// 4. Finally:
65+
// return myNewResponse
66+
// If this is not done, you may be causing the browser and server to go out
67+
// of sync and terminate the user's session prematurely!
68+
69+
return supabaseResponse;
70+
};

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { createServerClient, type CookieOptions } from "@supabase/ssr";
22
import { cookies } from "next/headers";
33
import { Database } from "@repo/database/types.gen.ts";
44

5+
// Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth
6+
57
export const createClient = async () => {
68
const cookieStore = await cookies();
79
const supabaseUrl = process.env.SUPABASE_URL;

apps/website/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "@repo/typescript-config/nextjs.json",
33
"compilerOptions": {
4+
"baseUrl": ".",
45
"paths": {
56
"~/*": ["./app/*"]
67
},

package-lock.json

Lines changed: 100 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ui/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"ui": "npx shadcn@latest"
2323
},
2424
"devDependencies": {
25-
"@repo/tailwind-config": "*",
2625
"@repo/eslint-config": "*",
26+
"@repo/tailwind-config": "*",
2727
"@repo/typescript-config": "*",
2828
"@turbo/gen": "^1.12.4",
2929
"@types/eslint": "^8.56.5",
@@ -34,6 +34,8 @@
3434
"typescript": "5.5.4"
3535
},
3636
"dependencies": {
37+
"@supabase/auth-ui-react": "0.4.7",
38+
"@supabase/supabase-js": "^2.50.0",
3739
"class-variance-authority": "^0.7.1",
3840
"clsx": "^2.1.1",
3941
"lucide-react": "^0.468.0",

0 commit comments

Comments
 (0)