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

47 lines
1.4 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 });
}
// Get the access token from the session
const accessToken = session.accessToken;
if (!accessToken) {
return NextResponse.json({ error: "No access token found" }, { status: 401 });
}
// Use the Keycloak token to authenticate with Rocket.Chat
const response = await fetch('https://parole.slm-lab.net/api/v1/channels.messages', {
headers: {
'Authorization': `Bearer ${accessToken}`,
},
// 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 }
);
}
}