Neah/app/api/courrier/credentials/route.ts
2025-04-26 09:14:44 +02:00

54 lines
1.7 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { getUserEmailCredentials } from '@/lib/services/email-service';
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 using our service
const mailCredentials = await getUserEmailCredentials(userId);
// 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 (excluding password)
console.log(`Successfully retrieved mail credentials for user ID: ${userId}`);
return NextResponse.json({
credentials: {
email: mailCredentials.email,
host: mailCredentials.host,
port: mailCredentials.port
}
});
} catch (error) {
console.error("Error retrieving mail credentials:", error);
return NextResponse.json({
error: "Internal Server Error"
}, { status: 500 });
}
}