From c66fb1d698b1eeda2d0e461657fd24f1503016c3 Mon Sep 17 00:00:00 2001 From: Alma Date: Fri, 11 Apr 2025 11:51:47 +0200 Subject: [PATCH] widget chat 10 --- app/api/auth/[...nextauth]/route.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index c251137c..fce1bbd5 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -139,13 +139,35 @@ export const authOptions: NextAuthOptions = { let personalToken: string | null = null; let rocketChatUserId: string | null = null; + // First, get the user's Rocket.Chat ID + const userInfoResponse = await fetch(`https://parole.slm-lab.net/api/v1/users.info?username=${newToken.username}`, { + headers: { + 'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!, + 'X-User-Id': process.env.ROCKET_CHAT_USER_ID!, + }, + }); + + if (!userInfoResponse.ok) { + console.error('Failed to get user info from Rocket.Chat'); + return newToken; + } + + const userInfo = await userInfoResponse.json(); + console.log('User info from Rocket.Chat:', userInfo); + + if (userInfo.user && userInfo.user._id) { + rocketChatUserId = userInfo.user._id; + } else { + console.error('No user ID found in Rocket.Chat response'); + return newToken; + } + if (tokensData.tokens && tokensData.tokens.length > 0) { // Use existing token const existingToken = tokensData.tokens.find((t: any) => t.name === tokenName); if (existingToken) { console.log('Found existing token:', existingToken); personalToken = existingToken.lastTokenPart; - rocketChatUserId = process.env.ROCKET_CHAT_USER_ID!; } } @@ -169,7 +191,6 @@ export const authOptions: NextAuthOptions = { const createTokenData = await createTokenResponse.json(); console.log('Created token data:', createTokenData); personalToken = createTokenData.token; - rocketChatUserId = process.env.ROCKET_CHAT_USER_ID!; } else { console.error('Failed to create personal access token'); return newToken;