mirror of
https://git.coastit.co.za/caitlin/CRM_ENVR.git
synced 2026-07-10 11:15:43 +02:00
1adc4806fa
Security Fixes
File Change
.env.local Placeholder JWT secret (rotate to random value: openssl rand -base64 64)
src/middleware.ts Removed /api/auth/me bypass; API routes return JSON 401 (not 302 redirect)
src/lib/auth.ts sameSite: "lax" → "strict" (create + destroy cookie); SVG name chars are pre-computed (no injection)
src/lib/avatar.ts Added sanitizeName() — strips non-alphanumeric/safe chars, max 50 chars
src/app/api/users/route.ts POST restricted to super_admin; validates role against whitelist ["sales","admin","super_admin","dev"]; validates active defaults to true
src/app/api/users/[id]/route.ts DELETE restricted to super_admin; blocks self-deletion
src/app/api/leads/[id]/route.ts GET: non-admin users filtered by assigned_to; PATCH: ownership check + score validated 0-100 range
src/app/api/leads/route.ts DELETE: ownership + admin check; GET: ownership filter for non-admin users
src/app/api/conversations/[id]/messages/route.ts GET/POST: verify user is a conversation_participant before access
rust-ai/src/main.rs CORS: AllowOrigin::list(["localhost:3006", "127.0.0.1:3006"]) (was Any)
Performance Fixes
File Change
src/app/api/leads/route.ts Added limit (default 50), offset query params; ownership filter in SQL
src/app/api/users/route.ts Added limit (default 50), offset query params
src/app/api/conversations/route.ts Added LIMIT 50
src/app/api/leads/[id]/notes/route.ts Added limit (default 50), offset query params
src/app/api/conversations/[id]/messages/route.ts Added limit (default 100), offset, before query params
src/app/(dashboard)/chats/page.tsx Pruned to 5 cached conversations; useMemo wraps messages/conversation/filteredConversations; otherParticipant moved outside component; added 10MB file size limit; recordingChunksRef cleared on discard
src/app/(dashboard)/leads/[id]/page.tsx Optimistic status update rolls back on fetch failure
database/migrations/003_chat.sql Added composite index idx_messages_conversation_created on (conversation_id, created_at DESC)
Code Quality Fixes
File Change
src/lib/ai.ts Removed dead getInstructions()/updateInstructions() (called nonexistent /ai/instructions)
src/lib/auth.ts bcrypt.hash(password, 12) — kept (acceptable, OWASP-recommended)
src/app/login/page.tsx "Forgot password?" button now shows alert to contact admin (was no-op)
src/components/users/user-form-dialog.tsx Password min 6 → 8 chars
26 silent catch {} blocks across 16 files (see below) Added console.warn("...") context messages
121 lines
2.8 KiB
TypeScript
121 lines
2.8 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server"
|
|
import {
|
|
comparePassword,
|
|
getUserByEmail,
|
|
getUserByUsername,
|
|
mapDbUserToSessionUser,
|
|
recordLoginAttempt,
|
|
incrementFailedAttempts,
|
|
resetFailedAttempts,
|
|
isAccountLocked,
|
|
createSession,
|
|
} 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)
|
|
|
|
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 }
|
|
)
|
|
}
|
|
}
|