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 { 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 });
}
}

View File

@ -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);
}
};