59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth/next';
|
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
|
import { prisma } from '@/lib/prisma';
|
|
|
|
export async function GET() {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session || !session.user?.id) {
|
|
console.log("No authenticated session found");
|
|
return NextResponse.json(
|
|
{ error: "Not authenticated" },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
console.log(`Attempting to fetch mail credentials for user ${session.user.id}`);
|
|
|
|
// Get mail credentials
|
|
const credentials = await prisma.mailCredentials.findUnique({
|
|
where: {
|
|
userId: session.user.id,
|
|
},
|
|
});
|
|
|
|
if (!credentials) {
|
|
console.log(`No mail credentials found for user ${session.user.id}`);
|
|
return NextResponse.json(
|
|
{ error: "No mail credentials found" },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
console.log(`Found credentials for email: ${credentials.email}`);
|
|
|
|
// Return only necessary credential details
|
|
return NextResponse.json({
|
|
credentials: {
|
|
email: credentials.email,
|
|
host: credentials.host,
|
|
port: credentials.port
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching mail credentials:", error);
|
|
|
|
// Log more detailed error information
|
|
if (error instanceof Error) {
|
|
console.error("Error name:", error.name);
|
|
console.error("Error message:", error.message);
|
|
console.error("Error stack:", error.stack);
|
|
}
|
|
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch mail credentials" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|