mail page imap connection mime 5 bis rest 16 login page 20

This commit is contained in:
alma 2025-04-16 00:43:35 +02:00
parent 2fdebf82e1
commit c025203267
2 changed files with 54 additions and 34 deletions

View File

@ -91,14 +91,21 @@ function getStoredCredentials(): StoredCredentials | null {
export async function GET() {
try {
console.log('GET /api/mail called');
const credentials = getStoredCredentials();
if (!credentials) {
console.log('No credentials found in cookies');
return NextResponse.json(
{ error: 'No stored credentials found' },
{ status: 401 }
);
}
console.log('Using credentials:', {
...credentials,
password: '***'
});
const imapConfig: ImapConfig = {
user: credentials.email,
password: credentials.password,
@ -110,29 +117,38 @@ export async function GET() {
debug: (info: string) => console.log('IMAP Debug:', info)
};
console.log('IMAP Config:', {
...imapConfig,
password: '***'
});
console.log('Connecting to IMAP server...');
const imap = new Imap(imapConfig);
const connectPromise = new Promise((resolve, reject) => {
imap.once('ready', () => resolve(true));
imap.once('error', (err: Error) => reject(err));
imap.once('ready', () => {
console.log('IMAP connection ready');
resolve(true);
});
imap.once('error', (err: Error) => {
console.error('IMAP connection error:', err);
reject(err);
});
imap.connect();
});
await connectPromise;
console.log('Opening INBOX...');
const openBoxPromise = new Promise<ImapBox>((resolve, reject) => {
imap.openBox('INBOX', false, (err: Error | null, box: ImapBox) => {
if (err) reject(err);
else resolve(box);
if (err) {
console.error('Error opening INBOX:', err);
reject(err);
} else {
console.log('INBOX opened successfully');
resolve(box);
}
});
});
const box = await openBoxPromise;
console.log('Total messages in INBOX:', box.messages.total);
const fetchPromise = (imap: Imap, seqno: number): Promise<ImapMessage> => {
return new Promise((resolve, reject) => {

View File

@ -401,33 +401,33 @@ export default function MailPage() {
// Check for stored credentials
useEffect(() => {
const storedCredentials = localStorage.getItem('imapCredentials');
if (!storedCredentials) {
router.push('/mail/login');
} else {
setLoading(false);
}
const checkCredentials = async () => {
try {
console.log('Checking for stored credentials...');
const response = await fetch('/api/mail');
if (!response.ok) {
const errorData = await response.json();
console.log('API response error:', errorData);
if (errorData.error === 'No stored credentials found') {
console.log('No credentials found, redirecting to login...');
router.push('/mail/login');
return;
}
throw new Error(errorData.error || 'Failed to check credentials');
}
console.log('Credentials verified, loading emails...');
setLoading(false);
loadEmails();
} catch (err) {
console.error('Error checking credentials:', err);
setError(err instanceof Error ? err.message : 'Failed to check credentials');
setLoading(false);
}
};
checkCredentials();
}, [router]);
// Load emails on mount and when selectedAccount changes
useEffect(() => {
if (!loading) {
loadEmails();
}
}, [loading, selectedAccount]);
// Show loading state if credentials are not loaded
if (loading) {
return (
<div className="flex min-h-screen items-center justify-center">
<div className="text-center">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-gray-900 mx-auto"></div>
<p className="mt-4 text-gray-600">Loading...</p>
</div>
</div>
);
}
// Fetch emails from IMAP API
const loadEmails = async () => {
try {
@ -441,6 +441,10 @@ export default function MailPage() {
if (!response.ok) {
const errorData = await response.json();
console.error('API error:', errorData);
if (errorData.error === 'No stored credentials found') {
router.push('/mail/login');
return;
}
throw new Error(errorData.error || 'Failed to load emails');
}