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

crypto for edge runtime #3143

Closed
wants to merge 1 commit into from
Closed

Conversation

artemavrin
Copy link

No description provided.

@charmander
Copy link
Collaborator

globalThis.crypto is already there.

@charmander charmander closed this Feb 7, 2024
@artemavrin
Copy link
Author

ok, I just want to fix this error in nextjs middleware

Error: The edge runtime does not support Node.js 'crypto' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
    at (node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/esm/server/web/globals.js:29:18)
    at (node_modules/.pnpm/[email protected]/node_modules/pg/lib/crypto/utils-webcrypto.js:21:31)
    at (webpack/bootstrap:21:0)
    at (node_modules/.pnpm/[email protected]/node_modules/pg/lib/crypto/utils.js:8:2)
    at (webpack/bootstrap:21:0)
    at (node_modules/.pnpm/[email protected]/node_modules/pg/lib/crypto/sasl.js:2:15)
    at (webpack/bootstrap:21:0)
    at (node_modules/.pnpm/[email protected]/node_modules/pg/lib/client.js:5:11)
    at (webpack/bootstrap:21:0)
    at (node_modules/.pnpm/[email protected]/node_modules/pg/lib/index.js:3:13)

how can we solve this problem?

@constantgillet
Copy link

I also have this error in NextJS middleware

@jimmy-grande
Copy link

globalThis.crypto is already there.

The problem is not that you are not using webcrypto. To make it work on edge runtime, we should get rid of the require statement.

Is it possible to change the way you select the crypto lib by saying

If the global crypto library is available, use it
otherwise use the webcrypto from the node module

which should give something like this:

const webCrypto = globalThis.crypto ?? require('node:crypto').webcrypto

What do you think @charmander ?

@charmander
Copy link
Collaborator

The problem is not that you are not using webcrypto.

Yes, and that’s not the problem with the pull request (which did the equivalent of globalThis.crypto || globalThis.crypto).

Your proposal looks more correct, so feel free to make a pull request.

@ETHANTALJAFFE
Copy link

ETHANTALJAFFE commented Aug 14, 2024

Hey, still having issues with pg in my middleware.js when running nextjs locally.

Even when using [email protected] it seems like it still imports nodejs crypto like this const nodeCrypto = require('crypto').

  • I am using Bun for running locally and installing packages.
  • I tried uninstalling pg, deleting bun.lockb file and reinstalling and I am still seeing the nodejs crypto import inside of the utils-webcrypto file

I noticed that on github in master branch it still shows the old version https://github.com/brianc/node-postgres/blob/master/packages/pg/lib/crypto/utils-webcrypto.js

Is there a reason this was never merged? I can open a pull request with the suggested change

@artemavrin
Copy link
Author

I figured out this issue. We should separate providers and other configuration like this

auth.config.ts

import type { NextAuthConfig } from 'next-auth';

export const authConfig = {
  pages: {
    signIn: '/login',
  },
  providers: [
    // added later in auth.ts since it requires bcrypt which is only compatible with Node.js
    // while this file is also used in non-Node.js environments
  ],
  callbacks: {
    authorized({ auth, request: { nextUrl } }) {
     // your code here ...
      return true;
    },
  },
} satisfies NextAuthConfig;

auth.ts

// imports block
export const { auth, signIn, signOut } = NextAuth({
  ...authConfig, // <- from auth.config.ts
  providers: [
    Credentials({
      async authorize(credentials) {
        // auth code here ...
      },
    }),
  ],
});

middleware.ts

import NextAuth from 'next-auth';
import { authConfig } from './auth.config';

export default NextAuth(authConfig).auth;

export const config = {
  // https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
  matcher: ['/((?!api|_next/static|_next/image|.png).*)'],
};

So you can use pg or drizzle in auth.ts and import other configs in middleware from auth.config.ts

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

Successfully merging this pull request may close these issues.

5 participants