[GH-ISSUE #1953] The edge runtime does not support Node.js 'crypto' module. #8980

Closed
opened 2026-04-13 04:13:42 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @kazmi066 on GitHub (Mar 23, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1953

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Project Stack:

  • Nextjs
  • Better-auth
  • Mongoose/mongodb

I am using the email/password login from better-auth in my NextJs project. Auth works fine until I introduce middleware which runs on Edge Runtime.

Here is the error:
Image

Here is my middleware.ts

export async function middleware(request: NextRequest) {
  const pathname = request.nextUrl.pathname;
  const session = await auth.api.getSession({
    headers: await headers(),
  });
}

Current vs. Expected behavior

Expected Behavior:
Getting the session values inside middleware.

What version of Better Auth are you using?

"next": "^15.1.6", "better-auth": "^1.2.4",

Provide environment information

- OS: Windows 11
- Browser: Chrome

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from 'better-auth';
import { mongodbAdapter } from 'better-auth/adapters/mongodb';
import { MongoClient } from 'mongodb';

const client = new MongoClient(process.env.MONGODB_URI!);
const db = client.db();

export const auth = betterAuth({
  database: mongodbAdapter(db),
  user: {
    modelName: 'users',
    additionalFields: {
      roles: {
        type: 'string[]',
        defaultValue: ['user'],
      },
      image: {
        type: 'string',
        required: false,
      },
      businesses: {
        type: 'string[]',
        required: false,
      },
    },
  },
  databaseHooks: {
    user: {
      create: {
        before: async (user) => {
          return {
            data: {
              ...user,
              roles: ['user'],
              image: null,
              businesses: [],
            },
          };
        },
      },
    },
  },
  emailAndPassword: {
    enabled: true,
  },
});

Additional context

No response

Originally created by @kazmi066 on GitHub (Mar 23, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1953 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Project Stack: - Nextjs - Better-auth - Mongoose/mongodb I am using the `email/password` login from `better-auth` in my `NextJs` project. Auth works fine until I introduce `middleware` which runs on Edge Runtime. Here is the error: ![Image](https://github.com/user-attachments/assets/c41e2cec-1c1f-49ab-858c-acf94933638e) Here is my `middleware.ts` ```ts export async function middleware(request: NextRequest) { const pathname = request.nextUrl.pathname; const session = await auth.api.getSession({ headers: await headers(), }); } ``` ### Current vs. Expected behavior Expected Behavior: Getting the session values inside middleware. ### What version of Better Auth are you using? "next": "^15.1.6", "better-auth": "^1.2.4", ### Provide environment information ```bash - OS: Windows 11 - Browser: Chrome ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth'; import { mongodbAdapter } from 'better-auth/adapters/mongodb'; import { MongoClient } from 'mongodb'; const client = new MongoClient(process.env.MONGODB_URI!); const db = client.db(); export const auth = betterAuth({ database: mongodbAdapter(db), user: { modelName: 'users', additionalFields: { roles: { type: 'string[]', defaultValue: ['user'], }, image: { type: 'string', required: false, }, businesses: { type: 'string[]', required: false, }, }, }, databaseHooks: { user: { create: { before: async (user) => { return { data: { ...user, roles: ['user'], image: null, businesses: [], }, }; }, }, }, }, emailAndPassword: { enabled: true, }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 04:13:42 -05:00
Author
Owner

@moshetanzer commented on GitHub (Mar 23, 2025):

Hey,

Check this out, it should help you ❤

https://www.better-auth.com/docs/integrations/next#for-nextjs-release-1517-and-below

<!-- gh-comment-id:2746342024 --> @moshetanzer commented on GitHub (Mar 23, 2025): Hey, Check this out, it should help you ❤ https://www.better-auth.com/docs/integrations/next#for-nextjs-release-1517-and-below
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8980