-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
crypto for edge runtime #3143
Conversation
|
ok, I just want to fix this error in nextjs middleware
how can we solve this problem? |
I also have this error in NextJS middleware |
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
which should give something like this: const webCrypto = globalThis.crypto ?? require('node:crypto').webcrypto What do you think @charmander ? |
Yes, and that’s not the problem with the pull request (which did the equivalent of Your proposal looks more correct, so feel free to make a pull request. |
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
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 |
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 |
No description provided.