From c12a1b2891b8a3e7f9e0c7df881f9a68c7612e43 Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 25 Apr 2025 21:03:09 +0200 Subject: [PATCH] panel 2 courier api restore --- app/api/courrier/credentials/route.ts | 84 +++++++++++++-------------- app/courrier/page.tsx | 28 ++++++--- 2 files changed, 61 insertions(+), 51 deletions(-) diff --git a/app/api/courrier/credentials/route.ts b/app/api/courrier/credentials/route.ts index 7652b635..5b1e0220 100644 --- a/app/api/courrier/credentials/route.ts +++ b/app/api/courrier/credentials/route.ts @@ -1,59 +1,59 @@ import { NextResponse } from 'next/server'; -import { getServerSession } from 'next-auth/next'; +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); - if (!session || !session.user?.id) { - console.log("No authenticated session found"); - return NextResponse.json( - { error: "Not authenticated" }, - { status: 401 } - ); + + // Check if user is authenticated + if (!session || !session.user) { + console.error("Unauthorized access to mail credentials"); + return NextResponse.json({ + error: "Unauthorized" + }, { status: 401 }); } - console.log(`Attempting to fetch mail credentials for user ${session.user.id}`); + 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 }); + } - // Get mail credentials - const credentials = await prisma.mailCredentials.findUnique({ + // Fetch mail credentials for this user + const mailCredentials = await prisma.mailCredentials.findUnique({ where: { - userId: session.user.id, + userId: userId }, - }); - - 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 + select: { + email: true, + host: true, + port: true } }); - } 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); + + // 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 NextResponse.json( - { error: "Failed to fetch mail credentials" }, - { status: 500 } - ); + + // 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 }); } } \ No newline at end of file diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index e2671363..3533cb3c 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -1959,17 +1959,27 @@ export default function CourrierPage() { // Add a new function to fetch the IMAP credentials email const fetchImapCredentials = async () => { try { - const response = await fetch('/api/courrier/credentials'); + console.log("Fetching IMAP credentials..."); + const response = await fetch("/api/courrier/credentials", { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); + + console.log("IMAP credentials response status:", response.status); + if (!response.ok) { - console.error('Failed to fetch IMAP credentials', response.status); - return; + const errorData = await response.json().catch(() => ({})); + console.error("Error fetching IMAP credentials:", response.status, errorData); + throw new Error(`Failed to fetch IMAP credentials: ${response.status}`); } - + const data = await response.json(); - - // If we have credentials with an email, update the account + console.log("IMAP credentials data:", data); + if (data && data.credentials && data.credentials.email) { - console.log('Got IMAP credentials email:', data.credentials.email); + console.log("Setting account with IMAP email:", data.credentials.email); setAccounts(prev => prev.map(account => account.id === 1 ? { @@ -1980,10 +1990,10 @@ export default function CourrierPage() { : account )); } else { - console.warn('No IMAP email found in credentials'); + console.log("No valid IMAP credentials found in response:", data); } } catch (error) { - console.error('Error fetching IMAP credentials:', error); + console.error("Error in fetchImapCredentials:", error); } };