51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from "@/app/api/auth/options";
|
|
import { prisma } from '@/lib/prisma';
|
|
|
|
export async function GET(request: Request) {
|
|
try {
|
|
// Authenticate user
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Get all email accounts for this user
|
|
const accounts = await prisma.mailCredentials.findMany({
|
|
where: { userId: session.user.id },
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
host: true,
|
|
port: true,
|
|
secure: true,
|
|
display_name: true,
|
|
color: true,
|
|
smtp_host: true,
|
|
smtp_port: true,
|
|
smtp_secure: true,
|
|
createdAt: true,
|
|
updatedAt: true
|
|
}
|
|
});
|
|
|
|
// Never return passwords
|
|
return NextResponse.json({
|
|
success: true,
|
|
accounts
|
|
});
|
|
} catch (error) {
|
|
console.error('Error fetching email accounts:', error);
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Failed to fetch email accounts',
|
|
details: error instanceof Error ? error.message : 'Unknown error'
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|