* Validates chat-based authentication for deployed chat voice mode * Checks if the user has a valid chat auth cookie for the given chatId
(request: NextRequest, chatId: string)
| 17 | * Checks if the user has a valid chat auth cookie for the given chatId |
| 18 | */ |
| 19 | async function validateChatAuth(request: NextRequest, chatId: string): Promise<boolean> { |
| 20 | try { |
| 21 | const chatResult = await db |
| 22 | .select({ |
| 23 | id: chat.id, |
| 24 | isActive: chat.isActive, |
| 25 | authType: chat.authType, |
| 26 | password: chat.password, |
| 27 | }) |
| 28 | .from(chat) |
| 29 | .where(eq(chat.id, chatId)) |
| 30 | .limit(1) |
| 31 | |
| 32 | if (chatResult.length === 0 || !chatResult[0].isActive) { |
| 33 | logger.warn('Chat not found or inactive for TTS auth:', chatId) |
| 34 | return false |
| 35 | } |
| 36 | |
| 37 | const chatData = chatResult[0] |
| 38 | |
| 39 | if (chatData.authType === 'public') { |
| 40 | return true |
| 41 | } |
| 42 | |
| 43 | const cookieName = `chat_auth_${chatId}` |
| 44 | const authCookie = request.cookies.get(cookieName) |
| 45 | |
| 46 | if ( |
| 47 | authCookie && |
| 48 | validateAuthToken(authCookie.value, chatId, chatData.authType, chatData.password) |
| 49 | ) { |
| 50 | return true |
| 51 | } |
| 52 | |
| 53 | return false |
| 54 | } catch (error) { |
| 55 | logger.error('Error validating chat auth for TTS:', error) |
| 56 | return false |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | export const POST = withRouteHandler(async (request: NextRequest) => { |
| 61 | try { |