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 ? undefined
: startParams.typebot, : startParams.typebot,
message, message,
userId: parseUserId(user?.id), userId: user?.id,
} }
: { : {
type: 'live', 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 ? undefined
: startParams.typebot, : startParams.typebot,
message, message,
userId: parseUserId(user?.id), userId: user?.id,
} }
: { : {
type: 'live', 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 { import {
startPreviewChatInputSchema, startPreviewChatInputSchema,
startPreviewChatResponseSchema, startPreviewChatResponseSchema,
@@ -6,8 +5,9 @@ import {
import { startSession } from '@typebot.io/bot-engine/startSession' import { startSession } from '@typebot.io/bot-engine/startSession'
import { saveStateToDatabase } from '@typebot.io/bot-engine/saveStateToDatabase' import { saveStateToDatabase } from '@typebot.io/bot-engine/saveStateToDatabase'
import { restartSession } from '@typebot.io/bot-engine/queries/restartSession' import { restartSession } from '@typebot.io/bot-engine/queries/restartSession'
import { publicProcedure } from '@/helpers/server/trpc'
export const startChatPreview = authenticatedProcedure export const startChatPreview = publicProcedure
.meta({ .meta({
openapi: { openapi: {
method: 'POST', method: 'POST',
@@ -47,7 +47,7 @@ export const startChatPreview = authenticatedProcedure
startFrom, startFrom,
typebotId, typebotId,
typebot: startTypebot, typebot: startTypebot,
userId: user.id, userId: user?.id,
}, },
message, message,
}) })

View File

@@ -1,3 +1,4 @@
import { publicProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server' import { TRPCError } from '@trpc/server'
import { z } from 'zod' import { z } from 'zod'
import { getSession } from '@typebot.io/bot-engine/queries/getSession' import { getSession } from '@typebot.io/bot-engine/queries/getSession'
@@ -8,9 +9,8 @@ import {
Variable, Variable,
} from '@typebot.io/schemas' } from '@typebot.io/schemas'
import prisma from '@typebot.io/lib/prisma' import prisma from '@typebot.io/lib/prisma'
import { authenticatedProcedure } from '@/helpers/server/trpc'
export const updateTypebotInSession = authenticatedProcedure export const updateTypebotInSession = publicProcedure
.meta({ .meta({
openapi: { openapi: {
method: 'POST', method: 'POST',
@@ -28,6 +28,8 @@ export const updateTypebotInSession = authenticatedProcedure
) )
.output(z.object({ message: z.literal('success') })) .output(z.object({ message: z.literal('success') }))
.mutation(async ({ input: { sessionId }, ctx: { user } }) => { .mutation(async ({ input: { sessionId }, ctx: { user } }) => {
if (!user)
throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Unauthorized' })
const session = await getSession(sessionId) const session = await getSession(sessionId)
if (!session) if (!session)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Session not found' }) 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 { OpenApiMeta } from 'trpc-openapi'
import superjson from 'superjson' import superjson from 'superjson'
import { Context } from './context' import { Context } from './context'
@@ -8,23 +8,13 @@ const t = initTRPC.context<Context>().meta<OpenApiMeta>().create({
transformer: superjson, transformer: superjson,
}) })
export const router = t.router
const sentryMiddleware = t.middleware( const sentryMiddleware = t.middleware(
Sentry.Handlers.trpcMiddleware({ Sentry.Handlers.trpcMiddleware({
attachRpcInput: true, attachRpcInput: true,
}) })
) )
export const publicProcedure = t.procedure.use(sentryMiddleware) const injectUser = t.middleware(({ next, ctx }) => {
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',
})
}
return next({ return next({
ctx: { ctx: {
user: ctx.user, user: ctx.user,
@@ -32,6 +22,10 @@ const isAuthed = t.middleware(({ next, ctx }) => {
}) })
}) })
export const authenticatedProcedure = t.procedure.use( const finalMiddleware = sentryMiddleware.unstable_pipe(injectUser)
sentryMiddleware.unstable_pipe(isAuthed)
) 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 StartParams =
| ({ | ({
type: 'preview' type: 'preview'
userId: string userId?: string
} & StartPreviewChatInput) } & StartPreviewChatInput)
| ({ | ({
type: 'live' type: 'live'
@@ -272,6 +272,13 @@ export const startSession = async ({
const getTypebot = async (startParams: StartParams): Promise<StartTypebot> => { const getTypebot = async (startParams: StartParams): Promise<StartTypebot> => {
if (startParams.type === 'preview' && startParams.typebot) if (startParams.type === 'preview' && startParams.typebot)
return 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 = const typebotQuery =
startParams.type === 'preview' startParams.type === 'preview'
? await findTypebot({ ? await findTypebot({