Files
hipmi/src/app/api/user-validate/route.ts
bagasbanuna d43f3762a3 Fixed Bug Server
## Summary
This branch contains several bug fixes and performance improvements, primarily focusing on:
- Database connection management
- MQTT client stability
- Logging optimization
- API enhancements

## Detailed Changes

### Fixed Issues
1. **Database Connection Management**
   - Removed  from user-validate API route to prevent connection pool exhaustion
   - Added proper connection handling in global Prisma setup
   - Reduced logging verbosity in production environments

2. **MQTT Client Improvements**
   - Enhanced MQTT client initialization with proper error handling
   - Added reconnection logic with configurable intervals
   - Implemented cleanup functions to prevent memory leaks
   - Added separate initialization logic for server and client-side code

3. **Logging Optimization**
   - Removed excessive logging in middleware that was causing high CPU usage
   - Configured appropriate log levels for development and production

4. **Component Stability**
   - Added safety checks in text editor component to prevent MQTT operations on the server side
   - Improved MQTT publishing logic with client availability checks

### New Files
-  - Utility functions for safe database operations

### Modified Files
1.
   - Removed problematic  call

2.
   - Configured different logging levels for dev/prod
   - Removed process listeners that were causing disconnections
   - Exported prisma instance separately

3.
   - Removed excessive logging statements

4.
   - Enhanced initialization with error handling
   - Added reconnection and timeout configurations

5.
   - Added proper cleanup functions
   - Improved connection handling

6.
   - Added MQTT client availability checks
   - Prevented server-side MQTT operations

### Performance Improvements
- Reduced database connection overhead
- Optimized MQTT connection handling
- Eliminated unnecessary logging in production
- Better memory management with proper cleanup functions

### No Issue
2026-02-12 16:29:03 +08:00

91 lines
2.1 KiB
TypeScript

import { decrypt } from "@/app/(auth)/_lib/decrypt";
import { prisma } from "@/lib";
import { cookies } from "next/headers";
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
export async function GET(req: Request) {
try {
const SESSIONKEY = process.env.NEXT_PUBLIC_BASE_SESSION_KEY!;
const TOKENKEY = process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!;
const cookieStore = cookies();
const authHeader = req.headers.get("Authorization") || "";
const bearerToken = authHeader.startsWith("Bearer ")
? authHeader.split(" ")[1]
: undefined;
const token = cookieStore.get(SESSIONKEY)?.value || bearerToken;
if (!token) {
return NextResponse.json(
{
success: false,
message: "Unauthorized token not found",
},
{ status: 401 }
);
}
const decrypted = await decrypt({
token,
encodedKey: TOKENKEY,
});
if (!decrypted?.id) {
return NextResponse.json(
{
success: false,
message: "Unauthorized: invalid token data",
},
{ status: 401 }
);
}
const user = await prisma.user.findUnique({
where: {
id: decrypted.id,
},
});
if (!user) {
return NextResponse.json(
{
success: false,
message: "User tidak ditemukan",
},
{ status: 404 }
);
}
if (!user.active) {
return NextResponse.json(
{
success: false,
message: "User belum aktif",
data: user,
},
{ status: 403 }
);
}
return NextResponse.json({
success: true,
message: "Berhasil mendapatkan data",
data: user,
});
} catch (error) {
console.error("Error in user validation:", error);
return NextResponse.json(
{
success: false,
message: "Terjadi kesalahan pada server",
},
{ status: 500 }
);
}
// Removed prisma.$disconnect() from here to prevent connection pool exhaustion
// Prisma connections are handled globally and shouldn't be disconnected on each request
}