114 lines
3.3 KiB
TypeScript
114 lines
3.3 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
|
import { saveUserEmailCredentials } from '@/lib/services/email-service';
|
|
|
|
// Define EmailCredentials interface inline since we're having import issues
|
|
interface EmailCredentials {
|
|
email: string;
|
|
password?: string;
|
|
host: string;
|
|
port: number;
|
|
secure?: boolean;
|
|
smtp_host?: string;
|
|
smtp_port?: number;
|
|
smtp_secure?: boolean;
|
|
display_name?: string;
|
|
color?: string;
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
// Authenticate user
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Parse request body
|
|
const body = await request.json().catch(e => {
|
|
console.error('Error parsing request body:', e);
|
|
return {};
|
|
});
|
|
|
|
// Log the request (but hide password)
|
|
console.log('Adding account:', {
|
|
...body,
|
|
password: body.password ? '***' : undefined
|
|
});
|
|
|
|
const {
|
|
email,
|
|
password,
|
|
host,
|
|
port,
|
|
secure,
|
|
smtp_host,
|
|
smtp_port,
|
|
smtp_secure,
|
|
display_name,
|
|
color
|
|
} = body;
|
|
|
|
// Validate required fields
|
|
const missingFields = [];
|
|
if (!email) missingFields.push('email');
|
|
if (!password) missingFields.push('password');
|
|
if (!host) missingFields.push('host');
|
|
if (port === undefined) missingFields.push('port');
|
|
|
|
if (missingFields.length > 0) {
|
|
console.error(`Missing required fields: ${missingFields.join(', ')}`);
|
|
return NextResponse.json(
|
|
{ error: `Required fields missing: ${missingFields.join(', ')}` },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Fix common hostname errors - strip http/https prefixes
|
|
let cleanHost = host;
|
|
if (cleanHost.startsWith('http://')) {
|
|
cleanHost = cleanHost.substring(7);
|
|
} else if (cleanHost.startsWith('https://')) {
|
|
cleanHost = cleanHost.substring(8);
|
|
}
|
|
|
|
// Create credentials object
|
|
const credentials: EmailCredentials = {
|
|
email,
|
|
password,
|
|
host: cleanHost,
|
|
port: typeof port === 'string' ? parseInt(port) : port,
|
|
secure: secure ?? true,
|
|
// Optional SMTP settings
|
|
...(smtp_host && { smtp_host }),
|
|
...(smtp_port && { smtp_port: typeof smtp_port === 'string' ? parseInt(smtp_port) : smtp_port }),
|
|
...(smtp_secure !== undefined && { smtp_secure }),
|
|
// Optional display settings
|
|
...(display_name && { display_name }),
|
|
...(color && { color })
|
|
};
|
|
|
|
// Connection test is no longer needed here since we validate with the test-connection endpoint first
|
|
// Save credentials to database and cache
|
|
await saveUserEmailCredentials(session.user.id, credentials);
|
|
console.log(`Email account successfully added for user ${session.user.id}`);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: 'Email account added successfully'
|
|
});
|
|
} catch (error) {
|
|
console.error('Error adding email account:', error);
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Failed to add email account',
|
|
details: error instanceof Error ? error.message : 'Unknown error'
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|