fix error server
This commit is contained in:
9
.env.build
Normal file
9
.env.build
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
DATABASE_URL="postgresql://bip:Production_123@localhost:5433/hipmi_build?schema=public"
|
||||||
|
WIBU_PWD="QWERTYUIOPLKJHGFDSAZXCVBNMQAZWSXEDCRFVTGBYHNUJMIKOLPPOIUYTREWQLKJHGFDSAMNBVCXZlghvftyguhijknhbgvcfytguu8okjnhbgvfty7u8oilkjnhgvtygu7u8ojilnkhbgvhujnkhghvjhukjnhb"
|
||||||
|
Client_KEY="SB-Mid-client-9NDTxltqdZrEB9m-"
|
||||||
|
Server_KEY="SB-Mid-server-NyltU-U7fLVQd1nv1LWBKylr"
|
||||||
|
MAPBOX_TOKEN="pk.eyJ1IjoibWFsaWtrdXJvc2FraSIsImEiOiJjbHppZHh2enYwZnQ3MmlyMWc2Y2RlMzZoIn0.XssvJvq_iniclf8UhvXaIg"
|
||||||
|
WS_APIKEY="eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7ImlkIjoiY20wdXIxeXh3MDAwMDU2bnNqbHI2MTg3cCIsIm5hbWUiOiJiYWdhcyIsImVtYWlsIjoiYmFnYXNAZ21haWwuY29tIiwiQXBpS2V5IjpbeyJpZCI6ImNtMHVyMXl5MzAwMDI1Nm5zazNia2xyc28iLCJuYW1lIjoiZGVmYXVsdCJ9XX0sImlhdCI6MTcyNTk1NjMyMSwiZXhwIjo0ODgxNzE2MzIxfQ.9D3YszZA_ljrkTKMcgo03u7PL5mo9OaoM41rbUrOsz8"
|
||||||
|
NEXT_PUBLIC_WIBU_REALTIME_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inp5aml4c2J1c2diYnR2am9namhvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MjY3Mzk1NDUsImV4cCI6MjA0MjMxNTU0NX0.jHNW5Pwhj-KXUQOMqzILaAz62k3xlKEL5XKE4xoR7Xc"
|
||||||
|
NEXT_PUBLIC_BASE_TOKEN_KEY="QWERTYUIOPLKJHGFDSAZXCVBNMQAZWSXEDCRFVTGBYHNUJMIKOLPPOIUYTREWQLKJHGFDSAMNBVCXZlghvftyguhijknhbgvcfytguu8okjnhbgvfty7u8oilkjnhgvtygu7u8ojilnkhbgvhujnkhghvjhukjnhb"
|
||||||
|
NEXT_PUBLIC_BASE_SESSION_KEY="hipmi-key"
|
||||||
3
build.wibu
Normal file
3
build.wibu
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
bun --env-file=.env.build prisma db push
|
||||||
|
bun --env-file=.env.build prisma db seed
|
||||||
|
bun --env-file=.env.build run build
|
||||||
22
src/app/(auth)/_lib/decrypt.back.txt
Normal file
22
src/app/(auth)/_lib/decrypt.back.txt
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { jwtVerify } from "jose";
|
||||||
|
|
||||||
|
export async function decrypt({
|
||||||
|
token,
|
||||||
|
encodedKey,
|
||||||
|
}: {
|
||||||
|
token: string;
|
||||||
|
encodedKey: string;
|
||||||
|
}): Promise<Record<string, any> | null> {
|
||||||
|
try {
|
||||||
|
const enc = new TextEncoder().encode(encodedKey);
|
||||||
|
const { payload } = await jwtVerify(token, enc, {
|
||||||
|
algorithms: ["HS256"],
|
||||||
|
});
|
||||||
|
return (payload.user as Record<string, any>) || null;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Gagal verifikasi session", error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wibu:0.2.82
|
||||||
@@ -7,16 +7,43 @@ export async function decrypt({
|
|||||||
token: string;
|
token: string;
|
||||||
encodedKey: string;
|
encodedKey: string;
|
||||||
}): Promise<Record<string, any> | null> {
|
}): Promise<Record<string, any> | null> {
|
||||||
|
if (!token || !encodedKey) {
|
||||||
|
console.error("Missing required parameters:", {
|
||||||
|
hasToken: !!token,
|
||||||
|
hasEncodedKey: !!encodedKey,
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const enc = new TextEncoder().encode(encodedKey);
|
const enc = new TextEncoder().encode(encodedKey);
|
||||||
const { payload } = await jwtVerify(token, enc, {
|
const { payload } = await jwtVerify(token, enc, {
|
||||||
algorithms: ["HS256"],
|
algorithms: ["HS256"],
|
||||||
});
|
});
|
||||||
return (payload.user as Record<string, any>) || null;
|
|
||||||
|
if (!payload || !payload.user) {
|
||||||
|
console.error("Invalid payload structure:", {
|
||||||
|
hasPayload: !!payload,
|
||||||
|
hasUser: payload ? !!payload.user : false,
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logging untuk debug
|
||||||
|
// console.log("Decrypt successful:", {
|
||||||
|
// payloadExists: !!payload,
|
||||||
|
// userExists: !!payload.user,
|
||||||
|
// tokenPreview: token.substring(0, 10) + "...",
|
||||||
|
// });
|
||||||
|
|
||||||
|
return payload.user as Record<string, any>;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Gagal verifikasi session", error);
|
console.error("Token verification failed:", {
|
||||||
|
error,
|
||||||
|
tokenLength: token?.length,
|
||||||
|
errorName: error instanceof Error ? error.name : "Unknown error",
|
||||||
|
errorMessage: error instanceof Error ? error.message : String(error),
|
||||||
|
});
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// wibu:0.2.82
|
|
||||||
|
|||||||
@@ -3,15 +3,16 @@ import backendLogger from "@/util/backendLogger";
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
const method = request.method;
|
if (request.method !== "GET") {
|
||||||
if (method !== "GET") {
|
return NextResponse.json(
|
||||||
return NextResponse.json({
|
{
|
||||||
success: false,
|
success: false,
|
||||||
message: "Method not allowed",
|
message: "Method not allowed",
|
||||||
},
|
},
|
||||||
{ status: 405 }
|
{ status: 405 }
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let fixData;
|
let fixData;
|
||||||
fixData = await prisma.voting.count({
|
fixData = await prisma.voting.count({
|
||||||
@@ -20,24 +21,27 @@ export async function GET(request: Request) {
|
|||||||
name: "Publish",
|
name: "Publish",
|
||||||
},
|
},
|
||||||
isArsip: true,
|
isArsip: true,
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
return NextResponse.json({
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Success get data voting dashboard',
|
message: "Success get data voting dashboard",
|
||||||
data: fixData
|
data: fixData,
|
||||||
},
|
},
|
||||||
{ status: 200 }
|
{ status: 200 }
|
||||||
)
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
backendLogger.error('Error get data voting dashboard >>', error);
|
backendLogger.error("Error get data voting dashboard >>", error);
|
||||||
NextResponse.json({
|
return NextResponse.json(
|
||||||
|
{
|
||||||
success: false,
|
success: false,
|
||||||
message: 'Error get data voting dashboard',
|
message: "Error get data voting dashboard",
|
||||||
reason: (error as Error).message
|
reason: (error as Error).message,
|
||||||
},
|
},
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
)
|
);
|
||||||
} finally {
|
} finally {
|
||||||
await prisma.$disconnect();
|
await prisma.$disconnect();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,39 @@
|
|||||||
|
import { decrypt } from "@/app/(auth)/_lib/decrypt";
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
export async function GET(request: NextRequest) {
|
|
||||||
const id = request.nextUrl.searchParams.get("id");
|
|
||||||
// const { searchParams } = new URL(request.url);
|
|
||||||
// const id = searchParams.get("id");
|
|
||||||
|
|
||||||
// const delToken = await prisma.userSession.delete({
|
export async function GET() {
|
||||||
// where: {
|
const sessionKey = process.env.NEXT_PUBLIC_BASE_SESSION_KEY!; // Gunakan environment variable yang tidak diekspos ke client-side
|
||||||
// userId: id as string,
|
if (!sessionKey) {
|
||||||
// },
|
|
||||||
// });
|
|
||||||
|
|
||||||
const del = cookies().delete(process.env.NEXT_PUBLIC_BASE_SESSION_KEY!);
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: true, message: "Logout Berhasil" },
|
{ success: false, message: "Session key tidak ditemukan" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cookieStore = cookies();
|
||||||
|
const sessionCookie = cookieStore.get(sessionKey);
|
||||||
|
|
||||||
|
if (!sessionCookie) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ success: false, message: "Session tidak ditemukan" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
cookieStore.delete(sessionKey);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ success: true, message: "Logout berhasil" },
|
||||||
{ status: 200 }
|
{ status: 200 }
|
||||||
);
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Gagal menghapus cookie:", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ success: false, message: "Gagal melakukan logout" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,14 @@ export async function POST(req: Request) {
|
|||||||
user: dataUser as any,
|
user: dataUser as any,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
{ success: false, message: "Gagal membuat session" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// Buat response dengan token dalam cookie
|
||||||
|
const response = NextResponse.json(
|
||||||
{
|
{
|
||||||
success: true,
|
success: true,
|
||||||
message: "Berhasil Login",
|
message: "Berhasil Login",
|
||||||
@@ -47,6 +54,16 @@ export async function POST(req: Request) {
|
|||||||
},
|
},
|
||||||
{ status: 200 }
|
{ status: 200 }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Set cookie dengan token yang sudah dipastikan tidak null
|
||||||
|
response.cookies.set(process.env.NEXT_PUBLIC_BASE_SESSION_KEY!, token, {
|
||||||
|
path: "/",
|
||||||
|
sameSite: "lax",
|
||||||
|
secure: process.env.NODE_ENV === "production",
|
||||||
|
maxAge: 30 * 24 * 60 * 60, // 30 hari dalam detik (1 bulan)
|
||||||
|
});
|
||||||
|
|
||||||
|
return response;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
backendLogger.log("API Error or Server Error", error);
|
backendLogger.log("API Error or Server Error", error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
@@ -21,13 +21,11 @@ export async function GET(request: Request) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await prisma.$disconnect();
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: true, message: "Berhasil mendapatkan data", data: res },
|
{ success: true, message: "Berhasil mendapatkan data", data: res },
|
||||||
{ status: 200 }
|
{ status: 200 }
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await prisma.$disconnect();
|
|
||||||
backendLogger.error("Error Get Master Bank >>", error);
|
backendLogger.error("Error Get Master Bank >>", error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
@@ -37,5 +35,7 @@ export async function GET(request: Request) {
|
|||||||
},
|
},
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
|
} finally {
|
||||||
|
await prisma.$disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
import { RouterAdminDashboard } from "@/app/lib/router_hipmi/router_admin";
|
|
||||||
import { RouterHome } from "@/app/lib/router_hipmi/router_home";
|
|
||||||
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
|
|
||||||
import { funGlobal_getUserById } from "@/app_modules/_global/fun/get/fun_get_user_by_id";
|
|
||||||
import { CheckCookies_UiView } from "@/app_modules/check_cookies";
|
|
||||||
import { redirect } from "next/navigation";
|
|
||||||
|
|
||||||
export default async function Page() {
|
|
||||||
// const userLoginId = await funGetUserIdByToken();
|
|
||||||
// const dataUser = await funGlobal_getUserById({ userId: userLoginId });
|
|
||||||
|
|
||||||
// if (dataUser?.masterUserRoleId === "1") {
|
|
||||||
// return redirect(RouterHome.main_home);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (dataUser?.masterUserRoleId !== "1") {
|
|
||||||
// return redirect(RouterAdminDashboard.splash_admin);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return <CheckCookies_UiView />;
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
import { CheckCookies_UiLayout } from "./layout_cek_cookies";
|
|
||||||
import { CheckCookies_UiView } from "./ui_check_cookies";
|
|
||||||
|
|
||||||
export { CheckCookies_UiView };
|
|
||||||
export { CheckCookies_UiLayout };
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { RouterAuth } from "@/app/lib/router_hipmi/router_auth";
|
|
||||||
import { useShallowEffect } from "@mantine/hooks";
|
|
||||||
import { redirect, useRouter } from "next/navigation";
|
|
||||||
import { MODEL_USER } from "../home/model/interface";
|
|
||||||
import { RouterHome } from "@/app/lib/router_hipmi/router_home";
|
|
||||||
import { RouterAdminDashboard } from "@/app/lib/router_hipmi/router_admin";
|
|
||||||
|
|
||||||
export function CheckCookies_UiLayout({
|
|
||||||
children,
|
|
||||||
dataUser,
|
|
||||||
}: {
|
|
||||||
children: React.ReactNode;
|
|
||||||
dataUser: MODEL_USER;
|
|
||||||
}) {
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
// if (dataUser.active == false){
|
|
||||||
// router.push(RouterHome.home_user_non_active, { scroll: false });
|
|
||||||
// return children
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
// useShallowEffect(() => {
|
|
||||||
// onCheckCookies();
|
|
||||||
// }, []);
|
|
||||||
|
|
||||||
// async function onCheckCookies() {
|
|
||||||
// const cek = await fetch("/api/check-cookies");
|
|
||||||
|
|
||||||
// const result = await cek.json();
|
|
||||||
|
|
||||||
// if (result.success === false) {
|
|
||||||
// router.push(RouterAuth.login, { scroll: false });
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (dataUser.masterUserRoleId === "1") {
|
|
||||||
// router.push(RouterHome.main_home, { scroll: false });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (dataUser.masterUserRoleId !== "1") {
|
|
||||||
// router.push(RouterAdminDashboard.splash_admin, { scroll: false });
|
|
||||||
// }
|
|
||||||
|
|
||||||
return <>{children}</>;
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { RouterAuth } from "@/app/lib/router_hipmi/router_auth";
|
|
||||||
import { Button, Center } from "@mantine/core";
|
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { UIGlobal_LayoutDefault } from "../_global/ui";
|
|
||||||
|
|
||||||
export function CheckCookies_UiView() {
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<UIGlobal_LayoutDefault>
|
|
||||||
<Center h={"80vh"}>
|
|
||||||
<Button radius={"xl"} onClick={() => router.push(RouterAuth.login)}>
|
|
||||||
Kembali ke Halaman Login
|
|
||||||
</Button>
|
|
||||||
</Center>
|
|
||||||
</UIGlobal_LayoutDefault>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
import { jwtVerify } from "jose";
|
import { jwtVerify } from "jose";
|
||||||
import { apies, pages } from "./lib/routes";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
type MiddlewareConfig = {
|
type MiddlewareConfig = {
|
||||||
apiPath: string;
|
apiPath: string;
|
||||||
@@ -110,7 +109,7 @@ export const middleware = async (req: NextRequest) => {
|
|||||||
// Preserve token in cookie when redirecting
|
// Preserve token in cookie when redirecting
|
||||||
if (token) {
|
if (token) {
|
||||||
response.cookies.set(sessionKey, token, {
|
response.cookies.set(sessionKey, token, {
|
||||||
httpOnly: true,
|
// httpOnly: true,
|
||||||
secure: process.env.NODE_ENV === "production",
|
secure: process.env.NODE_ENV === "production",
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
path: "/",
|
path: "/",
|
||||||
@@ -145,6 +144,19 @@ export const middleware = async (req: NextRequest) => {
|
|||||||
|
|
||||||
const userValidateJson = await userValidate.json();
|
const userValidateJson = await userValidate.json();
|
||||||
|
|
||||||
|
if (userValidateJson.success == true && userValidateJson.data == null) {
|
||||||
|
const logout = await fetch(new URL("/api/auth/logout", req.url), {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!logout.ok) {
|
||||||
|
throw new Error("Failed to logout user");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!userValidateJson.data.active) {
|
if (!userValidateJson.data.active) {
|
||||||
return setCorsHeaders(
|
return setCorsHeaders(
|
||||||
NextResponse.redirect(new URL("/waiting-room", req.url))
|
NextResponse.redirect(new URL("/waiting-room", req.url))
|
||||||
@@ -186,7 +198,7 @@ export const middleware = async (req: NextRequest) => {
|
|||||||
// Ensure token is preserved in cookie
|
// Ensure token is preserved in cookie
|
||||||
if (token) {
|
if (token) {
|
||||||
response.cookies.set(sessionKey, token, {
|
response.cookies.set(sessionKey, token, {
|
||||||
httpOnly: true,
|
// httpOnly: true,
|
||||||
secure: process.env.NODE_ENV === "production",
|
secure: process.env.NODE_ENV === "production",
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
path: "/",
|
path: "/",
|
||||||
|
|||||||
Reference in New Issue
Block a user