This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { NextRequest } from "next/server"
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import {
|
||||
comparePassword,
|
||||
getUserByEmail,
|
||||
@@ -15,10 +15,7 @@ import {
|
||||
import { checkRateLimit } from "@/lib/rate-limit"
|
||||
|
||||
function jsonResponse(data: unknown, status: number, headers?: Record<string, string>) {
|
||||
return new Response(JSON.stringify(data), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json", ...headers },
|
||||
})
|
||||
return NextResponse.json(data, { status, headers })
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
@@ -132,20 +129,21 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
const user = mapDbUserToSessionUser(dbUser)
|
||||
|
||||
const cookieStr = `${SESSION_COOKIE}=${token}; HttpOnly; SameSite=Strict; Path=/; Max-Age=86400${process.env.NODE_ENV === "production" ? "; Secure" : ""}`
|
||||
|
||||
return new Response(JSON.stringify({ user }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Set-Cookie": cookieStr,
|
||||
},
|
||||
const isHttps = request.nextUrl.protocol === "https:" || request.headers.get("x-forwarded-proto") === "https"
|
||||
const response = NextResponse.json({ user })
|
||||
response.cookies.set(SESSION_COOKIE, token, {
|
||||
httpOnly: true,
|
||||
sameSite: "strict",
|
||||
path: "/",
|
||||
maxAge: 86400,
|
||||
secure: isHttps,
|
||||
})
|
||||
return response
|
||||
} catch (error) {
|
||||
console.error("Login error:", error)
|
||||
return new Response(
|
||||
JSON.stringify({ error: "Authentication service unavailable." }),
|
||||
{ status: 503, headers: { "Content-Type": "application/json" } }
|
||||
return NextResponse.json(
|
||||
{ error: "Authentication service unavailable." },
|
||||
{ status: 503 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user