Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/auth/confirm/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export async function GET(request: NextRequest): Promise<void> {
return redirect("/auth/auth-code-error");
}

return redirect("/");
return redirect("/volunteers");
}
3 changes: 1 addition & 2 deletions src/app/globals.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@import "tailwindcss";
@import url("https://fonts.googleapis.com/css2?family=Manrope:wght@400;700;800&display=swap");
@import "../styles/variables.css";
@import "tailwindcss";

:root {
--background: #ffffff;
Expand Down
17 changes: 13 additions & 4 deletions src/lib/client/supabase/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import { NextResponse, type NextRequest } from "next/server";
const publicPathPrefixes = ["/auth", "/animation-test", "/login"];

function isPublicPath(pathname: string): boolean {
return (
pathname === "/" ||
publicPathPrefixes.some((path) => pathname.startsWith(path))
);
return publicPathPrefixes.some((path) => pathname.startsWith(path));
}

export async function updateSession(
request: NextRequest
): Promise<NextResponse> {
if (request.nextUrl.pathname === "/") {
const url = request.nextUrl.clone();
url.pathname = "/volunteers";
return NextResponse.redirect(url);
}

let supabaseResponse = NextResponse.next({
request,
});
Expand Down Expand Up @@ -51,6 +54,12 @@ export async function updateSession(
const { data } = await supabase.auth.getClaims();
const user = data?.claims;

if (!user && request.nextUrl.pathname === "/volunteers") {
const url = request.nextUrl.clone();
url.pathname = "/login";
return NextResponse.redirect(url);
}

if (!user && !isPublicPath(request.nextUrl.pathname)) {
// no user, potentially respond by redirecting the user to the login page
const url = request.nextUrl.clone();
Expand Down
43 changes: 31 additions & 12 deletions src/lib/client/supabase/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,45 @@ import type { SupabaseClient } from "@supabase/supabase-js";
import { createClient as createBrowserClient } from "@supabase/supabase-js";
import * as process from "node:process";

function getSupabaseUrl(preferLocal: boolean): string {
const url = preferLocal
? (process.env["API_URL"] ?? process.env["NEXT_PUBLIC_SUPABASE_URL"])
: (process.env["NEXT_PUBLIC_SUPABASE_URL"] ?? process.env["API_URL"]);
if (!url) {
throw new Error("Missing env var API_URL or NEXT_PUBLIC_SUPABASE_URL");
}
return url;
}

function getPublishableKey(preferLocal: boolean): string {
const key = preferLocal
? (process.env["PUBLISHABLE_KEY"] ??
process.env["NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY"])
: (process.env["NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY"] ??
process.env["PUBLISHABLE_KEY"]);
if (!key) {
throw new Error(
"Missing env var PUBLISHABLE_KEY or NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY"
);
}
return key;
}

// During tests we prefer to use the plain `@supabase/supabase-js` client
// because Next's `cookies()` and `@supabase/ssr` internals are not
// available in the Vitest environment.
export async function createClient(): Promise<SupabaseClient<Database>> {
if (
process.env.NODE_ENV === "test" ||
process.env["SUPABASE_TESTING"] === "1" ||
process.env.NODE_ENV === "development"
process.env["SUPABASE_TESTING"] === "1"
) {
// In tests we connect to a local Supabase instance
// and do not use SSR or cookies
console.log("Creating Supabase client locally for tests...");

return createBrowserClient<Database>(
process.env["API_URL"]!,
process.env["PUBLISHABLE_KEY"]!,
getSupabaseUrl(true),
getPublishableKey(true),
{
global: {
headers: {
Expand All @@ -37,8 +60,8 @@ export async function createClient(): Promise<SupabaseClient<Database>> {
console.log("Creating Supabase SSR client...");
const cookieStore = await cookies();
return createServerClient<Database>(
process.env["API_URL"]!,
process.env["PUBLISHABLE_KEY"]!,
getSupabaseUrl(false),
getPublishableKey(false),
{
cookies: {
getAll() {
Expand All @@ -61,21 +84,17 @@ export async function createClient(): Promise<SupabaseClient<Database>> {
}

export function createAdminClient(): SupabaseClient<Database> {
// Prefer SUPABASE_SERVICE_ROLE_KEY to avoid collisions with other tooling that
// may also define SERVICE_ROLE_KEY with a non-Supabase token.
const supabaseUrl = getSupabaseUrl(false);
const serviceRoleKey =
process.env["SUPABASE_SERVICE_ROLE_KEY"] ?? process.env["SERVICE_ROLE_KEY"];

if (!process.env["API_URL"]) {
throw new Error("Missing env var API_URL");
}
if (!serviceRoleKey) {
throw new Error(
"Missing env var SERVICE_ROLE_KEY or SUPABASE_SERVICE_ROLE_KEY"
);
}

return createBrowserClient<Database>(process.env["API_URL"], serviceRoleKey, {
return createBrowserClient<Database>(supabaseUrl, serviceRoleKey, {
global: {
headers: {
Authorization: `Bearer ${serviceRoleKey}`,
Expand Down
Loading