Neah version calendar fix 3 debuger sec chance danger debug 4

This commit is contained in:
alma 2025-04-17 00:34:13 +02:00
parent bef5ed1946
commit 16219fc250

View File

@ -2,18 +2,10 @@ import { NextResponse } from 'next/server';
import Imap from 'imap'; import Imap from 'imap';
import nodemailer from 'nodemailer'; import nodemailer from 'nodemailer';
import { parseEmailHeaders, decodeEmailBody } from '@/lib/email-parser'; import { parseEmailHeaders, decodeEmailBody } from '@/lib/email-parser';
import { cookies } from 'next/headers';
import { getServerSession } from 'next-auth/next'; import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/lib/auth'; import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma'; import { prisma } from '@/lib/prisma';
interface StoredCredentials {
email: string;
password: string;
host: string;
port: number;
}
interface Email { interface Email {
id: string; id: string;
from: string; from: string;
@ -58,42 +50,6 @@ interface ImapConfig {
debug?: (info: string) => void; debug?: (info: string) => void;
} }
function getStoredCredentials(): StoredCredentials | null {
const cookieStore = cookies();
const credentialsCookie = cookieStore.get('imap_credentials');
console.log('Retrieved credentials cookie:', credentialsCookie ? 'Found' : 'Not found');
if (!credentialsCookie?.value) {
console.log('No credentials cookie found');
return null;
}
try {
const credentials = JSON.parse(credentialsCookie.value);
console.log('Parsed credentials:', {
...credentials,
password: '***'
});
// Validate required fields
if (!credentials.email || !credentials.password || !credentials.host || !credentials.port) {
console.error('Missing required credentials fields');
return null;
}
return {
email: credentials.email,
password: credentials.password,
host: credentials.host,
port: credentials.port
};
} catch (error) {
console.error('Error parsing credentials cookie:', error);
return null;
}
}
export async function GET(request: Request) { export async function GET(request: Request) {
const session = await getServerSession(authOptions); const session = await getServerSession(authOptions);
@ -108,7 +64,7 @@ export async function GET(request: Request) {
if (!userCredentials) { if (!userCredentials) {
return NextResponse.json( return NextResponse.json(
{ error: 'No email credentials configured' }, { error: 'No email credentials configured. Please set up your email account first.' },
{ status: 401 } { status: 401 }
); );
} }
@ -307,28 +263,27 @@ export async function GET(request: Request) {
} }
export async function POST(request: Request) { export async function POST(request: Request) {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
}
try { try {
const credentials = getStoredCredentials(); const userCredentials = await prisma.emailCredentials.findUnique({
if (!credentials) { where: { userId: session.user.id }
});
if (!userCredentials) {
return NextResponse.json( return NextResponse.json(
{ error: 'No stored credentials found' }, { error: 'No email credentials configured. Please set up your email account first.' },
{ status: 401 } { status: 401 }
); );
} }
let body; const { to, subject, body, attachments } = await request.json();
try {
body = await request.json();
} catch (error) {
return NextResponse.json(
{ error: 'Invalid JSON in request body' },
{ status: 400 }
);
}
const { to, subject, body: emailBody, attachments } = body; if (!to || !subject || !body) {
if (!to || !subject || !emailBody) {
return NextResponse.json( return NextResponse.json(
{ error: 'Missing required fields: to, subject, or body' }, { error: 'Missing required fields: to, subject, or body' },
{ status: 400 } { status: 400 }
@ -336,20 +291,20 @@ export async function POST(request: Request) {
} }
const transporter = nodemailer.createTransport({ const transporter = nodemailer.createTransport({
host: credentials.host, host: userCredentials.host,
port: credentials.port, port: userCredentials.port,
secure: true, secure: true,
auth: { auth: {
user: credentials.email, user: userCredentials.email,
pass: credentials.password, pass: userCredentials.password,
}, },
}); });
const mailOptions = { const mailOptions = {
from: credentials.email, from: userCredentials.email,
to, to,
subject, subject,
text: emailBody, html: body,
attachments: attachments || [], attachments: attachments || [],
}; };