Skip to content

Commit 56bd94d

Browse files
committed
Expose public content
1 parent 2a9684f commit 56bd94d

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

apps/website/app/api/content/[space_id]/[resource_id]/route.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextResponse, NextRequest } from "next/server";
22
import { createClient } from "~/utils/supabase/server";
3+
import { createClientAdmin } from "~/utils/supabase/serverAdmin";
34
import { asPostgrestFailure } from "@repo/database/lib/contextFunctions";
45
import {
56
defaultOptionsHandler,
@@ -51,7 +52,7 @@ export const GET = async (
5152
);
5253
}
5354
const supabase = await createClient();
54-
const spaceResponse = await supabase
55+
let spaceResponse = await supabase
5556
.from("Space")
5657
.select()
5758
.eq("id", spaceIdN)
@@ -60,12 +61,21 @@ export const GET = async (
6061
return createApiResponse(request, spaceResponse);
6162
}
6263
if (!spaceResponse.data) {
63-
// consideration: We may not see it because we don't have access,
64-
// so it would be worth re-fetching as superuser to see if I should redirect to login.
65-
return createApiResponse(
66-
request,
67-
asPostgrestFailure("Space not found", "401", 401),
68-
);
64+
// We may not see it because we don't have access, try as admin
65+
const supabaseAdmin = createClientAdmin();
66+
spaceResponse = await supabaseAdmin
67+
.from("Space")
68+
.select()
69+
.eq("id", spaceIdN)
70+
.maybeSingle();
71+
if (spaceResponse.error) {
72+
return createApiResponse(request, spaceResponse);
73+
}
74+
if (!spaceResponse.data)
75+
return createApiResponse(
76+
request,
77+
asPostgrestFailure("Please login", "401", 401),
78+
);
6979
}
7080
const space: Space = spaceResponse.data;
7181
const conceptResponse = await supabase
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { createClient } from "@supabase/supabase-js";
2+
import type { Database } from "@repo/database/dbTypes";
3+
import { envContents } from "@repo/database/dbDotEnv";
4+
5+
// This is a supabase client with admin rights. Use sparingly.
6+
export const createClientAdmin = () => {
7+
const dbEnv = envContents();
8+
const supabaseUrl = dbEnv.SUPABASE_URL;
9+
const supabaseKey = dbEnv.SUPABASE_SECRET_KEY;
10+
11+
if (!supabaseUrl || !supabaseKey) {
12+
throw new Error("Missing required Supabase environment variables");
13+
}
14+
15+
// following https://supabase.com/docs/guides/auth/server-side/creating-a-client?queryGroups=environment&environment=server
16+
return createClient<Database>(supabaseUrl, supabaseKey);
17+
};

0 commit comments

Comments
 (0)