2
0

Customizable allowed origins

This commit is contained in:
Baptiste Arnaud
2024-01-17 09:04:07 +01:00
parent b2f8cd44b8
commit 8771def9a1
12 changed files with 151 additions and 5 deletions

View File

@@ -28,7 +28,7 @@ export const continueChat = publicProcedure
})
)
.output(continueChatResponseSchema)
.mutation(async ({ input: { sessionId, message } }) => {
.mutation(async ({ input: { sessionId, message }, ctx: { res, origin } }) => {
const session = await getSession(sessionId)
if (!session) {
@@ -49,6 +49,15 @@ export const continueChat = publicProcedure
message: 'Session expired. You need to start a new session.',
})
if (
session?.state.allowedOrigins &&
session.state.allowedOrigins.length > 0
) {
if (origin && session.state.allowedOrigins.includes(origin))
res.setHeader('Access-Control-Allow-Origin', origin)
else res.removeHeader('Access-Control-Allow-Origin')
}
const {
messages,
input,

View File

@@ -28,6 +28,7 @@ export const startChat = publicProcedure
prefilledVariables,
resultId: startResultId,
},
ctx: { origin, res },
}) => {
const {
typebot,
@@ -52,6 +53,15 @@ export const startChat = publicProcedure
message,
})
if (
newSessionState.allowedOrigins &&
newSessionState.allowedOrigins.length > 0
) {
if (origin && newSessionState.allowedOrigins.includes(origin))
res.setHeader('Access-Control-Allow-Origin', origin)
else res.removeHeader('Access-Control-Allow-Origin')
}
const session = isOnlyRegistering
? await restartSession({
state: newSessionState,

View File

@@ -11,6 +11,8 @@ export async function createContext(opts: trpcNext.CreateNextContextOptions) {
return {
user,
origin: opts.req.headers.origin,
res: opts.res,
}
}