11import { Client } from "pg" ;
22
33interface Env {
4- AGENT_API_TOKEN ?: string ;
54 OPERATOR_API_TOKEN ?: string ;
65 OPERATOR_EMAILS ?: string ;
76 DATABASE_URL ?: string ;
@@ -464,7 +463,7 @@ async function requireAuth(request: Request, env: Env, scope: "agent" | "operato
464463 }
465464 }
466465
467- const configuredToken = scope === "agent " ? env . AGENT_API_TOKEN : env . OPERATOR_API_TOKEN ;
466+ const configuredToken = scope === "operator " ? env . OPERATOR_API_TOKEN : undefined ;
468467 const header = request . headers . get ( "authorization" ) ?? "" ;
469468 const token = header . startsWith ( "Bearer " ) ? header . slice ( "Bearer " . length ) : "" ;
470469 if ( configuredToken && token === configuredToken ) return { ok : true } ;
@@ -484,7 +483,7 @@ async function requireAuth(request: Request, env: Env, scope: "agent" | "operato
484483 if ( tokenRow ) return { ok : false , response : json ( { error : "Agent access is not approved." } , 403 ) } ;
485484 }
486485 }
487- if ( ! configuredToken && scope !== "agent ") return { ok : false , response : json ( { error : "Auth token is not configured." } , 503 ) } ;
486+ if ( ! configuredToken && scope === "operator ") return { ok : false , response : json ( { error : "Auth token is not configured." } , 503 ) } ;
488487 return { ok : false , response : json ( { error : "Unauthorized." } , 401 ) } ;
489488}
490489
@@ -1737,6 +1736,8 @@ export async function onRequest(context: { request: Request; env: Env }) {
17371736 const url = new URL ( request . url ) ;
17381737 const path = url . pathname . replace ( / ^ \/ a p i \/ ? / , "" ) ;
17391738 const method = request . method . toUpperCase ( ) ;
1739+ if ( method === "POST" && path === "agent/signup-requests" ) return requestSignup ( request , env ) ;
1740+
17401741 const scope = path . startsWith ( "operator/" ) ? "operator" : "agent" ;
17411742 const auth = await requireAuth ( request , env , scope ) ;
17421743 if ( ! auth . ok ) return auth . response ;
@@ -1754,7 +1755,6 @@ export async function onRequest(context: { request: Request; env: Env }) {
17541755 if ( method === "GET" && path === "agent/threads" ) return listThreads ( env , url . searchParams . get ( "forumId" ) ) ;
17551756 if ( method === "POST" && path === "agent/threads" ) return createThread ( request , env , auth ) ;
17561757 if ( method === "POST" && path === "agent/thread-replies" ) return createAgentThreadReply ( request , env , auth ) ;
1757- if ( method === "POST" && path === "agent/signup-requests" ) return requestSignup ( request , env ) ;
17581758 if ( method === "GET" && path . startsWith ( "agent/direct-messages/" ) ) {
17591759 return readDirectMessages (
17601760 env ,
0 commit comments