From 9f82be44cc4328905ac1262f8064582864eab5c2 Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 27 Apr 2025 11:29:18 +0200 Subject: [PATCH] courrier refactor rebuild 2 --- app/api/user/email/route.ts | 38 +++++++++++++++++++++++++++++++++++++ app/courrier/page.tsx | 33 ++++++++++++++++++++++++++++---- 2 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 app/api/user/email/route.ts diff --git a/app/api/user/email/route.ts b/app/api/user/email/route.ts new file mode 100644 index 00000000..5b114a1b --- /dev/null +++ b/app/api/user/email/route.ts @@ -0,0 +1,38 @@ +import { NextResponse } from 'next/server'; +import { getServerSession } from 'next-auth'; +import { authOptions } from '@/app/api/auth/[...nextauth]/route'; +import { getUserEmailCredentials } from '@/lib/services/email-service'; + +export async function GET() { + try { + // Authenticate user + const session = await getServerSession(authOptions); + if (!session || !session.user?.id) { + return NextResponse.json( + { error: "Not authenticated" }, + { status: 401 } + ); + } + + // Get user's email credentials + const credentials = await getUserEmailCredentials(session.user.id); + + if (!credentials) { + return NextResponse.json( + { error: "No email credentials found" }, + { status: 404 } + ); + } + + // Return the email address + return NextResponse.json({ + email: credentials.email + }); + } catch (error: any) { + console.error("Error fetching user email:", error); + return NextResponse.json( + { error: "Failed to fetch user email", message: error.message }, + { status: 500 } + ); + } +} \ No newline at end of file diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 0dd141cb..5612c60b 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -101,24 +101,49 @@ export default function CourrierPage() { const [currentView, setCurrentView] = useState('INBOX'); const [unreadCount, setUnreadCount] = useState(0); const [loading, setLoading] = useState(false); + const [userEmail, setUserEmail] = useState(''); - // Mock accounts for the sidebar + // Get user's email from session data + useEffect(() => { + const getUserEmail = async () => { + try { + // Try to get user email from API + const response = await fetch('/api/user/email'); + if (response.ok) { + const data = await response.json(); + if (data.email) { + setUserEmail(data.email); + } + } + } catch (error) { + console.error('Error fetching user email:', error); + } + }; + + getUserEmail(); + }, []); + + // Accounts for the sidebar (using the actual user email) const [accounts, setAccounts] = useState([ { id: 0, name: 'All', email: '', color: 'bg-gray-500' }, - { id: 1, name: 'Mail', email: 'user@example.com', color: 'bg-blue-500', folders: mailboxes } + { id: 1, name: userEmail || 'Loading...', email: userEmail || '', color: 'bg-blue-500', folders: mailboxes } ]); const [selectedAccount, setSelectedAccount] = useState(null); - // Update account folders when mailboxes change + // Update account folders and email when mailboxes or user email changes useEffect(() => { setAccounts(prev => { const updated = [...prev]; if (updated[1]) { updated[1].folders = mailboxes; + if (userEmail) { + updated[1].name = userEmail; + updated[1].email = userEmail; + } } return updated; }); - }, [mailboxes]); + }, [mailboxes, userEmail]); // Calculate unread count (this would be replaced with actual data in production) useEffect(() => {