Fix Vercel function request body parsing
🐛 Bug Fix: - Replace req.json() with req.text() + JSON.parse() for Vercel compatibility - Add proper error handling for malformed JSON requests - Fix authentication endpoints (login/register) body parsing - Fix admin users endpoint body parsing 🔧 Technical Details: - Vercel functions don't support req.json() method directly - Use req.text() to get raw body content then parse manually - Add try/catch blocks for JSON parsing errors - Maintain same API interface and error responses ✅ Endpoints Fixed: - /api/auth/register - User registration - /api/auth/login - User authentication - /api/admin/users - Admin user management This resolves the 'req.json is not a function' error in production.
This commit is contained in:
parent
dc95d0d76d
commit
07b3dda6fa
3 changed files with 42 additions and 3 deletions
|
|
@ -105,7 +105,20 @@ export default async function handler(req: NextRequest) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = await req.json();
|
// Parse request body
|
||||||
|
let body: { isActive?: boolean; roles?: string[] };
|
||||||
|
try {
|
||||||
|
const bodyText = await req.text();
|
||||||
|
body = JSON.parse(bodyText);
|
||||||
|
} catch (parseError) {
|
||||||
|
return new NextResponse(JSON.stringify({
|
||||||
|
error: 'Invalid JSON in request body'
|
||||||
|
}), {
|
||||||
|
status: 400,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const { isActive, roles } = body;
|
const { isActive, roles } = body;
|
||||||
|
|
||||||
// Update user status
|
// Update user status
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,20 @@ export default async function handler(req: NextRequest) {
|
||||||
const client = await pool.connect();
|
const client = await pool.connect();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const body: LoginRequest = await req.json();
|
// Parse request body
|
||||||
|
let body: LoginRequest;
|
||||||
|
try {
|
||||||
|
const bodyText = await req.text();
|
||||||
|
body = JSON.parse(bodyText);
|
||||||
|
} catch (parseError) {
|
||||||
|
return new NextResponse(JSON.stringify({
|
||||||
|
error: 'Invalid JSON in request body'
|
||||||
|
}), {
|
||||||
|
status: 400,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const { username, password } = body;
|
const { username, password } = body;
|
||||||
|
|
||||||
// Validate input
|
// Validate input
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,20 @@ export default async function handler(req: NextRequest) {
|
||||||
const client = await pool.connect();
|
const client = await pool.connect();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const body: RegisterRequest = await req.json();
|
// Parse request body
|
||||||
|
let body: RegisterRequest;
|
||||||
|
try {
|
||||||
|
const bodyText = await req.text();
|
||||||
|
body = JSON.parse(bodyText);
|
||||||
|
} catch (parseError) {
|
||||||
|
return new NextResponse(JSON.stringify({
|
||||||
|
error: 'Invalid JSON in request body'
|
||||||
|
}), {
|
||||||
|
status: 400,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const { username, email, password, firstName, lastName } = body;
|
const { username, email, password, firstName, lastName } = body;
|
||||||
|
|
||||||
// Validate input
|
// Validate input
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue