11import { NextResponse , NextRequest } from "next/server" ;
22import { createClient } from "~/utils/supabase/server" ;
3+ import { createClientAdmin } from "~/utils/supabase/serverAdmin" ;
34import { asPostgrestFailure } from "@repo/database/lib/contextFunctions" ;
45import {
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
0 commit comments