update widget token mail 6

This commit is contained in:
Alma 2025-04-09 23:27:47 +02:00
parent f68a0a873d
commit daa8de03a4

View File

@ -15,41 +15,35 @@ export async function GET() {
accessToken: session.accessToken ? 'present' : 'missing' accessToken: session.accessToken ? 'present' : 'missing'
}); });
// First, login to Rocket.Chat using OAuth // First, get user info using the OAuth token
const loginResponse = await fetch('https://parole.slm-lab.net/api/v1/login', { const meResponse = await fetch('https://parole.slm-lab.net/api/v1/me', {
method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Authorization': `Bearer ${session.accessToken}`
}, },
body: JSON.stringify({ cache: 'no-store',
serviceName: 'keycloak',
accessToken: session.accessToken,
expiresIn: 200
})
}); });
if (!loginResponse.ok) { if (!meResponse.ok) {
console.error('Rocket.Chat login error:', { console.error('Rocket.Chat me error:', {
status: loginResponse.status, status: meResponse.status,
statusText: loginResponse.statusText, statusText: meResponse.statusText,
response: await loginResponse.text().catch(() => 'Could not get response text') response: await meResponse.text().catch(() => 'Could not get response text')
}); });
return NextResponse.json( return NextResponse.json(
{ error: "Failed to authenticate with Rocket.Chat" }, { error: "Failed to get user info from Rocket.Chat" },
{ status: loginResponse.status } { status: meResponse.status }
); );
} }
const loginData = await loginResponse.json(); const userData = await meResponse.json();
const { authToken, userId } = loginData.data; const { _id: userId } = userData;
// Get the user's subscriptions (rooms they are in) // Get the user's subscriptions (rooms they are in)
const subscriptionsResponse = await fetch( const subscriptionsResponse = await fetch(
'https://parole.slm-lab.net/api/v1/subscriptions.get', 'https://parole.slm-lab.net/api/v1/subscriptions.get',
{ {
headers: { headers: {
'X-Auth-Token': authToken, 'Authorization': `Bearer ${session.accessToken}`
'X-User-Id': userId,
}, },
cache: 'no-store', cache: 'no-store',
} }
@ -81,8 +75,7 @@ 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: {
'X-Auth-Token': authToken, 'Authorization': `Bearer ${session.accessToken}`
'X-User-Id': userId,
}, },
cache: 'no-store', cache: 'no-store',
} }