Fixed the login again?
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { NextRequest } from "next/server"
|
||||
import {
|
||||
comparePassword,
|
||||
getUserByEmail,
|
||||
@@ -13,6 +13,13 @@ import {
|
||||
SESSION_COOKIE,
|
||||
} from "@/lib/auth"
|
||||
|
||||
function jsonResponse(data: unknown, status: number) {
|
||||
return new Response(JSON.stringify(data), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
})
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { email, username, password } = await request.json()
|
||||
@@ -20,16 +27,16 @@ export async function POST(request: NextRequest) {
|
||||
const credential = email || username
|
||||
|
||||
if (!credential || !password) {
|
||||
return NextResponse.json(
|
||||
return jsonResponse(
|
||||
{ error: "Email/Username and password are required." },
|
||||
{ status: 400 }
|
||||
400
|
||||
)
|
||||
}
|
||||
|
||||
if (credential.trim().length === 0 || password.trim().length === 0) {
|
||||
return NextResponse.json(
|
||||
return jsonResponse(
|
||||
{ error: "Credentials cannot be empty." },
|
||||
{ status: 400 }
|
||||
400
|
||||
)
|
||||
}
|
||||
|
||||
@@ -59,9 +66,9 @@ export async function POST(request: NextRequest) {
|
||||
false,
|
||||
"User not found"
|
||||
)
|
||||
return NextResponse.json(
|
||||
return jsonResponse(
|
||||
{ error: "Invalid email/username or password." },
|
||||
{ status: 401 }
|
||||
401
|
||||
)
|
||||
}
|
||||
|
||||
@@ -75,9 +82,9 @@ export async function POST(request: NextRequest) {
|
||||
false,
|
||||
lockStatus.reason
|
||||
)
|
||||
return NextResponse.json(
|
||||
return jsonResponse(
|
||||
{ error: lockStatus.reason },
|
||||
{ status: 423 }
|
||||
423
|
||||
)
|
||||
}
|
||||
|
||||
@@ -92,9 +99,9 @@ export async function POST(request: NextRequest) {
|
||||
false,
|
||||
"Invalid password"
|
||||
)
|
||||
return NextResponse.json(
|
||||
return jsonResponse(
|
||||
{ error: "Invalid email/username or password." },
|
||||
{ status: 401 }
|
||||
401
|
||||
)
|
||||
}
|
||||
|
||||
@@ -112,19 +119,20 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
const user = mapDbUserToSessionUser(dbUser)
|
||||
|
||||
const response = NextResponse.json({ user }, { status: 200 })
|
||||
response.cookies.set(SESSION_COOKIE, token, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
sameSite: "strict",
|
||||
path: "/",
|
||||
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,
|
||||
},
|
||||
})
|
||||
return response
|
||||
} catch (error) {
|
||||
console.error("Login error:", error)
|
||||
return NextResponse.json(
|
||||
{ error: "Authentication service unavailable." },
|
||||
{ status: 503 }
|
||||
return new Response(
|
||||
JSON.stringify({ error: "Authentication service unavailable." }),
|
||||
{ status: 503, headers: { "Content-Type": "application/json" } }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,19 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { SESSION_COOKIE } from "@/lib/auth"
|
||||
|
||||
export async function POST() {
|
||||
try {
|
||||
const response = NextResponse.json({ success: true }, { status: 200 })
|
||||
response.cookies.set(SESSION_COOKIE, "", {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
sameSite: "strict",
|
||||
path: "/",
|
||||
maxAge: 0,
|
||||
return new Response(JSON.stringify({ success: true }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Set-Cookie": `${SESSION_COOKIE}=; HttpOnly; SameSite=Strict; Path=/; Max-Age=0`,
|
||||
},
|
||||
})
|
||||
return response
|
||||
} catch (error) {
|
||||
console.error("Logout error:", error)
|
||||
return NextResponse.json(
|
||||
{ error: "Logout failed." },
|
||||
{ status: 500 }
|
||||
return new Response(
|
||||
JSON.stringify({ error: "Logout failed." }),
|
||||
{ status: 500, headers: { "Content-Type": "application/json" } }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user