diff --git a/app/api/courrier/account/route.ts b/app/api/courrier/account/route.ts new file mode 100644 index 00000000..49ccce1c --- /dev/null +++ b/app/api/courrier/account/route.ts @@ -0,0 +1,115 @@ +import { NextResponse } from 'next/server'; +import { getServerSession } from 'next-auth'; +import { authOptions } from '@/app/api/auth/[...nextauth]/route'; +import { saveUserEmailCredentials, testEmailConnection } 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(); + const { + email, + password, + host, + port, + secure, + smtp_host, + smtp_port, + smtp_secure, + display_name, + color + } = body; + + // Validate required fields + if (!email || !password || !host || !port) { + return NextResponse.json( + { error: 'Required fields missing: email, password, host, port' }, + { status: 400 } + ); + } + + // Create credentials object + const credentials: EmailCredentials = { + email, + password, + host, + 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 }) + }; + + // Test connection before saving + const connectionTest = await testEmailConnection(credentials); + + if (!connectionTest.imap) { + return NextResponse.json( + { + error: 'Failed to connect to IMAP server with provided credentials', + details: connectionTest.error + }, + { status: 400 } + ); + } + + // If SMTP details provided but connection failed + if (smtp_host && smtp_port && !connectionTest.smtp) { + return NextResponse.json( + { + error: 'IMAP connection successful, but SMTP connection failed', + details: connectionTest.error + }, + { status: 400 } + ); + } + + // Save credentials to database and cache + await saveUserEmailCredentials(session.user.id, credentials); + + return NextResponse.json({ + success: true, + message: 'Email account added successfully', + connectionStatus: { + imap: connectionTest.imap, + smtp: connectionTest.smtp + } + }); + } 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 } + ); + } +} \ No newline at end of file diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index cf92347f..f81064b8 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -27,6 +27,10 @@ import { ScrollArea } from '@/components/ui/scroll-area'; import { Avatar, AvatarFallback } from '@/components/ui/avatar'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; +import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'; +import { Checkbox } from '@/components/ui/checkbox'; +import { Label } from '@/components/ui/label'; +import { toast } from '@/components/ui/use-toast'; // Import components import EmailSidebar from '@/components/email/EmailSidebar'; @@ -449,38 +453,141 @@ export default function CourrierPage() { {showAddAccountForm && (