Fix: middleware

Deskripsi
- Fix middleware
- Fix metode login ( sekarang menggunakan api )
This commit is contained in:
2024-12-02 16:24:03 +08:00
parent 45279cd37f
commit 31124c5500
55 changed files with 1675 additions and 420 deletions

View 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

View File

@@ -0,0 +1,25 @@
import { SignJWT } from "jose";
export async function encrypt({
user,
exp = "7 year",
encodedKey,
}: {
user: Record<string, any>;
exp?: string;
encodedKey: string;
}): Promise<string | null> {
try {
const enc = new TextEncoder().encode(encodedKey);
return new SignJWT({ user })
.setProtectedHeader({ alg: "HS256" })
.setIssuedAt()
.setExpirationTime(exp)
.sign(enc);
} catch (error) {
console.error("Gagal mengenkripsi", error);
return null;
}
}
// wibu:0.2.82

View File

@@ -0,0 +1,35 @@
import { cookies } from "next/headers";
import { encrypt } from "./encrypt";
export async function sessionCreate({
sessionKey,
exp = "7 year",
encodedKey,
user,
}: {
sessionKey: string;
exp?: string;
encodedKey: string;
user: Record<string, unknown>;
}) {
const token = await encrypt({
exp,
encodedKey,
user,
});
const cookie: any = {
key: sessionKey,
value: token,
options: {
httpOnly: true,
sameSite: "lax",
path: "/",
},
};
cookies().set(cookie.key, cookie.value, { ...cookie.options });
return token;
}
// wibu:0.2.82

View File

@@ -0,0 +1,27 @@
import { prisma } from "@/app/lib";
import { sessionCreate } from "../../_lib/session_create";
export async function POST(req: Request) {
const user = await prisma.user.findUnique({
where: {
nomor: "6281339158911",
},
select: {
id: true,
nomor: true,
},
});
if (!user)
return new Response(
JSON.stringify({ success: false, message: "User not found" }), {status: 404}
);
const token = await sessionCreate({
sessionKey: process.env.NEXT_PUBLIC_BASE_SESSION_KEY!,
encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!,
user: user as any,
});
return new Response(JSON.stringify({ success: true, token }));
}

View File

@@ -0,0 +1,5 @@
import { cookies } from "next/headers";
export async function GET() {
const del = cookies().delete(process.env.NEXT_PUBLIC_BASE_SESSION_KEY!);
return new Response(JSON.stringify({ success: true }));
}

View File

@@ -0,0 +1,39 @@
"use client";
import { Button } from "@mantine/core";
import { useState } from "react";
export default function Page() {
const [loading, setLoading] = useState(false);
async function login() {
setLoading(true);
try {
const res = await fetch("/auth/api/login", {
method: "POST",
});
const dataText = await res.text();
if (!res.ok) {
console.error(dataText);
throw new Error(res.statusText);
}
const dataJson = JSON.parse(dataText);
console.log(dataJson);
// window.location.replace("/dev/home");
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
}
return (
<>
<Button loading={loading} onClick={login}>
Login
</Button>
</>
);
}