diff --git a/package.json b/package.json index d2d0edb..a14bd42 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,6 @@ { "name": "next-elysia-prisma", "version": "1.0.0", - "type": "module", "private": true, "scripts": { "dev": "next dev --turbopack", diff --git a/src/app/api/[[...route]]/route.ts b/src/app/api/[[...route]]/route.ts index a3c5fea..770b1e0 100644 --- a/src/app/api/[[...route]]/route.ts +++ b/src/app/api/[[...route]]/route.ts @@ -2,6 +2,8 @@ import { authRoute } from "@/server/auth"; import { userRoute } from "@/server/user"; import { Elysia } from "elysia"; +export const dynamic = "force-dynamic"; + /** * Main API router * Combines auth and user routes under the '/api' prefix diff --git a/src/server/auth/index.ts b/src/server/auth/index.ts index 9b19fb9..6ea8f98 100644 --- a/src/server/auth/index.ts +++ b/src/server/auth/index.ts @@ -29,15 +29,18 @@ export const authRoute = new Elysia({ prefix: "/auth" }) }, }); - // Set authentication cookie - (await cookies()).set({ + const cookie: ResponseCookie = { name: serverEnv.AUTH_COOKIE, value: (await encrypt(user))!, path: "/", httpOnly: true, maxAge: serverEnv.SEVEN_DAYS, - }); + }; + // Set authentication cookie + (await cookies()).set(cookie); + console.log("Cookies:", cookie); + return "success"; }, { body: authUser }, // Use authUser schema for request body validation @@ -62,10 +65,10 @@ export const authRoute = new Elysia({ prefix: "/auth" }) httpOnly: true, maxAge: serverEnv.SEVEN_DAYS, }; - - (await cookies()).set({ - ...cookie, - }); + // Set authentication cookie + (await cookies()).set(cookie); + + console.log("Cookies:", cookie); return "success"; }, diff --git a/src/server/user/index.ts b/src/server/user/index.ts index ac25a02..eaefa94 100644 --- a/src/server/user/index.ts +++ b/src/server/user/index.ts @@ -15,10 +15,11 @@ export const userRoute = new Elysia({ prefix: "/user" }).get( // Decrypt user information from the authentication jwt cookie try { const cookieStore = await cookies(); - const allCookies = cookieStore.getAll(); + const headersList = await headers(); + const referer = headersList.get("user-agent"); console.log( "All cookies:", - (await headers()).count, + referer, // Object.values((await headers()).toJSON()), ); const user = await decrypt( @@ -29,8 +30,8 @@ export const userRoute = new Elysia({ prefix: "/user" }).get( return user; } catch (error) { - console.error("Error:", error); - throw new InternalServerError("User not found"); + // console.error("Error:", error); + throw error; } }, ); diff --git a/src/utils/server.ts b/src/utils/server.ts index 7fb615d..3d7684e 100644 --- a/src/utils/server.ts +++ b/src/utils/server.ts @@ -5,17 +5,18 @@ import { serverEnv } from "./env/server"; * get cookie from nextjs header for RPC calls in server components ONLY. * @returns An object containing the cookie header for authentication. */ -export const setCookies = async () => ({ - $headers: { - cookie: await Promise.all( - [serverEnv.AUTH_COOKIE] - .map( - async (cookie) => `${cookie}=${(await cookies()).get(cookie)?.value}`, - ) - .join("; "), - ), - }, -}); +export const setCookies = async () => { + const cookieStore = await cookies(); + const cookie = [serverEnv.AUTH_COOKIE] + .map((name) => { + const value = cookieStore.get(name)?.value; + return value ? `${name}=${value}` : ""; + }) + .filter(Boolean) + .join("; "); + + return { $headers: { cookie } }; +}; /** * Retrieves the server URL from server components ONLY, connected with `middlerware.ts`