diff --git a/app/api/rocket-chat/messages/route.ts b/app/api/rocket-chat/messages/route.ts index bd0206a..ba9acbf 100644 --- a/app/api/rocket-chat/messages/route.ts +++ b/app/api/rocket-chat/messages/route.ts @@ -73,7 +73,16 @@ export async function GET(request: Request) { } } - const baseUrl = process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL?.split('/channel')[0]; + // Extract base URL, removing any paths like /channel, /_oauth/connect, etc. + let baseUrl = process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL; + if (baseUrl) { + // Remove /channel and everything after + baseUrl = baseUrl.split('/channel')[0]; + // Remove /_oauth/connect and everything after + baseUrl = baseUrl.split('/_oauth')[0]; + // Remove trailing slashes + baseUrl = baseUrl.replace(/\/+$/, ''); + } if (!baseUrl) { logger.error('[ROCKET_CHAT] Failed to get Rocket.Chat base URL'); return NextResponse.json({ error: 'Server configuration error' }, { status: 500 }); diff --git a/app/api/rocket-chat/user-token/route.ts b/app/api/rocket-chat/user-token/route.ts index 486310b..7a9ade2 100644 --- a/app/api/rocket-chat/user-token/route.ts +++ b/app/api/rocket-chat/user-token/route.ts @@ -18,7 +18,16 @@ export async function GET(request: Request) { ); } - const baseUrl = process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL?.split('/channel')[0]; + // Extract base URL, removing any paths like /channel, /_oauth/connect, etc. + let baseUrl = process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL; + if (baseUrl) { + // Remove /channel and everything after + baseUrl = baseUrl.split('/channel')[0]; + // Remove /_oauth/connect and everything after + baseUrl = baseUrl.split('/_oauth')[0]; + // Remove trailing slashes + baseUrl = baseUrl.replace(/\/+$/, ''); + } if (!baseUrl) { logger.error('[ROCKET_CHAT_USER_TOKEN] Failed to get Rocket.Chat base URL'); return NextResponse.json(