update widget token mail 11

This commit is contained in:
Alma 2025-04-09 23:54:33 +02:00
parent 35e689a1e3
commit bb3acefc1a

View File

@ -56,65 +56,65 @@ export async function GET() {
); );
} }
// Get Rocket.Chat OAuth token // First authenticate as admin to get an auth token
const rocketChatAuthResponse = await fetch( const adminLoginResponse = await fetch(
'https://parole.slm-lab.net/api/v1/oauth/token', 'https://parole.slm-lab.net/api/v1/login',
{ {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify({ body: JSON.stringify({
grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange', user: process.env.ROCKET_CHAT_ADMIN_USERNAME,
client_id: process.env.ROCKET_CHAT_CLIENT_ID, password: process.env.ROCKET_CHAT_ADMIN_PASSWORD
client_secret: process.env.ROCKET_CHAT_CLIENT_SECRET,
subject_token: session.accessToken,
subject_token_type: 'urn:ietf:params:oauth:token-type:access_token'
}) })
} }
); );
if (!rocketChatAuthResponse.ok) { if (!adminLoginResponse.ok) {
console.error('Rocket.Chat OAuth login error:', { console.error('Rocket.Chat admin login error:', {
status: rocketChatAuthResponse.status, status: adminLoginResponse.status,
statusText: rocketChatAuthResponse.statusText, statusText: adminLoginResponse.statusText,
response: await rocketChatAuthResponse.text().catch(() => 'Could not get response text') response: await adminLoginResponse.text().catch(() => 'Could not get response text')
}); });
return NextResponse.json( return NextResponse.json(
{ error: "Failed to authenticate with Rocket.Chat" }, { error: "Failed to authenticate with Rocket.Chat" },
{ status: rocketChatAuthResponse.status } { status: adminLoginResponse.status }
); );
} }
const rocketChatAuth = await rocketChatAuthResponse.json(); const adminAuth = await adminLoginResponse.json();
console.log('Rocket.Chat auth success:', { console.log('Admin auth success:', {
hasToken: !!rocketChatAuth.access_token hasToken: !!adminAuth.data?.authToken
}); });
// Now get user info from Rocket.Chat using the obtained token // Get user info by username using admin token
const meResponse = await fetch('https://parole.slm-lab.net/api/v1/me', { const userInfoResponse = await fetch(
`https://parole.slm-lab.net/api/v1/users.info?username=${username}`,
{
headers: { headers: {
'Authorization': `Bearer ${rocketChatAuth.access_token}` 'X-Auth-Token': adminAuth.data.authToken,
}, 'X-User-Id': adminAuth.data.userId
cache: 'no-store', }
}); }
);
if (!meResponse.ok) { if (!userInfoResponse.ok) {
console.error('Rocket.Chat me error:', { console.error('Failed to get user info:', {
status: meResponse.status, status: userInfoResponse.status,
statusText: meResponse.statusText, statusText: userInfoResponse.statusText,
response: await meResponse.text().catch(() => 'Could not get response text') response: await userInfoResponse.text().catch(() => 'Could not get response text')
}); });
return NextResponse.json( return NextResponse.json(
{ error: "Failed to get user info from Rocket.Chat" }, { error: "Failed to get user info" },
{ status: meResponse.status } { status: userInfoResponse.status }
); );
} }
const userData = await meResponse.json(); const userInfo = await userInfoResponse.json();
console.log('Rocket.Chat user data:', { console.log('User info success:', {
userId: userData._id, userId: userInfo.user._id,
username: userData.username username: userInfo.user.username
}); });
// Get the user's subscriptions (rooms they are in) // 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', 'https://parole.slm-lab.net/api/v1/subscriptions.get',
{ {
headers: { headers: {
'Authorization': `Bearer ${rocketChatAuth.access_token}` 'X-Auth-Token': adminAuth.data.authToken,
}, 'X-User-Id': userInfo.user._id
cache: 'no-store', }
} }
); );
@ -154,9 +154,9 @@ export async function GET() {
`https://parole.slm-lab.net/api/v1/chat.getMessage?msgId=${room.lastMessage._id}`, `https://parole.slm-lab.net/api/v1/chat.getMessage?msgId=${room.lastMessage._id}`,
{ {
headers: { headers: {
'Authorization': `Bearer ${rocketChatAuth.access_token}` 'X-Auth-Token': adminAuth.data.authToken,
}, 'X-User-Id': userInfo.user._id
cache: 'no-store', }
} }
); );