50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
||
import { auth } from "@/auth";
|
||
import { getMailSession, setMailSession, clearMailSession } from "@/lib/mail-session";
|
||
import { listFolders } from "@/lib/imap";
|
||
|
||
// POST /api/mail/auth — login to mailbox (store creds in cookie)
|
||
export async function POST(req: NextRequest) {
|
||
const session = await auth();
|
||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||
|
||
const { email, password } = await req.json();
|
||
if (!email || !password) {
|
||
return NextResponse.json({ error: "Email ve şifre gerekli" }, { status: 400 });
|
||
}
|
||
|
||
// Test the credentials by listing folders
|
||
try {
|
||
await listFolders({ email, password });
|
||
} catch (err: any) {
|
||
return NextResponse.json(
|
||
{ error: "IMAP bağlantısı başarısız: " + (err?.message ?? "Bilinmeyen hata") },
|
||
{ status: 401 }
|
||
);
|
||
}
|
||
|
||
await setMailSession({ email, password });
|
||
return NextResponse.json({ success: true, email });
|
||
}
|
||
|
||
// GET /api/mail/auth — check if mail session exists
|
||
export async function GET() {
|
||
const session = await auth();
|
||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||
|
||
const mailSession = await getMailSession();
|
||
if (!mailSession) {
|
||
return NextResponse.json({ connected: false });
|
||
}
|
||
return NextResponse.json({ connected: true, email: mailSession.email });
|
||
}
|
||
|
||
// DELETE /api/mail/auth — logout from mailbox
|
||
export async function DELETE() {
|
||
const session = await auth();
|
||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||
|
||
await clearMailSession();
|
||
return NextResponse.json({ success: true });
|
||
}
|