ReferenceError: crypto is not defined #469

Closed
opened 2026-03-13 07:48:44 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @sarthakkimtani on GitHub (Dec 25, 2024).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Create auth.ts in your express app

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";

import prisma from "./prisma.js";

export const auth = betterAuth({
  baseURL: process.env.BETTER_AUTH_URL,
  secret: process.env.BETTER_AUTH_SECRET,
  trustedOrigins: [process.env.ORIGIN_URL as string],
  database: prismaAdapter(prisma, {
    provider: "postgresql",
  }),
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
    },
    github: {
      clientId: process.env.GITHUB_CLIENT_ID as string,
      clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
    },
  },
});

Mount auth to all auth routes

import { toNodeHandler } from "better-auth/node";
import { auth } from "./lib/auth.js";

app.all("/api/auth/*", toNodeHandler(auth));

Current vs. Expected behavior

The above code on the backend works perfectly when authenticating using GitHub, However, when I click on the Sign in with Google Button in NextJS. I get the following error in the express app:

ERROR [Better Auth]: ReferenceError ReferenceError: crypto is not defined
    at Object.digest (/node_modules/.pnpm/@better-auth+utils@0.2.2/node_modules/@better-auth/utils/dist/hash.mjs:8:26)
    at ft (node_modules/.pnpm/better-auth@1.1.3/node_modules/better-auth/dist/index.js:2:13474)
    at O (/node_modules/.pnpm/better-auth@1.1.3/node_modules/better-auth/dist/index.js:2:14156)
    at Object.createAuthorizationURL (node_modules/.pnpm/better-auth@1.1.3/node_modules/better-auth/dist/index.js:2:24080)
    at /node_modules/.pnpm/better-auth@1.1.3/node_modules/better-auth/dist/index.js:3:5129
    at async handle (/node_modules/.pnpm/better-call@0.3.3-beta.4/node_modules/better-call/dist/index.js:378:17)
    at async We.d.<computed> (/node_modules/.pnpm/better-auth@1.1.3/node_modules/better-auth/dist/index.js:83:10078)
    at async handler (/node_modules/.pnpm/better-call@0.3.3-beta.4/node_modules/better-call/dist/index.js:594:26)
    at async handler (/node_modules/.pnpm/better-call@0.3.3-beta.4/node_modules/better-call/dist/index.js:645:19)
    at async /node_modules/.pnpm/better-call@0.3.3-beta.4/node_modules/better-call/dist/index.js:847:22

When I check the frontend the following POST request fails with a 500 error:

POST http://localhost:8000/api/auth/sign-in/social?currentURL=http://localhost:3000/auth

What version of Better Auth are you using?

1.1.3

Provide environment information

- OS: MacOS 15.2
- Node: 18.20.4
- pnpm: 9.12.3

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

Backend, Package

Auth config (if applicable)

No response

Additional context

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "outDir": "./dist",
    "sourceMap": true,
    "strict": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "ts-node": {
    "esm": true
  },
  "include": ["src/**/*"]
}
Originally created by @sarthakkimtani on GitHub (Dec 25, 2024). ### Is this suited for github? - [X] Yes, this is suited for github ### To Reproduce Create `auth.ts` in your express app ```javascript import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import prisma from "./prisma.js"; export const auth = betterAuth({ baseURL: process.env.BETTER_AUTH_URL, secret: process.env.BETTER_AUTH_SECRET, trustedOrigins: [process.env.ORIGIN_URL as string], database: prismaAdapter(prisma, { provider: "postgresql", }), socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID as string, clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, }, github: { clientId: process.env.GITHUB_CLIENT_ID as string, clientSecret: process.env.GITHUB_CLIENT_SECRET as string, }, }, }); ``` Mount `auth` to all auth routes ```javascript import { toNodeHandler } from "better-auth/node"; import { auth } from "./lib/auth.js"; app.all("/api/auth/*", toNodeHandler(auth)); ``` ### Current vs. Expected behavior The above code on the backend works perfectly when authenticating using GitHub, However, when I click on the Sign in with Google Button in NextJS. I get the following error in the express app: ``` ERROR [Better Auth]: ReferenceError ReferenceError: crypto is not defined at Object.digest (/node_modules/.pnpm/@better-auth+utils@0.2.2/node_modules/@better-auth/utils/dist/hash.mjs:8:26) at ft (node_modules/.pnpm/better-auth@1.1.3/node_modules/better-auth/dist/index.js:2:13474) at O (/node_modules/.pnpm/better-auth@1.1.3/node_modules/better-auth/dist/index.js:2:14156) at Object.createAuthorizationURL (node_modules/.pnpm/better-auth@1.1.3/node_modules/better-auth/dist/index.js:2:24080) at /node_modules/.pnpm/better-auth@1.1.3/node_modules/better-auth/dist/index.js:3:5129 at async handle (/node_modules/.pnpm/better-call@0.3.3-beta.4/node_modules/better-call/dist/index.js:378:17) at async We.d.<computed> (/node_modules/.pnpm/better-auth@1.1.3/node_modules/better-auth/dist/index.js:83:10078) at async handler (/node_modules/.pnpm/better-call@0.3.3-beta.4/node_modules/better-call/dist/index.js:594:26) at async handler (/node_modules/.pnpm/better-call@0.3.3-beta.4/node_modules/better-call/dist/index.js:645:19) at async /node_modules/.pnpm/better-call@0.3.3-beta.4/node_modules/better-call/dist/index.js:847:22 ``` When I check the frontend the following POST request fails with a 500 error: <br> ``` POST http://localhost:8000/api/auth/sign-in/social?currentURL=http://localhost:3000/auth ``` ### What version of Better Auth are you using? 1.1.3 ### Provide environment information ```bash - OS: MacOS 15.2 - Node: 18.20.4 - pnpm: 9.12.3 ``` ### Which area(s) are affected? (Select all that apply) Backend, Package ### Auth config (if applicable) _No response_ ### Additional context `tsconfig.json` ```json { "compilerOptions": { "target": "ES2020", "module": "NodeNext", "moduleResolution": "NodeNext", "outDir": "./dist", "sourceMap": true, "strict": true, "emitDecoratorMetadata": true, "experimentalDecorators": true }, "ts-node": { "esm": true }, "include": ["src/**/*"] } ```
GiteaMirror added the bug label 2026-03-13 07:48:44 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#469