Fix: Cookies

This commit is contained in:
2024-11-26 01:06:46 +08:00
parent ac5b8a8c4b
commit 038c40a6fb
29 changed files with 341 additions and 172 deletions

View File

@@ -0,0 +1,28 @@
"use server";
import { ServerEnv } from "@/app/lib/server_env";
import { unsealData } from "iron-session";
import _ from "lodash";
import { cookies } from "next/headers";
export async function funCheckCookies() {
const c = cookies().get("mySession");
if (!c || !c?.value || _.isEmpty(c?.value) || _.isUndefined(c?.value)) {
// console.log("return pertama");
return false;
}
const token = JSON.parse(
await unsealData(c?.value as string, {
password: ServerEnv.value?.WIBU_PWD as string,
})
);
if (_.isEmpty(token)) {
// console.log("return kedua");
return false;
}
return true;
}

View File

@@ -1,17 +1,26 @@
"use server";
import prisma from "@/app/lib/prisma";
import { ServerEnv } from "@/app/lib/server_env";
import { unsealData } from "iron-session";
import { cookies } from "next/headers";
export async function funGetUserIdByToken() {
const c = cookies().get("mySession");
const token = c?.value
const cekToken = await prisma.userSession.findFirst({
where: {
token: token,
},
});
const c = cookies().get("mySession");
if (cekToken === null) return null
return cekToken.userId;
const token = JSON.parse(
await unsealData(c?.value as string, {
password: process.env.WIBU_PWD as string,
})
);
return token.id;
// const token = c?.value
// const cekToken = await prisma.userSession.findFirst({
// where: {
// token: token,
// },
// });
// if (cekToken === null) return null
// return cekToken.userId;
}

View File

@@ -1,4 +1,6 @@
import { ServerEnv } from "@/app/lib/server_env";
import { TokenStorage } from "@/app/lib/token";
import { envs } from "@/lib/envs";
export async function funGlobal_UploadToStorage({
file,
@@ -7,7 +9,7 @@ export async function funGlobal_UploadToStorage({
file: File;
dirId: string;
}) {
if (!file) console.log("Tidak ada file");
const Env_WS_APIKEY = TokenStorage.value;
const allowedMimeTypes = [
"image/png",
@@ -35,7 +37,7 @@ export async function funGlobal_UploadToStorage({
method: "POST",
body: formData,
headers: {
Authorization: `Bearer ${TokenStorage.value}`,
Authorization: `Bearer ${Env_WS_APIKEY}`,
},
});
@@ -52,4 +54,6 @@ export async function funGlobal_UploadToStorage({
console.error("Error:", error);
return { success: false, data: {} };
}
return { success: false, data: { id: "" } };
}