2
0

Revert new authentication method for preview bot

This commit is contained in:
Baptiste Arnaud
2023-11-13 15:45:11 +01:00
parent 084588a086
commit 06065c3e85
6 changed files with 26 additions and 43 deletions

View File

@ -90,7 +90,7 @@ export const sendMessageV1 = publicProcedure
? undefined
: startParams.typebot,
message,
userId: parseUserId(user?.id),
userId: user?.id,
}
: {
type: 'live',
@ -172,13 +172,3 @@ export const sendMessageV1 = publicProcedure
}
}
)
const parseUserId = (userId?: string): string => {
if (!userId)
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You need to be authenticated to perform this action',
})
return userId
}

View File

@ -90,7 +90,7 @@ export const sendMessageV2 = publicProcedure
? undefined
: startParams.typebot,
message,
userId: parseUserId(user?.id),
userId: user?.id,
}
: {
type: 'live',
@ -172,13 +172,3 @@ export const sendMessageV2 = publicProcedure
}
}
)
const parseUserId = (userId?: string): string => {
if (!userId)
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You need to be authenticated to perform this action',
})
return userId
}

View File

@ -1,4 +1,3 @@
import { authenticatedProcedure } from '@/helpers/server/trpc'
import {
startPreviewChatInputSchema,
startPreviewChatResponseSchema,
@ -6,8 +5,9 @@ import {
import { startSession } from '@typebot.io/bot-engine/startSession'
import { saveStateToDatabase } from '@typebot.io/bot-engine/saveStateToDatabase'
import { restartSession } from '@typebot.io/bot-engine/queries/restartSession'
import { publicProcedure } from '@/helpers/server/trpc'
export const startChatPreview = authenticatedProcedure
export const startChatPreview = publicProcedure
.meta({
openapi: {
method: 'POST',
@ -47,7 +47,7 @@ export const startChatPreview = authenticatedProcedure
startFrom,
typebotId,
typebot: startTypebot,
userId: user.id,
userId: user?.id,
},
message,
})

View File

@ -1,3 +1,4 @@
import { publicProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
import { getSession } from '@typebot.io/bot-engine/queries/getSession'
@ -8,9 +9,8 @@ import {
Variable,
} from '@typebot.io/schemas'
import prisma from '@typebot.io/lib/prisma'
import { authenticatedProcedure } from '@/helpers/server/trpc'
export const updateTypebotInSession = authenticatedProcedure
export const updateTypebotInSession = publicProcedure
.meta({
openapi: {
method: 'POST',
@ -28,6 +28,8 @@ export const updateTypebotInSession = authenticatedProcedure
)
.output(z.object({ message: z.literal('success') }))
.mutation(async ({ input: { sessionId }, ctx: { user } }) => {
if (!user)
throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Unauthorized' })
const session = await getSession(sessionId)
if (!session)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Session not found' })

View File

@ -1,4 +1,4 @@
import { TRPCError, initTRPC } from '@trpc/server'
import { initTRPC } from '@trpc/server'
import { OpenApiMeta } from 'trpc-openapi'
import superjson from 'superjson'
import { Context } from './context'
@ -8,23 +8,13 @@ const t = initTRPC.context<Context>().meta<OpenApiMeta>().create({
transformer: superjson,
})
export const router = t.router
const sentryMiddleware = t.middleware(
Sentry.Handlers.trpcMiddleware({
attachRpcInput: true,
})
)
export const publicProcedure = t.procedure.use(sentryMiddleware)
const isAuthed = t.middleware(({ next, ctx }) => {
if (!ctx.user?.id) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You need to be authenticated to perform this action',
})
}
const injectUser = t.middleware(({ next, ctx }) => {
return next({
ctx: {
user: ctx.user,
@ -32,6 +22,10 @@ const isAuthed = t.middleware(({ next, ctx }) => {
})
})
export const authenticatedProcedure = t.procedure.use(
sentryMiddleware.unstable_pipe(isAuthed)
)
const finalMiddleware = sentryMiddleware.unstable_pipe(injectUser)
export const middleware = t.middleware
export const router = t.router
export const publicProcedure = t.procedure.use(finalMiddleware)

View File

@ -39,7 +39,7 @@ import { VisitedEdge } from '@typebot.io/prisma'
type StartParams =
| ({
type: 'preview'
userId: string
userId?: string
} & StartPreviewChatInput)
| ({
type: 'live'
@ -272,6 +272,13 @@ export const startSession = async ({
const getTypebot = async (startParams: StartParams): Promise<StartTypebot> => {
if (startParams.type === 'preview' && startParams.typebot)
return startParams.typebot
if (startParams.type === 'preview' && !startParams.userId)
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You need to be authenticated to perform this action',
})
const typebotQuery =
startParams.type === 'preview'
? await findTypebot({