237 lines
6.2 KiB
TypeScript
237 lines
6.2 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { ImapFlow } from 'imapflow';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
|
import { prisma } from '@/lib/prisma';
|
|
import { LRUCache } from 'lru-cache';
|
|
|
|
// Simple in-memory cache for email content
|
|
const emailContentCache = new LRUCache<string, any>({
|
|
max: 100,
|
|
ttl: 1000 * 60 * 15, // 15 minutes
|
|
});
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Verify user exists
|
|
const user = await prisma.user.findUnique({
|
|
where: { id: session.user.id }
|
|
});
|
|
|
|
if (!user) {
|
|
return NextResponse.json(
|
|
{ error: 'User not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const { email, password, host, port } = await request.json();
|
|
|
|
if (!email || !password || !host || !port) {
|
|
return NextResponse.json(
|
|
{ error: 'Missing required fields' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Test IMAP connection
|
|
const client = new ImapFlow({
|
|
host: host,
|
|
port: parseInt(port),
|
|
secure: true,
|
|
auth: {
|
|
user: email,
|
|
pass: password,
|
|
},
|
|
logger: false,
|
|
emitLogs: false,
|
|
tls: {
|
|
rejectUnauthorized: false // Allow self-signed certificates
|
|
}
|
|
});
|
|
|
|
try {
|
|
await client.connect();
|
|
await client.mailboxOpen('INBOX');
|
|
|
|
// Store or update credentials in database
|
|
await prisma.mailCredentials.upsert({
|
|
where: {
|
|
userId: session.user.id
|
|
},
|
|
update: {
|
|
email,
|
|
password,
|
|
host,
|
|
port: parseInt(port)
|
|
},
|
|
create: {
|
|
userId: session.user.id,
|
|
email,
|
|
password,
|
|
host,
|
|
port: parseInt(port)
|
|
}
|
|
});
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
if (error.message.includes('Invalid login')) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid login or password' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
return NextResponse.json(
|
|
{ error: `IMAP connection error: ${error.message}` },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
return NextResponse.json(
|
|
{ error: 'Failed to connect to email server' },
|
|
{ status: 500 }
|
|
);
|
|
} finally {
|
|
try {
|
|
await client.logout();
|
|
} catch (e) {
|
|
console.error('Error during logout:', e);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error in login handler:', error);
|
|
return NextResponse.json(
|
|
{ error: 'An unexpected error occurred' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function GET(
|
|
request: Request,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
// 1. Properly await params to avoid Next.js error
|
|
const { id } = await Promise.resolve(params);
|
|
|
|
// 2. Authentication check
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// 3. Check cache first
|
|
const cacheKey = `email:${session.user.id}:${id}`;
|
|
const cachedEmail = emailContentCache.get(cacheKey);
|
|
if (cachedEmail) {
|
|
return NextResponse.json(cachedEmail);
|
|
}
|
|
|
|
// 4. Get credentials from database
|
|
const credentials = await prisma.mailCredentials.findUnique({
|
|
where: {
|
|
userId: session.user.id
|
|
}
|
|
});
|
|
|
|
if (!credentials) {
|
|
return NextResponse.json(
|
|
{ error: 'No mail credentials found. Please configure your email account.' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// 5. Create IMAP client
|
|
const client = new ImapFlow({
|
|
host: credentials.host,
|
|
port: credentials.port,
|
|
secure: true,
|
|
auth: {
|
|
user: credentials.email,
|
|
pass: credentials.password,
|
|
},
|
|
logger: false,
|
|
emitLogs: false,
|
|
tls: {
|
|
rejectUnauthorized: false
|
|
},
|
|
disableAutoIdle: true
|
|
});
|
|
|
|
try {
|
|
await client.connect();
|
|
|
|
// 6. Open INBOX
|
|
await client.mailboxOpen('INBOX');
|
|
|
|
// 7. Fetch the email with UID search
|
|
const options = {
|
|
uid: true, // This is crucial - we must specify uid:true to fetch by UID
|
|
source: true,
|
|
envelope: true,
|
|
bodyStructure: true,
|
|
flags: true
|
|
};
|
|
|
|
// Fetch by UID
|
|
console.log('Fetching email with UID:', id);
|
|
const message = await client.fetchOne(id, options);
|
|
|
|
if (!message) {
|
|
console.error('Email not found with UID:', id);
|
|
return NextResponse.json(
|
|
{ error: 'Email not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// 8. Parse the email content
|
|
const emailContent = {
|
|
id: message.uid.toString(),
|
|
from: message.envelope.from?.[0]?.address || '',
|
|
fromName: message.envelope.from?.[0]?.name ||
|
|
message.envelope.from?.[0]?.address?.split('@')[0] || '',
|
|
to: message.envelope.to?.map((addr: any) => addr.address).join(', ') || '',
|
|
subject: message.envelope.subject || '(No subject)',
|
|
date: message.envelope.date?.toISOString() || new Date().toISOString(),
|
|
content: message.source?.toString() || '',
|
|
read: message.flags.has('\\Seen'),
|
|
starred: message.flags.has('\\Flagged'),
|
|
flags: Array.from(message.flags),
|
|
hasAttachments: message.bodyStructure?.type === 'multipart'
|
|
};
|
|
|
|
// 9. Cache the email content
|
|
emailContentCache.set(cacheKey, emailContent);
|
|
|
|
// 10. Return the email content
|
|
return NextResponse.json(emailContent);
|
|
} finally {
|
|
// 11. Close the connection
|
|
try {
|
|
await client.logout();
|
|
} catch (e) {
|
|
console.error('Error during IMAP logout:', e);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching email:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch email content' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|