-
Notifications
You must be signed in to change notification settings - Fork 3
ENG-520: break-out infrastructure from ENG-373 #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { createClient as createSupabaseClient } from "@supabase/supabase-js"; | ||
| import { Database } from "@repo/database/types.gen.ts"; | ||
|
|
||
| // Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth | ||
|
|
||
| export const createClient = () => { | ||
| const url = process.env.NEXT_PUBLIC_SUPABASE_URL; | ||
| const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; | ||
|
|
||
| if (!url || !key) { | ||
| throw new Error("Missing required Supabase environment variables"); | ||
| } | ||
| return createSupabaseClient<Database, "public", Database["public"]>(url, key); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { createServerClient } from "@supabase/ssr"; | ||
| import { NextResponse, type NextRequest } from "next/server"; | ||
|
|
||
| // Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth | ||
|
|
||
| export const updateSession = async (request: NextRequest) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not used yet; I was following the documented pattern in the url above.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I briefly scanned the URL but didn't explicitly see it's use, only the code. Do you have a sense of where we will use it in the future? |
||
| const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; | ||
| const supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; | ||
|
|
||
| if (!supabaseUrl || !supabaseKey) { | ||
| throw new Error("Missing required Supabase environment variables"); | ||
| } | ||
|
|
||
| let supabaseResponse = NextResponse.next({ request }); | ||
|
|
||
| const supabase = createServerClient(supabaseUrl, supabaseKey, { | ||
| cookies: { | ||
| getAll() { | ||
| return request.cookies.getAll(); | ||
| }, | ||
| setAll(cookiesToSet) { | ||
| cookiesToSet.forEach(({ name, value }) => | ||
| request.cookies.set(name, value), | ||
| ); | ||
| supabaseResponse = NextResponse.next({ | ||
| request, | ||
| }); | ||
| cookiesToSet.forEach(({ name, value, options }) => | ||
| supabaseResponse.cookies.set(name, value, options), | ||
| ); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| // Do not run code between createServerClient and | ||
| // supabase.auth.getUser(). A simple mistake could make it very hard to debug | ||
| // issues with users being randomly logged out. | ||
|
|
||
| // IMPORTANT: DO NOT REMOVE auth.getUser() | ||
|
|
||
| const { | ||
| data: { user }, | ||
| } = await supabase.auth.getUser(); | ||
|
|
||
| if ( | ||
| !user && | ||
| !request.nextUrl.pathname.startsWith("/login") && | ||
| !request.nextUrl.pathname.startsWith("/auth") | ||
| ) { | ||
| // no user, potentially respond by redirecting the user to the login page | ||
| const url = request.nextUrl.clone(); | ||
| url.pathname = "/auth/login"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this will be added in #238 |
||
| return NextResponse.redirect(url); | ||
| } | ||
|
|
||
| // IMPORTANT: You *must* return the supabaseResponse object as it is. | ||
| // If you're creating a new response object with NextResponse.next() make sure to: | ||
| // 1. Pass the request in it, like so: | ||
| // const myNewResponse = NextResponse.next({ request }) | ||
| // 2. Copy over the cookies, like so: | ||
| // myNewResponse.cookies.setAll(supabaseResponse.cookies.getAll()) | ||
| // 3. Change the myNewResponse object to fit your needs, but avoid changing | ||
| // the cookies! | ||
| // 4. Finally: | ||
| // return myNewResponse | ||
| // If this is not done, you may be causing the browser and server to go out | ||
| // of sync and terminate the user's session prematurely! | ||
|
|
||
| return supabaseResponse; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| { | ||
| "extends": "@repo/typescript-config/nextjs.json", | ||
| "compilerOptions": { | ||
| "baseUrl": ".", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose for adding this? Or problem is this solving?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is fine, it is just helpful to add the context. |
||
| "paths": { | ||
| "~/*": ["./app/*"] | ||
| }, | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { createClient as createSupabaseClient } from "@supabase/supabase-js"; | ||
| import { Database } from "@repo/database/types.gen.ts"; | ||
|
|
||
| declare const SUPABASE_URL: string; | ||
maparent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| declare const SUPABASE_ANON_KEY: string; | ||
|
|
||
| // Inspired by https://supabase.com/ui/docs/react/password-based-auth | ||
|
|
||
| export const createClient = () => { | ||
| return createSupabaseClient<Database, "public", Database["public"]>( | ||
| SUPABASE_URL, | ||
| SUPABASE_ANON_KEY, | ||
| ); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.