widget parole 3
This commit is contained in:
parent
a29f81286e
commit
f3953b673b
@ -45,26 +45,6 @@ export async function GET(request: Request) {
|
||||
});
|
||||
}
|
||||
|
||||
console.log('Session data:', {
|
||||
hasToken: !!session.rocketChatToken,
|
||||
hasUserId: !!session.rocketChatUserId,
|
||||
tokenLength: session.rocketChatToken?.length,
|
||||
userIdLength: session.rocketChatUserId?.length,
|
||||
username: session.user?.username
|
||||
});
|
||||
|
||||
if (!session.rocketChatToken || !session.rocketChatUserId) {
|
||||
console.error('Missing Rocket.Chat credentials in session:', {
|
||||
hasToken: !!session.rocketChatToken,
|
||||
hasUserId: !!session.rocketChatUserId,
|
||||
username: session.user?.username
|
||||
});
|
||||
return new Response(JSON.stringify({ error: 'Missing Rocket.Chat credentials' }), {
|
||||
status: 401,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const baseUrl = process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL?.split('/channel')[0];
|
||||
if (!baseUrl) {
|
||||
@ -75,21 +55,16 @@ export async function GET(request: Request) {
|
||||
});
|
||||
}
|
||||
|
||||
// Step 1 & 2: Get user token using admin credentials
|
||||
const userToken = await getUserToken(baseUrl);
|
||||
if (!userToken) {
|
||||
return new Response(JSON.stringify({ error: 'Failed to create user token' }), {
|
||||
status: 401,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
// Use admin token to get messages
|
||||
const adminHeaders = {
|
||||
'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!,
|
||||
'X-User-Id': process.env.ROCKET_CHAT_USER_ID!,
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
// Step 3: Use the user token to fetch subscriptions
|
||||
// Get user's subscriptions using admin token
|
||||
const subscriptionsResponse = await fetch(`${baseUrl}/api/v1/subscriptions.get`, {
|
||||
headers: {
|
||||
'X-Auth-Token': userToken.authToken,
|
||||
'X-User-Id': userToken.userId,
|
||||
},
|
||||
headers: adminHeaders
|
||||
});
|
||||
|
||||
if (!subscriptionsResponse.ok) {
|
||||
@ -105,7 +80,7 @@ export async function GET(request: Request) {
|
||||
const messages: any[] = [];
|
||||
const processedRooms = new Set();
|
||||
|
||||
// Step 3: Use the same user token to fetch messages
|
||||
// Fetch messages using admin token
|
||||
for (const subscription of userSubscriptions) {
|
||||
if (messages.length >= 6 || processedRooms.has(subscription.rid)) continue;
|
||||
processedRooms.add(subscription.rid);
|
||||
@ -114,11 +89,7 @@ export async function GET(request: Request) {
|
||||
const messagesResponse = await fetch(
|
||||
`${baseUrl}/api/v1/chat.getMessage`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Auth-Token': userToken.authToken,
|
||||
'X-User-Id': userToken.userId,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
headers: adminHeaders,
|
||||
body: JSON.stringify({
|
||||
msgId: subscription.lastMessage?._id,
|
||||
roomId: subscription.rid,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user