Fix layout realtime

This commit is contained in:
2024-12-04 11:12:52 +08:00
parent b638865c33
commit a95f4a480e
7 changed files with 204 additions and 138 deletions

View File

@@ -7,7 +7,7 @@
}, },
"scripts": { "scripts": {
"dev": "bun --bun run next dev --experimental-https", "dev": "bun --bun run next dev --experimental-https",
"build": "bun --bun run next build", "build": "NODE_OPTIONS='--max-old-space-size=2048' bun --bun run next build",
"start": "bun --bun run next start", "start": "bun --bun run next start",
"lint": "bun --bun run next lint", "lint": "bun --bun run next lint",
"ver": "bunx commit-and-tag-version -- --prerelease" "ver": "bunx commit-and-tag-version -- --prerelease"

41
src/app/api/user/route.ts Normal file
View File

@@ -0,0 +1,41 @@
import { jwtVerify } from "jose";
import _ from "lodash";
import { cookies } from "next/headers";
import { NextResponse } from "next/server";
export async function GET() {
// const data = await req.text();
// console.log(data);
const c = cookies().get(process.env.NEXT_PUBLIC_BASE_SESSION_KEY!);
if (!c || !c?.value || _.isEmpty(c?.value) || _.isUndefined(c?.value)) {
return NextResponse.json({ status: 401, message: "Unauthorized" });
}
const token = c.value;
const dataUser = await decrypt({
token: token,
encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!,
});
return NextResponse.json({ status: 200, message: "OK", data: dataUser });
}
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;
}
}

View File

@@ -11,7 +11,7 @@ export default async function Layout({
}: { }: {
children: React.ReactNode; children: React.ReactNode;
}) { }) {
const userLoginId = await funGetUserIdByToken(); // const userLoginId = await funGetUserIdByToken();
// const dataUser = await funGlobal_getUserById({ // const dataUser = await funGlobal_getUserById({
// userId: userLoginId as string, // userId: userLoginId as string,
// }); // });
@@ -27,7 +27,7 @@ export default async function Layout({
return ( return (
<> <>
<RealtimeProvider <RealtimeProvider
userLoginId={userLoginId as string} userLoginId={"" as string}
WIBU_REALTIME_TOKEN={ WIBU_REALTIME_TOKEN={
ServerEnv.value?.NEXT_PUBLIC_WIBU_REALTIME_TOKEN as string ServerEnv.value?.NEXT_PUBLIC_WIBU_REALTIME_TOKEN as string
} }

View File

@@ -17,6 +17,8 @@ import {
gs_votingTiggerBeranda, gs_votingTiggerBeranda,
IRealtimeData, IRealtimeData,
} from "./global_state"; } from "./global_state";
import { useState } from "react";
import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global";
// const WIBU_REALTIME_TOKEN: string | undefined = // const WIBU_REALTIME_TOKEN: string | undefined =
// process.env.NEXT_PUBLIC_WIBU_REALTIME_TOKEN; // process.env.NEXT_PUBLIC_WIBU_REALTIME_TOKEN;
@@ -36,6 +38,7 @@ export default function RealtimeProvider({
userLoginId: string; userLoginId: string;
WIBU_REALTIME_TOKEN: string; WIBU_REALTIME_TOKEN: string;
}) { }) {
const [userId, setUserId] = useState("");
const [dataRealtime, setDataRealtime] = useAtom(gs_realtimeData); const [dataRealtime, setDataRealtime] = useAtom(gs_realtimeData);
const [newAdminNtf, setNewAdminNtf] = useAtom(gs_admin_ntf); const [newAdminNtf, setNewAdminNtf] = useAtom(gs_admin_ntf);
const [newUserNtf, setNewUserNtf] = useAtom(gs_user_ntf); const [newUserNtf, setNewUserNtf] = useAtom(gs_user_ntf);
@@ -72,7 +75,10 @@ export default function RealtimeProvider({
); );
useShallowEffect(() => { useShallowEffect(() => {
// if (WIBU_REALTIME_TOKEN === undefined) return alert("gak dapet key"); onLoadUser({
onSetUser(val: string) {
if (val !== "" || val !== undefined) {
try { try {
WibuRealtime.init({ WibuRealtime.init({
project: "hipmi", project: "hipmi",
@@ -89,7 +95,7 @@ export default function RealtimeProvider({
if ( if (
data.type == "notification" && data.type == "notification" &&
data.pushNotificationTo == "USER" && data.pushNotificationTo == "USER" &&
data.dataMessage?.userId == userLoginId data.dataMessage?.userId == userId
) { ) {
setNewUserNtf((e) => e + 1); setNewUserNtf((e) => e + 1);
setDataRealtime(data.dataMessage as any); setDataRealtime(data.dataMessage as any);
@@ -136,7 +142,7 @@ export default function RealtimeProvider({
data.type == "notification" && data.type == "notification" &&
data.pushNotificationTo == "USER" && data.pushNotificationTo == "USER" &&
data.dataMessage?.status == "Peserta Event" && data.dataMessage?.status == "Peserta Event" &&
userLoginId !== data.dataMessage?.userId userId !== data.dataMessage?.userId
) { ) {
setNewUserNtf((e) => e + 1); setNewUserNtf((e) => e + 1);
} }
@@ -164,7 +170,7 @@ export default function RealtimeProvider({
data.type == "notification" && data.type == "notification" &&
data.pushNotificationTo == "USER" && data.pushNotificationTo == "USER" &&
data.dataMessage?.status == "Voting Masuk" && data.dataMessage?.status == "Voting Masuk" &&
userLoginId !== data.dataMessage?.userId userId !== data.dataMessage?.userId
) { ) {
setNewUserNtf((e) => e + 1); setNewUserNtf((e) => e + 1);
} }
@@ -203,7 +209,24 @@ export default function RealtimeProvider({
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }
}, []); } else {
return undefined;
}
},
});
}, [setUserId]);
async function onLoadUser({
onSetUser,
}: {
onSetUser: (val: string) => void;
}) {
const res = await fetch("/api/user", {
method: "GET",
});
const result = await res.json();
onSetUser(result.data.id);
}
return null; return null;
} }

View File

@@ -7,13 +7,16 @@ import { jwtVerify } from "jose";
import { cookies } from "next/headers"; import { cookies } from "next/headers";
export async function funGetUserIdByToken() { export async function funGetUserIdByToken() {
const c = cookies().get(process.env.NEXT_PUBLIC_BASE_SESSION_KEY!); const SESSION_KEY = process.env.NEXT_PUBLIC_BASE_SESSION_KEY!;
// console.log("SESSION_KEY", SESSION_KEY);
const c = cookies().get("hipmi-key");
const cekUser = await decrypt({ const cekUser = await decrypt({
token: c?.value as string, token: c?.value as string,
encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!, encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!,
}); });
// console.log("userid" , cekUser?.id)
// const token = JSON.parse( // const token = JSON.parse(
// await unsealData(c?.value as string, { // await unsealData(c?.value as string, {

View File

@@ -74,9 +74,6 @@ export default function Ui_Konfirmasi({
} }
} }
if (isPresent === null) {
return <></>;
}
return ( return (
<> <>

View File

@@ -19,8 +19,10 @@ const middlewareConfig: MiddlewareConfig = {
userPath: "/dev/home", userPath: "/dev/home",
publicRoutes: [ publicRoutes: [
"/", "/",
"/api/validation",
"/api/auth/*", "/api/auth/*",
"/api/origin-url", "/api/origin-url",
"/api/user",
"/login", "/login",
"/register", "/register",
"/validasi", "/validasi",