diff --git a/app/api/mail/route.ts b/app/api/mail/route.ts index 2902643..44f28f7 100644 --- a/app/api/mail/route.ts +++ b/app/api/mail/route.ts @@ -2,18 +2,10 @@ import { NextResponse } from 'next/server'; import Imap from 'imap'; import nodemailer from 'nodemailer'; import { parseEmailHeaders, decodeEmailBody } from '@/lib/email-parser'; -import { cookies } from 'next/headers'; import { getServerSession } from 'next-auth/next'; import { authOptions } from '@/lib/auth'; import { prisma } from '@/lib/prisma'; -interface StoredCredentials { - email: string; - password: string; - host: string; - port: number; -} - interface Email { id: string; from: string; @@ -58,42 +50,6 @@ interface ImapConfig { 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) { const session = await getServerSession(authOptions); @@ -108,7 +64,7 @@ export async function GET(request: Request) { if (!userCredentials) { return NextResponse.json( - { error: 'No email credentials configured' }, + { error: 'No email credentials configured. Please set up your email account first.' }, { status: 401 } ); } @@ -307,28 +263,27 @@ export async function GET(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 { - const credentials = getStoredCredentials(); - if (!credentials) { + const userCredentials = await prisma.emailCredentials.findUnique({ + where: { userId: session.user.id } + }); + + if (!userCredentials) { return NextResponse.json( - { error: 'No stored credentials found' }, + { error: 'No email credentials configured. Please set up your email account first.' }, { status: 401 } ); } - let body; - try { - body = await request.json(); - } catch (error) { - return NextResponse.json( - { error: 'Invalid JSON in request body' }, - { status: 400 } - ); - } + const { to, subject, body, attachments } = await request.json(); - const { to, subject, body: emailBody, attachments } = body; - - if (!to || !subject || !emailBody) { + if (!to || !subject || !body) { return NextResponse.json( { error: 'Missing required fields: to, subject, or body' }, { status: 400 } @@ -336,20 +291,20 @@ export async function POST(request: Request) { } const transporter = nodemailer.createTransport({ - host: credentials.host, - port: credentials.port, + host: userCredentials.host, + port: userCredentials.port, secure: true, auth: { - user: credentials.email, - pass: credentials.password, + user: userCredentials.email, + pass: userCredentials.password, }, }); const mailOptions = { - from: credentials.email, + from: userCredentials.email, to, subject, - text: emailBody, + html: body, attachments: attachments || [], };