20a1744e7f
Database & Security: - Dual password storage: bcrypt (auth) + pgcrypto AES-256 (recovery) - SUPER_ADMIN master key recovery system (master_keys table) - Row Level Security on customers, leads, opportunities, communications, tasks - SALES_USER: own records only - ADMIN: all records - SUPER_ADMIN: all records (bypasses RLS) - Immutable audit logs (DELETE/UPDATE blocked by triggers) - New audit event types: BUG_CREATED, BUG_UPDATED, BUG_ASSIGNED, BUG_RESOLVED, LOGIN, LOGOUT - Database export logging (database_export_logs table) - Backup logging with pg_dump script (scripts/backup.ps1) - Fixed audit constraint to allow new action types Authentication: - Random JWT secret generated on every dev server start (invalidates all prior sessions after restart) - Session cookie is now session-only (no maxAge) - setSessionContext() for RLS integration Bug Reporting System: - bug_reports table with RLS (insert by all, select/update by admin only) - POST /api/bug-reports (any authenticated user) - GET /api/bug-reports (admin/super_admin only) - PATCH /api/bug-reports/:id (admin/super_admin only) - POST /api/auth/recover (super_admin password recovery) - Audit logging for all bug report actions Other: - Added 'dev' to UserRole type - Bug report modal UI with severity selector - Added bug report button to topbar
123 lines
2.9 KiB
TypeScript
123 lines
2.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server"
|
|
import {
|
|
comparePassword,
|
|
getUserByEmail,
|
|
getUserByUsername,
|
|
mapDbUserToSessionUser,
|
|
recordLoginAttempt,
|
|
incrementFailedAttempts,
|
|
resetFailedAttempts,
|
|
isAccountLocked,
|
|
createSession,
|
|
setSessionContext,
|
|
} from "@/lib/auth"
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const { email, username, password } = await request.json()
|
|
|
|
const credential = email || username
|
|
|
|
if (!credential || !password) {
|
|
return NextResponse.json(
|
|
{ error: "Email/Username and password are required." },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
if (credential.trim().length === 0 || password.trim().length === 0) {
|
|
return NextResponse.json(
|
|
{ error: "Credentials cannot be empty." },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const ipAddress =
|
|
request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ||
|
|
request.headers.get("x-real-ip") ||
|
|
"127.0.0.1"
|
|
|
|
const userAgent = request.headers.get("user-agent") || null
|
|
|
|
// Try to find user by email first, then by username
|
|
let dbUser =
|
|
email || credential.includes("@")
|
|
? await getUserByEmail(credential)
|
|
: null
|
|
|
|
if (!dbUser) {
|
|
dbUser = await getUserByUsername(credential)
|
|
}
|
|
|
|
if (!dbUser) {
|
|
await recordLoginAttempt(
|
|
null,
|
|
credential,
|
|
ipAddress,
|
|
userAgent,
|
|
false,
|
|
"User not found"
|
|
)
|
|
return NextResponse.json(
|
|
{ error: "Invalid email/username or password." },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const lockStatus = await isAccountLocked(dbUser)
|
|
if (lockStatus.locked) {
|
|
await recordLoginAttempt(
|
|
dbUser.id,
|
|
credential,
|
|
ipAddress,
|
|
userAgent,
|
|
false,
|
|
lockStatus.reason
|
|
)
|
|
return NextResponse.json(
|
|
{ error: lockStatus.reason },
|
|
{ status: 423 }
|
|
)
|
|
}
|
|
|
|
const valid = await comparePassword(password, dbUser.password_hash)
|
|
if (!valid) {
|
|
await incrementFailedAttempts(dbUser.id)
|
|
await recordLoginAttempt(
|
|
dbUser.id,
|
|
credential,
|
|
ipAddress,
|
|
userAgent,
|
|
false,
|
|
"Invalid password"
|
|
)
|
|
return NextResponse.json(
|
|
{ error: "Invalid email/username or password." },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
await resetFailedAttempts(dbUser.id)
|
|
await recordLoginAttempt(
|
|
dbUser.id,
|
|
credential,
|
|
ipAddress,
|
|
userAgent,
|
|
true
|
|
)
|
|
|
|
await createSession(dbUser.id, dbUser.role_name)
|
|
await setSessionContext(dbUser.id, ipAddress)
|
|
|
|
const user = mapDbUserToSessionUser(dbUser)
|
|
|
|
return NextResponse.json({ user }, { status: 200 })
|
|
} catch (error) {
|
|
console.error("Login error:", error)
|
|
return NextResponse.json(
|
|
{ error: "Authentication service unavailable." },
|
|
{ status: 503 }
|
|
)
|
|
}
|
|
}
|