Skip to content

Commit

Permalink
easy
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Nov 22, 2024
1 parent fd90d78 commit 8cec72d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "next-elysia-prisma",
"version": "1.0.0",
"type": "module",
"private": true,
"scripts": {
"dev": "next dev --turbopack",
Expand Down
2 changes: 2 additions & 0 deletions src/app/api/[[...route]]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions src/server/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
},
Expand Down
9 changes: 5 additions & 4 deletions src/server/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<User>(
Expand All @@ -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;
}
},
);
23 changes: 12 additions & 11 deletions src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 8cec72d

Please sign in to comment.