Fix layout realtime
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
},
|
||||
"scripts": {
|
||||
"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",
|
||||
"lint": "bun --bun run next lint",
|
||||
"ver": "bunx commit-and-tag-version -- --prerelease"
|
||||
@@ -94,4 +94,4 @@
|
||||
"wibu-pkg": "^1.0.3",
|
||||
"yaml": "^2.3.2"
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/app/api/user/route.ts
Normal file
41
src/app/api/user/route.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ export default async function Layout({
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const userLoginId = await funGetUserIdByToken();
|
||||
// const userLoginId = await funGetUserIdByToken();
|
||||
// const dataUser = await funGlobal_getUserById({
|
||||
// userId: userLoginId as string,
|
||||
// });
|
||||
@@ -27,7 +27,7 @@ export default async function Layout({
|
||||
return (
|
||||
<>
|
||||
<RealtimeProvider
|
||||
userLoginId={userLoginId as string}
|
||||
userLoginId={"" as string}
|
||||
WIBU_REALTIME_TOKEN={
|
||||
ServerEnv.value?.NEXT_PUBLIC_WIBU_REALTIME_TOKEN as string
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ import {
|
||||
gs_votingTiggerBeranda,
|
||||
IRealtimeData,
|
||||
} from "./global_state";
|
||||
import { useState } from "react";
|
||||
import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global";
|
||||
|
||||
// const WIBU_REALTIME_TOKEN: string | undefined =
|
||||
// process.env.NEXT_PUBLIC_WIBU_REALTIME_TOKEN;
|
||||
@@ -36,6 +38,7 @@ export default function RealtimeProvider({
|
||||
userLoginId: string;
|
||||
WIBU_REALTIME_TOKEN: string;
|
||||
}) {
|
||||
const [userId, setUserId] = useState("");
|
||||
const [dataRealtime, setDataRealtime] = useAtom(gs_realtimeData);
|
||||
const [newAdminNtf, setNewAdminNtf] = useAtom(gs_admin_ntf);
|
||||
const [newUserNtf, setNewUserNtf] = useAtom(gs_user_ntf);
|
||||
@@ -72,138 +75,158 @@ export default function RealtimeProvider({
|
||||
);
|
||||
|
||||
useShallowEffect(() => {
|
||||
// if (WIBU_REALTIME_TOKEN === undefined) return alert("gak dapet key");
|
||||
try {
|
||||
WibuRealtime.init({
|
||||
project: "hipmi",
|
||||
WIBU_REALTIME_TOKEN: WIBU_REALTIME_TOKEN,
|
||||
onData(data: TypeNotification) {
|
||||
if (
|
||||
data.type == "notification" &&
|
||||
data.pushNotificationTo == "ADMIN"
|
||||
) {
|
||||
setNewAdminNtf((e) => e + 1);
|
||||
onLoadUser({
|
||||
onSetUser(val: string) {
|
||||
|
||||
if (val !== "" || val !== undefined) {
|
||||
try {
|
||||
WibuRealtime.init({
|
||||
project: "hipmi",
|
||||
WIBU_REALTIME_TOKEN: WIBU_REALTIME_TOKEN,
|
||||
onData(data: TypeNotification) {
|
||||
if (
|
||||
data.type == "notification" &&
|
||||
data.pushNotificationTo == "ADMIN"
|
||||
) {
|
||||
setNewAdminNtf((e) => e + 1);
|
||||
}
|
||||
|
||||
// Notifikasi ke semua user , yang datanya di acc admin
|
||||
if (
|
||||
data.type == "notification" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.userId == userId
|
||||
) {
|
||||
setNewUserNtf((e) => e + 1);
|
||||
setDataRealtime(data.dataMessage as any);
|
||||
}
|
||||
|
||||
// ---------------------- JOB ------------------------- //
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "ADMIN" &&
|
||||
data.dataMessage?.kategoriApp == "JOB"
|
||||
) {
|
||||
setIsAdminJob_TriggerReview(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.kategoriApp == "JOB" &&
|
||||
data.dataMessage.status == "Publish"
|
||||
) {
|
||||
setIsTriggerJobBeranda(true);
|
||||
}
|
||||
// ---------------------- JOB ------------------------- //
|
||||
|
||||
// ---------------------- EVENT ------------------------- //
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "ADMIN" &&
|
||||
data.dataMessage?.kategoriApp == "EVENT"
|
||||
) {
|
||||
setIsAdminEvent_TriggerReview(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.kategoriApp == "EVENT" &&
|
||||
data.dataMessage.status == "Publish"
|
||||
) {
|
||||
setIsTriggerEventBeranda(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "notification" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.status == "Peserta Event" &&
|
||||
userId !== data.dataMessage?.userId
|
||||
) {
|
||||
setNewUserNtf((e) => e + 1);
|
||||
}
|
||||
// ---------------------- EVENT ------------------------- //
|
||||
|
||||
// ---------------------- VOTING ------------------------- //
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "ADMIN" &&
|
||||
data.dataMessage?.kategoriApp == "VOTING"
|
||||
) {
|
||||
setIsAdminVoting_TriggerReview(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.kategoriApp == "VOTING" &&
|
||||
data.dataMessage.status == "Publish"
|
||||
) {
|
||||
setIsTriggerVotingBeranda(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "notification" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.status == "Voting Masuk" &&
|
||||
userId !== data.dataMessage?.userId
|
||||
) {
|
||||
setNewUserNtf((e) => e + 1);
|
||||
}
|
||||
// ---------------------- VOTING ------------------------- //
|
||||
|
||||
// ---------------------- DONASI ------------------------- //
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "ADMIN" &&
|
||||
data.dataMessage?.kategoriApp == "DONASI"
|
||||
) {
|
||||
setIsAdminDonasi_TriggerReview(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.kategoriApp == "DONASI" &&
|
||||
data.dataMessage.status == "Publish"
|
||||
) {
|
||||
setIsTriggerDonasiBeranda(true);
|
||||
}
|
||||
|
||||
// if (
|
||||
// data.type == "notification" &&
|
||||
// data.pushNotificationTo == "ADMIN" &&
|
||||
// data.dataMessage?.status == "Menunggu" &&
|
||||
// userLoginId !== data.dataMessage?.userId
|
||||
// ) {
|
||||
// console.log("yes");
|
||||
// }
|
||||
|
||||
// ---------------------- DONASI ------------------------- //
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
},
|
||||
});
|
||||
}, [setUserId]);
|
||||
|
||||
// Notifikasi ke semua user , yang datanya di acc admin
|
||||
if (
|
||||
data.type == "notification" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.userId == userLoginId
|
||||
) {
|
||||
setNewUserNtf((e) => e + 1);
|
||||
setDataRealtime(data.dataMessage as any);
|
||||
}
|
||||
|
||||
// ---------------------- JOB ------------------------- //
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "ADMIN" &&
|
||||
data.dataMessage?.kategoriApp == "JOB"
|
||||
) {
|
||||
setIsAdminJob_TriggerReview(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.kategoriApp == "JOB" &&
|
||||
data.dataMessage.status == "Publish"
|
||||
) {
|
||||
setIsTriggerJobBeranda(true);
|
||||
}
|
||||
// ---------------------- JOB ------------------------- //
|
||||
|
||||
// ---------------------- EVENT ------------------------- //
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "ADMIN" &&
|
||||
data.dataMessage?.kategoriApp == "EVENT"
|
||||
) {
|
||||
setIsAdminEvent_TriggerReview(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.kategoriApp == "EVENT" &&
|
||||
data.dataMessage.status == "Publish"
|
||||
) {
|
||||
setIsTriggerEventBeranda(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "notification" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.status == "Peserta Event" &&
|
||||
userLoginId !== data.dataMessage?.userId
|
||||
) {
|
||||
setNewUserNtf((e) => e + 1);
|
||||
}
|
||||
// ---------------------- EVENT ------------------------- //
|
||||
|
||||
// ---------------------- VOTING ------------------------- //
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "ADMIN" &&
|
||||
data.dataMessage?.kategoriApp == "VOTING"
|
||||
) {
|
||||
setIsAdminVoting_TriggerReview(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.kategoriApp == "VOTING" &&
|
||||
data.dataMessage.status == "Publish"
|
||||
) {
|
||||
setIsTriggerVotingBeranda(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "notification" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.status == "Voting Masuk" &&
|
||||
userLoginId !== data.dataMessage?.userId
|
||||
) {
|
||||
setNewUserNtf((e) => e + 1);
|
||||
}
|
||||
// ---------------------- VOTING ------------------------- //
|
||||
|
||||
// ---------------------- DONASI ------------------------- //
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "ADMIN" &&
|
||||
data.dataMessage?.kategoriApp == "DONASI"
|
||||
) {
|
||||
setIsAdminDonasi_TriggerReview(true);
|
||||
}
|
||||
|
||||
if (
|
||||
data.type == "trigger" &&
|
||||
data.pushNotificationTo == "USER" &&
|
||||
data.dataMessage?.kategoriApp == "DONASI" &&
|
||||
data.dataMessage.status == "Publish"
|
||||
) {
|
||||
setIsTriggerDonasiBeranda(true);
|
||||
}
|
||||
|
||||
// if (
|
||||
// data.type == "notification" &&
|
||||
// data.pushNotificationTo == "ADMIN" &&
|
||||
// data.dataMessage?.status == "Menunggu" &&
|
||||
// userLoginId !== data.dataMessage?.userId
|
||||
// ) {
|
||||
// console.log("yes");
|
||||
// }
|
||||
|
||||
// ---------------------- DONASI ------------------------- //
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}, []);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -7,13 +7,16 @@ import { jwtVerify } from "jose";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
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({
|
||||
token: c?.value as string,
|
||||
encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!,
|
||||
});
|
||||
|
||||
// console.log("userid" , cekUser?.id)
|
||||
|
||||
// const token = JSON.parse(
|
||||
// await unsealData(c?.value as string, {
|
||||
|
||||
@@ -74,9 +74,6 @@ export default function Ui_Konfirmasi({
|
||||
}
|
||||
}
|
||||
|
||||
if (isPresent === null) {
|
||||
return <></>;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
@@ -19,8 +19,10 @@ const middlewareConfig: MiddlewareConfig = {
|
||||
userPath: "/dev/home",
|
||||
publicRoutes: [
|
||||
"/",
|
||||
"/api/validation",
|
||||
"/api/auth/*",
|
||||
"/api/origin-url",
|
||||
"/api/user",
|
||||
"/login",
|
||||
"/register",
|
||||
"/validasi",
|
||||
|
||||
Reference in New Issue
Block a user