panel 2 courier api restore

This commit is contained in:
alma 2025-04-25 21:03:09 +02:00
parent 33c6fe709a
commit c12a1b2891
2 changed files with 61 additions and 51 deletions

View File

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

View File

@ -1959,17 +1959,27 @@ export default function CourrierPage() {
// Add a new function to fetch the IMAP credentials email // Add a new function to fetch the IMAP credentials email
const fetchImapCredentials = async () => { const fetchImapCredentials = async () => {
try { 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) { if (!response.ok) {
console.error('Failed to fetch IMAP credentials', response.status); const errorData = await response.json().catch(() => ({}));
return; console.error("Error fetching IMAP credentials:", response.status, errorData);
throw new Error(`Failed to fetch IMAP credentials: ${response.status}`);
} }
const data = await response.json(); const data = await response.json();
console.log("IMAP credentials data:", data);
// If we have credentials with an email, update the account
if (data && data.credentials && data.credentials.email) { 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 => setAccounts(prev => prev.map(account =>
account.id === 1 account.id === 1
? { ? {
@ -1980,10 +1990,10 @@ export default function CourrierPage() {
: account : account
)); ));
} else { } else {
console.warn('No IMAP email found in credentials'); console.log("No valid IMAP credentials found in response:", data);
} }
} catch (error) { } catch (error) {
console.error('Error fetching IMAP credentials:', error); console.error("Error in fetchImapCredentials:", error);
} }
}; };