diff --git a/src/app/api/auth/logout/route.ts b/src/app/api/auth/logout/route.ts index d4c9049e..1ec6d2d1 100644 --- a/src/app/api/auth/logout/route.ts +++ b/src/app/api/auth/logout/route.ts @@ -3,7 +3,7 @@ import { NextResponse } from "next/server"; export async function GET() { cookies().set({ - name: "ssn", + name: "mySession", value: "", maxAge: 0, }); diff --git a/src/app/api/auth/register/route.ts b/src/app/api/auth/register/route.ts index 2c5ff71a..463f63d2 100644 --- a/src/app/api/auth/register/route.ts +++ b/src/app/api/auth/register/route.ts @@ -37,7 +37,7 @@ export async function POST(req: Request) { ); cookies().set({ - name: "ssn", + name: "mySession", value: seal, maxAge: 60 * 60 * 24 * 7, }); diff --git a/src/app/api/auth/validasi/route.ts b/src/app/api/auth/validasi/route.ts index 27858a53..11ebd0a7 100644 --- a/src/app/api/auth/validasi/route.ts +++ b/src/app/api/auth/validasi/route.ts @@ -35,10 +35,9 @@ export async function POST(req: Request) { ); cookies().set({ - name: "ssn", + name: "mySession", value: res, maxAge: 60 * 60 * 24 * 7, - }); revalidatePath("/dev/home"); diff --git a/src/app/api/check-cookies/route.ts b/src/app/api/check-cookies/route.ts index c6e67b93..b02fce67 100644 --- a/src/app/api/check-cookies/route.ts +++ b/src/app/api/check-cookies/route.ts @@ -4,7 +4,7 @@ import { NextResponse } from "next/server"; export async function GET(req: Request) { const cekCookies = cookies(); - const c = cekCookies.get("ssn"); + const c = cekCookies.get("mySession"); if (!c || !c?.value || _.isEmpty(c?.value) || _.isUndefined(c?.value)) { return NextResponse.json({ success: false }); diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 998860e6..d062d2d2 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -41,15 +41,15 @@ export default async function RootLayout({ }: { children: React.ReactNode; }) { - // const userLoginId = await funGetUserIdByToken(); + const userLoginId = await funGetUserIdByToken(); - // if (!token) return <>Require Token Storage; + if (!token) return <>Require Token Storage; return ( {/* */} - {/* */} + {children} ); diff --git a/src/app_modules/_global/fun/get/fun_cek_token.ts b/src/app_modules/_global/fun/get/fun_cek_token.ts index f0b16bc0..b25dbcc9 100644 --- a/src/app_modules/_global/fun/get/fun_cek_token.ts +++ b/src/app_modules/_global/fun/get/fun_cek_token.ts @@ -5,7 +5,7 @@ import _ from "lodash"; import { cookies } from "next/headers"; export async function funCheckToken() { - const c = cookies().get("ssn"); + const c = cookies().get("mySession"); const cekToken = await prisma.userSession.findFirst({ where: { token: c?.value, diff --git a/src/app_modules/_global/fun/get/fun_get_user_id_by_token.ts b/src/app_modules/_global/fun/get/fun_get_user_id_by_token.ts index ea8691fd..bafe6a06 100644 --- a/src/app_modules/_global/fun/get/fun_get_user_id_by_token.ts +++ b/src/app_modules/_global/fun/get/fun_get_user_id_by_token.ts @@ -4,7 +4,7 @@ import prisma from "@/app/lib/prisma"; import { cookies } from "next/headers"; export async function funGetUserIdByToken() { - const c = cookies().get("ssn"); + const c = cookies().get("mySession"); const token = c?.value const cekToken = await prisma.userSession.findFirst({ where: { diff --git a/src/app_modules/auth/fun/fun_logout.ts b/src/app_modules/auth/fun/fun_logout.ts index a6d11c63..96dc4111 100644 --- a/src/app_modules/auth/fun/fun_logout.ts +++ b/src/app_modules/auth/fun/fun_logout.ts @@ -18,7 +18,7 @@ export async function auth_Logout() { if (!delToken) return { status: 400, message: "Gagal Hapus User Session" }; cookies().delete({ - name: "ssn", + name: "mySession", }); return { status: 200, message: "Logout Berhasil" }; } catch (error) { diff --git a/src/app_modules/auth/fun/fun_register.ts b/src/app_modules/auth/fun/fun_register.ts index 825c6911..304f94f8 100644 --- a/src/app_modules/auth/fun/fun_register.ts +++ b/src/app_modules/auth/fun/fun_register.ts @@ -47,7 +47,7 @@ export async function Auth_funRegister({ ); cookies().set({ - name: "ssn", + name: "mySession", value: sealToken, maxAge: 60 * 60 * 24 * 7, }); diff --git a/src/app_modules/auth/fun/fun_validasi.ts b/src/app_modules/auth/fun/fun_validasi.ts index 1b8ca56c..c91a19a7 100644 --- a/src/app_modules/auth/fun/fun_validasi.ts +++ b/src/app_modules/auth/fun/fun_validasi.ts @@ -42,7 +42,7 @@ export async function auth_funValidasi({ ); cookies().set({ - name: "ssn", + name: "mySession", value: sealToken, maxAge: 60 * 60 * 24 * 30, }); diff --git a/src/app_modules/fun_global/get_user_token.ts b/src/app_modules/fun_global/get_user_token.ts index 2643a0f0..1ec2a5db 100644 --- a/src/app_modules/fun_global/get_user_token.ts +++ b/src/app_modules/fun_global/get_user_token.ts @@ -10,7 +10,7 @@ import { redirect } from "next/navigation"; export async function user_funGetOneUserId(): Promise { try { const kukis = cookies(); - const c = kukis.get("ssn"); + const c = kukis.get("mySession"); if (!c || !c?.value || _.isEmpty(c?.value) || _.isUndefined(c?.value)) return redirect(RouterAuth.login); diff --git a/src/lib/middleware.ts b/src/lib/middleware.ts index 764ed327..385d0631 100644 --- a/src/lib/middleware.ts +++ b/src/lib/middleware.ts @@ -1,12 +1,27 @@ -import { NextResponse } from "next/server"; -import type { NextRequest } from "next/server"; +import _ from "lodash"; +import { NextRequest, NextResponse } from "next/server"; // This function can be marked `async` if using `await` inside export function middleware(request: NextRequest) { - return NextResponse.redirect(new URL("/home", request.url)); + let cookies = request.cookies.get("mySession"); + console.log(cookies); + + if ( + !cookies || + !cookies?.value || + _.isEmpty(cookies?.value) || + _.isUndefined(cookies?.value) + ) { + return NextResponse.redirect(new URL("/", request.url)); + } + + // return NextResponse.redirect(new URL("/", request.url)); } // See "Matching Paths" below to learn more export const config = { - matcher: "/about/:path*", + matcher: [ + + "/((?!_next|static|favicon.ico|manifest).*)", + ], };