NeahFront3/app/api/rocket-chat/messages/route.ts
2025-04-09 22:56:29 +02:00

42 lines
1.3 KiB
TypeScript

import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { NextResponse } from "next/server";
export async function GET() {
try {
const session = await getServerSession(authOptions);
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
// Use the personal access token to authenticate with Rocket.Chat
const response = await fetch('https://parole.slm-lab.net/api/v1/channels.messages', {
headers: {
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
'X-User-Id': 'Tpuww59PJKsrGNQJB',
},
// Add cache control to prevent stale data
cache: 'no-store',
});
if (!response.ok) {
console.error('Rocket.Chat API error:', {
status: response.status,
statusText: response.statusText,
});
return NextResponse.json(
{ error: "Failed to fetch messages" },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Error fetching messages:', error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}