Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kenny-mwendwa opened this issue Feb 17, 2024 · 6 comments

Comments

@kenny-mwendwa
Copy link

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
Copy link

itsjavi commented Mar 25, 2024

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
Copy link

da1z commented Apr 12, 2024

same here. i was hoping it supports edge runtime

@jckw
Copy link

jckw commented Apr 17, 2024

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
Copy link

mlntr commented May 19, 2024

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
Copy link

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

@kenny-mwendwa
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants