porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.02k stars 255 forks source link

UnhandledSchemeError when using Drizzle ORM with Next.js authentication application #810

Open KennyMwendwaX opened 4 months ago

KennyMwendwaX commented 4 months ago

Description: I am encountering an issue while using postgres.js with Drizzle ORM in a Next.js authentication application. Despite not using Cloudflare Workers in my project, I'm encountering an error related to "cloudflare:sockets", indicating an UnhandledSchemeError.

Here's a summary of the relevant code and context:

// db.ts
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

const client = postgres(process.env.DATABASE_URL!);

const db = drizzle(client, { schema });

export default db;
// auth.ts
import NextAuth, { DefaultSession } from "next-auth";
import { authConfig } from "./auth.config";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import db from "@/database/db";

export const {
  handlers: { GET, POST },
  auth,
  signIn,
  signOut,
} = NextAuth({
  callbacks: {
    async session({ token, session }) {
      // Session callback logic
    },
    async jwt({ token }) {
      // JWT callback logic
      // Issue seems to arise here when interacting with the database
    },
  },
  adapter: DrizzleAdapter(db),
  secret: process.env.NEXTAUTH_SECRET,
  session: { strategy: "jwt" },
});
// middleware.ts
export {auth as default} from "./auth"

The error message received is:

Module build failed: UnhandledSchemeError: Reading from "cloudflare:sockets" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "cloudflare:" URIs.
Import trace for requested module:
cloudflare:sockets
./node_modules/postgres/cf/polyfills.js
./node_modules/postgres/cf/src/index.js
./src/database/db.ts
./src/auth.ts
itsjavi commented 3 months ago

Same here, tried with both node v18 and v20. I am on latest Next.js 14.1 and Drizzle 0.30.4

I think it's because the edge runtime is not supported. I had export runtime = 'edge' in one of my routes using the DB. After removing it it worked.

middleware.ts is always using edge.

da1z commented 2 months ago

same here. i was hoping it supports edge runtime

jckw commented 2 months ago

I just contributed to the bounty on this issue.

Each contribution to this bounty has an expiry time and will be auto-refunded to the contributor if the issue is not solved before then.

Current bounty reward

To make this a public bounty or have a reward split, the maintainer can reply to this comment.

mlntr commented 1 month ago

I have a similar problem in my middleware.ts. My error message is:

[auth][error] AdapterError: Read more at [https://errors.authjs.dev#adaptererror](https://errors.authjs.dev/#adaptererror) 
[auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime 
    at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
    at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
    at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)
[auth][error] SessionTokenError: Read more at [https://errors.authjs.dev#sessiontokenerror](https://errors.authjs.dev/#sessiontokenerror) 
[auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime 
    at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
    at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
    at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)
Filip4130 commented 1 month ago

Just ran into this issue, found a fix. It's somwhere between 2:05:00 and 2:23:00 in https://www.youtube.com/watch?v=1MTyCvS05V4&list=WL&index=58&t=109s&ab_channel=CodeWithAntonio

KennyMwendwaX commented 1 month ago

Just ran into this issue, found a fix. It's somwhere between 2:05:00 and 2:23:00 in https://www.youtube.com/watch?v=1MTyCvS05V4&list=WL&index=58&t=109s&ab_channel=CodeWithAntonio The issue that he solves in that timeframe is no auth secret, i did not see the current issue pop up because he is using the neo db drver instead of the postgres driver

borisyankov commented 15 hours ago

The solution in the video starts at 2:18:49 https://www.youtube.com/watch?v=1MTyCvS05V4&t=8327s