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 140970c commit 2821d7b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 44 deletions.
47 changes: 36 additions & 11 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
export default {
root: true,
parser: "@typescript-eslint/parser",
extends: "next/core-web-vitals",
overrides: [
{
files: ["**/*.tsx"],
rules: {
"react-hooks/exhaustive-deps": "off",
import js from "@eslint/js";
import nextPlugin from "@next/eslint-plugin-next";
import tsParser from "@typescript-eslint/parser";
import reactHooksPlugin from "eslint-plugin-react-hooks";
import globals from "globals";

export default [
js.configs.recommended,
{
files: ["**/*.{js,jsx,ts,tsx}"],
languageOptions: {
parser: tsParser,
globals: {
...globals.browser,
...globals.es2021,
...globals.node,
React: "readonly",
},
},
],
};
plugins: {
"@next/next": nextPlugin,
"react-hooks": reactHooksPlugin,
},
rules: {
"react-hooks/exhaustive-deps": "off",
"no-unused-vars": [
"warn",
{
varsIgnorePattern: "^(NodeJS|other)$",
ignoreRestSiblings: true,
args: "none",
caughtErrors: "none",
},
],
...nextPlugin.configs.recommended.rules,
},
},
];
7 changes: 5 additions & 2 deletions next.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
/* config options here */
};

export default nextConfig;
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "next-elysia-prisma",
"version": "1.0.0",
"type": "module",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "next dev --turbopack",
"build": "next build",
"start": "prisma migrate deploy && next start",
"lint": "next lint",
Expand All @@ -17,15 +18,15 @@
"elysia": "^1.1.25",
"jose": "^5.9.6",
"next": "15.0.3",
"react": "^18",
"react-dom": "^18"
"react": "19.0.0-rc-66855b96-20241106",
"react-dom": "19.0.0-rc-66855b96-20241106"
},
"devDependencies": {
"@types/bun": "^1.1.13",
"@types/node": "^22.9.1",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint": "^9.15.0",
"eslint-config-next": "15.0.3",
"postcss": "^8",
"prettier": "^3.3.3",
Expand All @@ -34,4 +35,4 @@
"tailwindcss": "^3.4.15",
"typescript": "^5"
}
}
}
7 changes: 6 additions & 1 deletion src/server/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import prisma from "@/lib/prisma";
import { authUser } from "@/lib/typebox/auth";
import { serverEnv } from "@/utils/env/server";
import { Elysia, InternalServerError } from "elysia";
import { ResponseCookie } from "next/dist/compiled/@edge-runtime/cookies";
import { cookies } from "next/headers";

/**
Expand Down Expand Up @@ -54,12 +55,16 @@ export const authRoute = new Elysia({ prefix: "/auth" })

if (!user) throw new InternalServerError("User not found");

(await cookies()).set({
const cookie: ResponseCookie = {
name: serverEnv.AUTH_COOKIE,
value: (await encrypt(user))!,
path: "/",
httpOnly: true,
maxAge: serverEnv.SEVEN_DAYS,
};

(await cookies()).set({
...cookie,
});

return "success";
Expand Down
24 changes: 18 additions & 6 deletions src/server/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { decrypt } from "@/lib/jwt";
import { serverEnv } from "@/utils/env/server";
import { User } from "@prisma/client";
import { Elysia, InternalServerError } from "elysia";
import { cookies } from "next/headers";
import { cookies, headers } from "next/headers";

/**
* User route for retrieving the current user's information.
Expand All @@ -13,12 +13,24 @@ export const userRoute = new Elysia({ prefix: "/user" }).get(
"/me",
async (ctx) => {
// Decrypt user information from the authentication jwt cookie
const user = await decrypt<User>(
(await cookies()).get(serverEnv.AUTH_COOKIE)?.value,
);
try {
const cookieStore = await cookies();
const allCookies = cookieStore.getAll();
console.log(
"All cookies:",
(await headers()).count,
// Object.values((await headers()).toJSON()),
);
const user = await decrypt<User>(
(await cookies()).get(serverEnv.AUTH_COOKIE)?.value,
);

if (!user) throw new InternalServerError("User not found");
if (!user) throw new InternalServerError("User not found");

return user;
return user;
} catch (error) {
console.error("Error:", error);
throw new InternalServerError("User not found");
}
},
);
25 changes: 6 additions & 19 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"compilerOptions": {
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
Expand All @@ -22,19 +19,9 @@
}
],
"paths": {
"@/*": [
"./src/*"
]
},
"target": "ES2017"
"@/*": ["./src/*"]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 2821d7b

Please sign in to comment.