diff --git a/apps/api/app/api/[[...route]]/auth.ts b/apps/api/app/api/[[...route]]/auth.ts new file mode 100644 index 0000000..b1f1886 --- /dev/null +++ b/apps/api/app/api/[[...route]]/auth.ts @@ -0,0 +1,10 @@ +import { auth } from "@repo/auth"; +import { Hono } from "hono"; + +const app = new Hono(); + +app.on(["POST", "GET"], "/**", (c) => { + return auth.handler(c.req.raw); +}); + +export default app; diff --git a/apps/api/app/api/[[...route]]/route.ts b/apps/api/app/api/[[...route]]/route.ts index c5cd4d2..483b2ab 100644 --- a/apps/api/app/api/[[...route]]/route.ts +++ b/apps/api/app/api/[[...route]]/route.ts @@ -1,9 +1,11 @@ import { handle } from "hono/vercel"; import { Hono } from "hono"; -import { auth } from "@repo/auth"; +import { auth as Auth } from "@repo/auth"; import { cors } from "hono/cors"; import mail from "./mail"; import hello from "./hello"; +import session from "./session"; +import auth from "./auth"; const allowedOrigins = [ "http://localhost:3003", @@ -11,12 +13,12 @@ const allowedOrigins = [ "https://app.plura.pro", ]; -export const runtime = "nodejs"; +export const runtime = "edge"; const app = new Hono<{ Variables: { - user: typeof auth.$Infer.Session.user | null; - session: typeof auth.$Infer.Session.session | null; + user: typeof Auth.$Infer.Session.user | null; + session: typeof Auth.$Infer.Session.session | null; }; }>().basePath("/api"); @@ -32,7 +34,7 @@ app.use( }), ); app.use("*", async (c, next) => { - const session = await auth.api.getSession({ headers: c.req.raw.headers }); + const session = await Auth.api.getSession({ headers: c.req.raw.headers }); if (!session) { c.set("user", null); @@ -52,29 +54,10 @@ app.get("/health", async (c) => { }); }); -app.get("/session", async (c) => { - const session = c.get("session"); - const user = c.get("user"); - - if (!user) return c.body(null, 401); - - return c.json({ - session, - user, - }); -}); +app.route("/session", session); app.route("/hello", hello); app.route("/mail", mail); - -app.on(["POST", "GET"], "/auth/**", (c) => { - return auth.handler(c.req.raw); -}); -app.get("/multi-sessions", async (c) => { - const res = await auth.api.listDeviceSessions({ - headers: c.req.raw.headers, - }); - return c.json(res); -}); +app.route("/auth", auth); const GET = handle(app); const POST = handle(app); diff --git a/apps/api/app/api/[[...route]]/session.ts b/apps/api/app/api/[[...route]]/session.ts new file mode 100644 index 0000000..f478e1e --- /dev/null +++ b/apps/api/app/api/[[...route]]/session.ts @@ -0,0 +1,21 @@ +import { Hono } from "hono"; +import { auth } from "@repo/auth"; +const app = new Hono(); +app.get("/", async (c) => { + const session = await auth.api.getSession({ headers: c.req.raw.headers }); + + if (!session) return c.json({ message: "no session found" }, 401); + + return c.json({ + session, + }); +}); + +app.get("/all", async (c) => { + const res = await auth.api.listDeviceSessions({ + headers: c.req.raw.headers, + }); + return c.json(res); +}); + +export default app; diff --git a/apps/www/app/auth/page.tsx b/apps/www/app/auth/page.tsx index 0099fe9..87223e7 100644 --- a/apps/www/app/auth/page.tsx +++ b/apps/www/app/auth/page.tsx @@ -287,21 +287,21 @@ export default function Auth() { +
+ + + +
-
- - - -
diff --git a/packages/auth/package.json b/packages/auth/package.json index b36af5a..38aa855 100644 --- a/packages/auth/package.json +++ b/packages/auth/package.json @@ -12,7 +12,7 @@ "dependencies": { "@repo/typescript-config": "workspace:*", "@repo/db": "workspace:*", - "better-auth": "0.8.5-beta.2", + "better-auth": "0.8.6-beta.3", "better-call": "0.2.14-beta.3", "oslo": "^1.2.1" } diff --git a/packages/database/package.json b/packages/database/package.json index 10f8952..8fafd69 100644 --- a/packages/database/package.json +++ b/packages/database/package.json @@ -5,12 +5,13 @@ "exports": { ".": "./src/index.ts" }, - "scripts": { + "scripts": { "db:generate": "prisma generate", "db:push": "prisma db push --skip-generate" }, "dependencies": { - "@prisma/client": "^5.22.0" + "@prisma/client": "^5.22.0", + "@prisma/extension-accelerate": "^1.2.1" }, "devDependencies": { "prisma": "^5.22.0" diff --git a/packages/database/prisma/schema.prisma b/packages/database/prisma/schema.prisma index 897d4f9..b0ff338 100644 --- a/packages/database/prisma/schema.prisma +++ b/packages/database/prisma/schema.prisma @@ -6,6 +6,7 @@ generator client { datasource db { provider = "postgresql" url = env("DATABASE_URL") + directUrl = env("DIRECT_DATABASE_URL") } model User { diff --git a/packages/database/src/index.ts b/packages/database/src/index.ts index 481d7a9..78a7d85 100644 --- a/packages/database/src/index.ts +++ b/packages/database/src/index.ts @@ -1,11 +1,16 @@ -import { PrismaClient } from "@prisma/client"; +import { PrismaClient } from "@prisma/client/edge"; +import { withAccelerate } from "@prisma/extension-accelerate"; -declare global { - var prisma: PrismaClient | undefined; -} +const prismaClientSingleton = () => { + return new PrismaClient().$extends(withAccelerate()); +}; -export const prisma = global.prisma || new PrismaClient(); +declare const globalThis: { + prismaGlobal: ReturnType; +} & typeof global; -if (process.env.NODE_ENV !== "production") global.prisma = prisma; +export const prisma = globalThis.prismaGlobal ?? prismaClientSingleton(); + +if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = prisma; export * from "@prisma/client"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9935648..5512a0e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -507,8 +507,8 @@ importers: specifier: workspace:* version: link:../typescript-config better-auth: - specifier: 0.8.5-beta.2 - version: 0.8.5-beta.2 + specifier: 0.8.6-beta.3 + version: 0.8.6-beta.3 better-call: specifier: 0.2.14-beta.3 version: 0.2.14-beta.3 @@ -530,6 +530,9 @@ importers: '@prisma/client': specifier: ^5.22.0 version: 5.22.0(prisma@5.22.0) + '@prisma/extension-accelerate': + specifier: ^1.2.1 + version: 1.2.1(@prisma/client@5.22.0(prisma@5.22.0)) devDependencies: prisma: specifier: ^5.22.0 @@ -1989,6 +1992,12 @@ packages: '@prisma/engines@5.22.0': resolution: {integrity: sha512-UNjfslWhAt06kVL3CjkuYpHAWSO6L4kDCVPegV6itt7nD1kSJavd3vhgAEhjglLJJKEdJ7oIqDJ+yHk6qO8gPA==} + '@prisma/extension-accelerate@1.2.1': + resolution: {integrity: sha512-QicnMeyqL226ilT3vvRsFAqPeIdqHGKR4c25CoK5zZ1tNIv8egfgpD1gCKqOGmfAz0pIKQnMuJU3eNg9KItC7A==} + engines: {node: '>=16'} + peerDependencies: + '@prisma/client': '>=4.16.1' + '@prisma/fetch-engine@5.22.0': resolution: {integrity: sha512-bkrD/Mc2fSvkQBV5EpoFcZ87AvOgDxbG99488a5cexp5Ccny+UM6MAe/UFkUC0wLYD9+9befNOqGiIJhhq+HbA==} @@ -3541,8 +3550,8 @@ packages: better-auth@0.8.1-beta.1: resolution: {integrity: sha512-IFBOPOoRooZoJ883UTA5AjpEiTcFCQNecKj5LJytYTv1E+g/F1AibwGMp7jLq2DJJSfuBdtcCn32Fc0qHroSZQ==} - better-auth@0.8.5-beta.2: - resolution: {integrity: sha512-KsyiRmibj15en4wo1tnLe1LbMka6OmGpSPrxjVJSWlcfHI34VmuVuN7CpmcOf5HFeWxGLkUK3ijQj0yhSc6m3Q==} + better-auth@0.8.6-beta.3: + resolution: {integrity: sha512-9V8JB0Sg+Xv/st/zRqyZfXRnc05Ic+7K1v5inlg3KKNuajBUVWsBjAKzBlqhU57qP1EYOIBxMjZ/6JriWvqawQ==} better-call@0.2.14-beta.3: resolution: {integrity: sha512-lA54ETanzM0xUZnQt6lm3BdTr4gVDyAe1DNpCQSTYUnwnGGOmQG8ob0FYivVd0XHsHQK68r01GB5c6cUuu4llQ==} @@ -8489,6 +8498,10 @@ snapshots: '@prisma/fetch-engine': 5.22.0 '@prisma/get-platform': 5.22.0 + '@prisma/extension-accelerate@1.2.1(@prisma/client@5.22.0(prisma@5.22.0))': + dependencies: + '@prisma/client': 5.22.0(prisma@5.22.0) + '@prisma/fetch-engine@5.22.0': dependencies: '@prisma/debug': 5.22.0 @@ -10385,7 +10398,7 @@ snapshots: transitivePeerDependencies: - encoding - better-auth@0.8.5-beta.2: + better-auth@0.8.6-beta.3: dependencies: '@better-fetch/fetch': 1.1.12 '@noble/ciphers': 0.6.0 @@ -11180,7 +11193,7 @@ snapshots: eslint: 8.57.1 eslint-import-resolver-node: 0.3.9 eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1) - eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1) + eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1) eslint-plugin-jsx-a11y: 6.10.2(eslint@8.57.1) eslint-plugin-react: 7.37.2(eslint@8.57.1) eslint-plugin-react-hooks: 5.0.0(eslint@8.57.1) @@ -11200,7 +11213,7 @@ snapshots: eslint: 8.57.1 eslint-import-resolver-node: 0.3.9 eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1) - eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1) + eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1) eslint-plugin-jsx-a11y: 6.10.2(eslint@8.57.1) eslint-plugin-react: 7.37.2(eslint@8.57.1) eslint-plugin-react-hooks: 5.0.0(eslint@8.57.1) @@ -11263,7 +11276,7 @@ snapshots: is-bun-module: 1.2.1 is-glob: 4.0.3 optionalDependencies: - eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1) + eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1) transitivePeerDependencies: - '@typescript-eslint/parser' - eslint-import-resolver-node @@ -11336,7 +11349,7 @@ snapshots: - eslint-import-resolver-webpack - supports-color - eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1): + eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1): dependencies: '@rtsao/scc': 1.1.0 array-includes: 3.1.8