Hebilicious / authjs-nuxt

AuthJS edge-compatible authentication Nuxt module.
https://authjs-nuxt.pages.dev/
MIT License
249 stars 28 forks source link

SignIn gives TypeError: crypto.getRandomValues is not a function #99

Closed Q16solver closed 1 year ago

Q16solver commented 1 year ago

Environment

Reproduction

N/A

Describe the bug

Running signIn function in production gives me a crypto module error for some reason

image

Additional context

No response

Logs

[auth][error][SignInError]: Read more at https://errors.authjs.dev#signinerror

[auth][cause]: TypeError: crypto.getRandomValues is not a function

at randomBytes (file:///app/.output/server/node_modules/oauth4webapi/build/index.js:237:24)

at Module.generateRandomCodeVerifier (file:///app/.output/server/node_modules/oauth4webapi/build/index.js:240:12)

at Object.create (file:///app/.output/server/node_modules/@auth/core/lib/oauth/checks.js:23:33)

at getAuthorizationUrl (file:///app/.output/server/node_modules/@auth/core/lib/oauth/authorization-url.js:57:57)

at processTicksAndRejections (node:internal/process/task_queues:96:5)

at async Module.signin (file:///app/.output/server/node_modules/@auth/core/lib/routes/signin.js:15:20)

at async AuthInternal (file:///app/.output/server/node_modules/@auth/core/lib/index.js:112:36)

at async Auth (file:///app/.output/server/node_modules/@auth/core/index.js:100:30)

at async file:///app/.output/server/chunks/nitro/node-server.mjs:7238:22

at async Object.handler (file:///app/.output/server/chunks/nitro/node-server.mjs:2380:19)

[auth][details]: {

"provider": "google"

}
Hebilicious commented 1 year ago

What is your production environment ? It should support crypto.getRandomValues as it's an auth.js requirement. This should be polyfilled by unenv or by this module directly.

Q16solver commented 1 year ago

I'm using railway to deploy which has node v16 and npm v9. Though yeah, interestingly enough, it does say in the logs that it's polyfilled crypto module.

> nuxt preview

[log]

╭───────────────────────────────────Preview Mode─────────────────────────────────────╮

│                                                                                    │

│  You are running Nuxt production build in preview mode.                            │

│  For production deployments, please directly use node ./server/index.mjs command.  │

│                                                                                    │

│  Node.js:           v16.18.1                                                       │

│  Nitro Preset:      node-server                                                    │

│  Working directory: .output                                                        │

│                                                                                    │

╰────────────────────────────────────────────────────────────────────────────────────╯

[info] Loading `.env`. This will not be loaded when running the server in production.

[info] Starting preview command: `node ./server/index.mjs`

[log]

{

severity_local: 'NOTICE',

severity: 'NOTICE',

code: '42P06',

message: 'schema "drizzle" already exists, skipping',

file: 'schemacmds.c',

line: '118',

routine: 'CreateSchemaCommand'

}

{

severity_local: 'NOTICE',

severity: 'NOTICE',

code: '42P07',

message: 'relation "__drizzle_migrations" already exists, skipping',

file: 'parse_utilcmd.c',

line: '209',

routine: 'transformCreateStmt'

}

Polyfilling crypto...

Listening on http://[::]:6072

WebSocket Server is listening

Removing session
Hebilicious commented 1 year ago

I'm using railway to deploy which has node v16 and npm v9. Though yeah, interestingly enough, it does say in the logs that it's polyfilled crypto module.

> nuxt preview

[log]

╭───────────────────────────────────Preview Mode─────────────────────────────────────╮

│                                                                                    │

│  You are running Nuxt production build in preview mode.                            │

│  For production deployments, please directly use node ./server/index.mjs command.  │

│                                                                                    │

│  Node.js:           v16.18.1                                                       │

│  Nitro Preset:      node-server                                                    │

│  Working directory: .output                                                        │

│                                                                                    │

╰────────────────────────────────────────────────────────────────────────────────────╯

[info] Loading `.env`. This will not be loaded when running the server in production.

[info] Starting preview command: `node ./server/index.mjs`

[log]

{

severity_local: 'NOTICE',

severity: 'NOTICE',

code: '42P06',

message: 'schema "drizzle" already exists, skipping',

file: 'schemacmds.c',

line: '118',

routine: 'CreateSchemaCommand'

}

{

severity_local: 'NOTICE',

severity: 'NOTICE',

code: '42P07',

message: 'relation "__drizzle_migrations" already exists, skipping',

file: 'parse_utilcmd.c',

line: '209',

routine: 'transformCreateStmt'

}

Polyfilling crypto...

Listening on http://[::]:6072

WebSocket Server is listening

Removing session

The polyfill is something that I tried to do there https://github.com/Hebilicious/authjs-nuxt/blob/2100f25fd7de00095e00b330db3c3e29c22d1209/packages/authjs-nuxt/src/runtime/lib/server.ts#L9 , although I haven't tested it under all conditions, and it looks like it isn't working with node 16.

For railway, it looks like you can use a more recent version of node, or even try out bun https://docs.railway.app/deploy/builds . Btw Node 16 as reached EOL so you should not use it anymore.

I would also recommend that you never run your production app with Nuxt preview, instead run the built version with node .output/server/index.mjs https://nuxt.com/docs/getting-started/deployment

Q16solver commented 1 year ago

Yeah, seems like that was the issue, updated to use node 18 and works now, I'll also update to run the js file directly, thanks for the advice c: