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