database wf 10

This commit is contained in:
alma 2025-04-17 13:30:04 +02:00
parent ed6a33b8d7
commit ec5108520b
2 changed files with 105 additions and 79 deletions

View File

@ -1,5 +1,5 @@
import { NextResponse } from 'next/server';
import Imap from 'imap';
import { ImapFlow } from 'imapflow';
import { cookies } from 'next/headers';
interface StoredCredentials {
@ -21,73 +21,69 @@ export async function POST(request: Request) {
}
// Test IMAP connection
const imap = new Imap({
user: email,
password,
const client = new ImapFlow({
host,
port: parseInt(port),
tls: true,
tlsOptions: {
rejectUnauthorized: false,
servername: host
secure: true,
auth: {
user: email,
pass: password,
},
authTimeout: 10000,
connTimeout: 10000,
debug: console.log
logger: false,
emitLogs: false
});
return new Promise((resolve, reject) => {
imap.once('ready', () => {
imap.end();
// Store credentials in cookie
const cookieStore = cookies();
const credentials: StoredCredentials = {
email,
password,
host,
port: parseInt(port)
};
try {
await client.connect();
await client.mailboxOpen('INBOX');
// Store credentials in cookie
const cookieStore = cookies();
const credentials: StoredCredentials = {
email,
password,
host,
port: parseInt(port)
};
// Set the cookie with proper security options
cookieStore.set('imap_credentials', JSON.stringify(credentials), {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
maxAge: 30 * 24 * 60 * 60 // 30 days
});
resolve(NextResponse.json({ success: true }));
// Set the cookie with proper security options
cookieStore.set('imap_credentials', JSON.stringify(credentials), {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
maxAge: 30 * 24 * 60 * 60 // 30 days
});
imap.once('error', (err: Error) => {
imap.end();
if (err.message.includes('Invalid login or password')) {
reject(new Error('Invalid login or password'));
} else {
reject(new Error(`IMAP connection error: ${err.message}`));
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 }
);
}
});
imap.connect();
});
} catch (error) {
console.error('Error in login handler:', error);
if (error instanceof Error) {
if (error.message.includes('Invalid login or password')) {
return NextResponse.json(
{ error: 'Invalid login or password', details: error.message },
{ status: 401 }
{ error: `IMAP connection error: ${error.message}` },
{ status: 500 }
);
}
return NextResponse.json(
{ error: 'Failed to connect to email server', details: error.message },
{ 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: 'Unknown error occurred' },
{ error: 'An unexpected error occurred' },
{ status: 500 }
);
}

View File

@ -9,43 +9,73 @@ export async function GET() {
if (!credentials) {
return NextResponse.json(
{ error: 'No credentials found' },
{ error: 'No credentials found. Please login to your email account first.' },
{ status: 401 }
);
}
const { email, password, host, port } = JSON.parse(credentials.value);
let client;
try {
const { email, password, host, port } = JSON.parse(credentials.value);
const client = new ImapFlow({
host,
port: parseInt(port),
secure: true,
auth: {
user: email,
pass: password,
},
});
await client.connect();
const mailbox = await client.mailboxOpen('INBOX');
const messages = await client.fetch('1:10', { envelope: true });
const result = [];
for await (const message of messages) {
result.push({
id: message.uid,
subject: message.envelope.subject,
from: message.envelope.from[0].address,
date: message.envelope.date,
client = new ImapFlow({
host,
port: parseInt(port),
secure: true,
auth: {
user: email,
pass: password,
},
logger: false,
emitLogs: false
});
}
await client.logout();
return NextResponse.json(result);
await client.connect();
const mailbox = await client.mailboxOpen('INBOX');
const messages = await client.fetch('1:10', { envelope: true });
const result = [];
for await (const message of messages) {
result.push({
id: message.uid,
subject: message.envelope.subject,
from: message.envelope.from[0].address,
date: message.envelope.date,
});
}
return NextResponse.json(result);
} catch (error) {
console.error('Mail API error:', error);
if (error instanceof Error) {
if (error.message.includes('Invalid login')) {
return NextResponse.json(
{ error: 'Invalid email credentials. Please login again.' },
{ status: 401 }
);
}
return NextResponse.json(
{ error: `Failed to fetch emails: ${error.message}` },
{ status: 500 }
);
}
return NextResponse.json(
{ error: 'Failed to fetch emails' },
{ status: 500 }
);
} finally {
if (client) {
try {
await client.logout();
} catch (e) {
console.error('Error during logout:', e);
}
}
}
} catch (error) {
console.error('Mail API error:', error);
console.error('Unexpected error:', error);
return NextResponse.json(
{ error: 'Failed to fetch emails' },
{ error: 'An unexpected error occurred' },
{ status: 500 }
);
}