Skip to content

Commit acbb672

Browse files
committed
fix: Handle studio requets before auth checks
1 parent 9c15915 commit acbb672

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/index.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createResponse } from "./utils";
22
import { StarbaseDB, StarbaseDBConfiguration } from "./handler";
33
import { DataSource, RegionLocationHint } from "./types";
44
import { createRemoteJWKSet, jwtVerify } from "jose";
5+
import { handleStudioRequest } from "./studio";
56

67
export { StarbaseDBDurableObject } from "./do";
78

@@ -65,6 +66,22 @@ export default {
6566
let role: StarbaseDBConfiguration["role"] = "client";
6667
let context = {};
6768

69+
// Handle Studio requests before auth checks in the worker.
70+
// StarbaseDB can handle this for us, but we need to handle it
71+
// here before auth checks.
72+
if (
73+
env.STUDIO_USER &&
74+
env.STUDIO_PASS &&
75+
request.method === "GET" &&
76+
url.pathname === "/studio"
77+
) {
78+
return handleStudioRequest(request, {
79+
username: env.STUDIO_USER,
80+
password: env.STUDIO_PASS,
81+
apiKey: env.ADMIN_AUTHORIZATION_TOKEN,
82+
});
83+
}
84+
6885
async function authenticate(token: string) {
6986
const isAdminAuthorization = token === env.ADMIN_AUTHORIZATION_TOKEN;
7087
const isClientAuthorization = token === env.CLIENT_AUTHORIZATION_TOKEN;
@@ -214,22 +231,17 @@ export default {
214231
features: {
215232
allowlist: env.ENABLE_ALLOWLIST,
216233
rls: env.ENABLE_RLS,
234+
studio: false, // This is handled above in the worker flow.
217235
},
218236
};
219237

220-
if (env.STUDIO_USER && env.STUDIO_PASS) {
221-
config.studio = {
222-
username: env.STUDIO_USER,
223-
password: env.STUDIO_PASS,
224-
apiKey: env.ADMIN_AUTHORIZATION_TOKEN,
225-
};
226-
}
227-
228-
// Return the final response to our user
229-
return await new StarbaseDB({
238+
const starbase = new StarbaseDB({
230239
dataSource,
231240
config,
232-
}).handle(request, ctx);
241+
});
242+
243+
// Return the final response to our user
244+
return await starbase.handle(request, ctx);
233245
} catch (error) {
234246
// Return error response to client
235247
return createResponse(

0 commit comments

Comments
 (0)