2026-06-15 09:28:22 +02:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
|
import { prisma } from "@/lib/prisma";
|
2026-06-24 07:02:03 +02:00
|
|
|
import { UMMAHID_URL } from "@/lib/auth";
|
2026-06-27 17:54:34 +08:00
|
|
|
import { checkRateLimit, getClientIp } from "@/lib/rate-limit";
|
2026-06-15 09:28:22 +02:00
|
|
|
|
|
|
|
|
export async function POST(req: NextRequest) {
|
|
|
|
|
try {
|
2026-06-27 17:54:34 +08:00
|
|
|
const ip = getClientIp(req);
|
|
|
|
|
const { allowed } = checkRateLimit(ip);
|
|
|
|
|
if (!allowed) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Too many requests. Try again later." },
|
|
|
|
|
{ status: 429, headers: { "Retry-After": "900", "X-RateLimit-Remaining": "0" } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-15 09:28:22 +02:00
|
|
|
const { email, password } = await req.json();
|
|
|
|
|
|
|
|
|
|
if (!email || !password) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Email and password are required" },
|
|
|
|
|
{ status: 400 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 07:02:03 +02:00
|
|
|
// Proxy to Ummah ID
|
|
|
|
|
const ummahRes = await fetch(`${UMMAHID_URL}/api/auth/login`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify({ email, password }),
|
|
|
|
|
});
|
2026-06-15 09:28:22 +02:00
|
|
|
|
2026-06-24 07:02:03 +02:00
|
|
|
const ummahData = await ummahRes.json();
|
|
|
|
|
|
|
|
|
|
if (!ummahRes.ok) {
|
2026-06-15 09:28:22 +02:00
|
|
|
return NextResponse.json(
|
2026-06-24 07:02:03 +02:00
|
|
|
{ error: ummahData.error || "Authentication failed" },
|
|
|
|
|
{ status: ummahRes.status }
|
2026-06-15 09:28:22 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 07:02:03 +02:00
|
|
|
const { token, user: ummahUser } = ummahData;
|
|
|
|
|
|
|
|
|
|
// Find or create local user by email
|
|
|
|
|
let localUser = await prisma.user.findUnique({ where: { email } });
|
|
|
|
|
if (!localUser) {
|
|
|
|
|
localUser = await prisma.user.create({
|
|
|
|
|
data: {
|
|
|
|
|
email,
|
|
|
|
|
name: ummahUser?.name || email?.split("@")[0] || "User",
|
|
|
|
|
provider: "ummahid",
|
|
|
|
|
providerId: ummahUser.id,
|
2026-06-27 17:54:34 +08:00
|
|
|
flhBalance: 5000,
|
2026-06-24 07:02:03 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-06-15 09:28:22 +02:00
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
|
token,
|
|
|
|
|
user: {
|
2026-06-24 07:02:03 +02:00
|
|
|
id: localUser.id,
|
|
|
|
|
email: localUser.email,
|
|
|
|
|
name: localUser.name,
|
|
|
|
|
isPremium: localUser.isPremium,
|
|
|
|
|
isPro: localUser.isPro,
|
|
|
|
|
flhBalance: localUser.flhBalance,
|
|
|
|
|
dailyMsgCount: localUser.dailyMsgCount,
|
2026-06-15 09:28:22 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Login error:", error);
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Login failed. Please try again." },
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|