Files
webmailserver/app/api/mail/auth/route.ts
2026-05-14 01:57:52 +03:00

50 lines
1.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 });
}