import { NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { prisma } from '@/lib/prisma'; export async function GET() { try { // Get server session to verify authentication const session = await getServerSession(authOptions); // Check if user is authenticated if (!session || !session.user) { console.error("Unauthorized access to mail credentials"); return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const userId = session.user.id; if (!userId) { console.error("User ID not found in session"); return NextResponse.json({ error: "User ID not found" }, { status: 401 }); } // Fetch mail credentials for this user const mailCredentials = await prisma.mailCredentials.findUnique({ where: { userId: userId }, select: { email: true, host: true, port: true } }); // If no credentials found if (!mailCredentials) { console.warn(`No mail credentials found for user ID: ${userId}`); return NextResponse.json({ error: "Mail credentials not found" }, { status: 404 }); } // Return the credentials console.log(`Successfully retrieved mail credentials for user ID: ${userId}`); return NextResponse.json({ credentials: mailCredentials }); } catch (error) { console.error("Error retrieving mail credentials:", error); return NextResponse.json({ error: "Internal Server Error" }, { status: 500 }); } }