From bb3acefc1ab224de0ac984be56deb11e15a84657 Mon Sep 17 00:00:00 2001 From: Alma Date: Wed, 9 Apr 2025 23:54:33 +0200 Subject: [PATCH] update widget token mail 11 --- app/api/rocket-chat/messages/route.ts | 82 +++++++++++++-------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/app/api/rocket-chat/messages/route.ts b/app/api/rocket-chat/messages/route.ts index 2430107f..b8d87bd1 100644 --- a/app/api/rocket-chat/messages/route.ts +++ b/app/api/rocket-chat/messages/route.ts @@ -56,65 +56,65 @@ export async function GET() { ); } - // Get Rocket.Chat OAuth token - const rocketChatAuthResponse = await fetch( - 'https://parole.slm-lab.net/api/v1/oauth/token', + // First authenticate as admin to get an auth token + const adminLoginResponse = await fetch( + 'https://parole.slm-lab.net/api/v1/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ - grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange', - client_id: process.env.ROCKET_CHAT_CLIENT_ID, - client_secret: process.env.ROCKET_CHAT_CLIENT_SECRET, - subject_token: session.accessToken, - subject_token_type: 'urn:ietf:params:oauth:token-type:access_token' + user: process.env.ROCKET_CHAT_ADMIN_USERNAME, + password: process.env.ROCKET_CHAT_ADMIN_PASSWORD }) } ); - if (!rocketChatAuthResponse.ok) { - console.error('Rocket.Chat OAuth login error:', { - status: rocketChatAuthResponse.status, - statusText: rocketChatAuthResponse.statusText, - response: await rocketChatAuthResponse.text().catch(() => 'Could not get response text') + if (!adminLoginResponse.ok) { + console.error('Rocket.Chat admin login error:', { + status: adminLoginResponse.status, + statusText: adminLoginResponse.statusText, + response: await adminLoginResponse.text().catch(() => 'Could not get response text') }); return NextResponse.json( { error: "Failed to authenticate with Rocket.Chat" }, - { status: rocketChatAuthResponse.status } + { status: adminLoginResponse.status } ); } - const rocketChatAuth = await rocketChatAuthResponse.json(); - console.log('Rocket.Chat auth success:', { - hasToken: !!rocketChatAuth.access_token + const adminAuth = await adminLoginResponse.json(); + console.log('Admin auth success:', { + hasToken: !!adminAuth.data?.authToken }); - // Now get user info from Rocket.Chat using the obtained token - const meResponse = await fetch('https://parole.slm-lab.net/api/v1/me', { - headers: { - 'Authorization': `Bearer ${rocketChatAuth.access_token}` - }, - cache: 'no-store', - }); + // Get user info by username using admin token + const userInfoResponse = await fetch( + `https://parole.slm-lab.net/api/v1/users.info?username=${username}`, + { + headers: { + 'X-Auth-Token': adminAuth.data.authToken, + 'X-User-Id': adminAuth.data.userId + } + } + ); - if (!meResponse.ok) { - console.error('Rocket.Chat me error:', { - status: meResponse.status, - statusText: meResponse.statusText, - response: await meResponse.text().catch(() => 'Could not get response text') + if (!userInfoResponse.ok) { + console.error('Failed to get user info:', { + status: userInfoResponse.status, + statusText: userInfoResponse.statusText, + response: await userInfoResponse.text().catch(() => 'Could not get response text') }); return NextResponse.json( - { error: "Failed to get user info from Rocket.Chat" }, - { status: meResponse.status } + { error: "Failed to get user info" }, + { status: userInfoResponse.status } ); } - const userData = await meResponse.json(); - console.log('Rocket.Chat user data:', { - userId: userData._id, - username: userData.username + const userInfo = await userInfoResponse.json(); + console.log('User info success:', { + userId: userInfo.user._id, + username: userInfo.user.username }); // Get the user's subscriptions (rooms they are in) @@ -122,9 +122,9 @@ export async function GET() { 'https://parole.slm-lab.net/api/v1/subscriptions.get', { headers: { - 'Authorization': `Bearer ${rocketChatAuth.access_token}` - }, - cache: 'no-store', + 'X-Auth-Token': adminAuth.data.authToken, + 'X-User-Id': userInfo.user._id + } } ); @@ -154,9 +154,9 @@ export async function GET() { `https://parole.slm-lab.net/api/v1/chat.getMessage?msgId=${room.lastMessage._id}`, { headers: { - 'Authorization': `Bearer ${rocketChatAuth.access_token}` - }, - cache: 'no-store', + 'X-Auth-Token': adminAuth.data.authToken, + 'X-User-Id': userInfo.user._id + } } );