154 lines
3.7 KiB
TypeScript
154 lines
3.7 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';
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
console.log('Session in mail login:', session);
|
|
|
|
if (!session?.user?.id) {
|
|
console.error('No user ID in session');
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Verify user exists
|
|
console.log('Checking for user with ID:', session.user.id);
|
|
const user = await prisma.user.findUnique({
|
|
where: { id: session.user.id }
|
|
});
|
|
|
|
console.log('User found in database:', user);
|
|
|
|
if (!user) {
|
|
console.error('User not found in database');
|
|
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() {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const credentials = await prisma.mailCredentials.findUnique({
|
|
where: {
|
|
userId: session.user.id
|
|
},
|
|
select: {
|
|
email: true,
|
|
host: true,
|
|
port: true
|
|
}
|
|
});
|
|
|
|
if (!credentials) {
|
|
return NextResponse.json(
|
|
{ error: 'No stored credentials found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json(credentials);
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{ error: 'Failed to retrieve credentials' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|