next-auth: Dynamo adapter can't deploy to edge

Environment

I’m using SvelteKit so I ran

npx envinfo --system --binaries --browsers --npmPackages "@sveltejs/kit,svelte,@auth/*"
  System:
    OS: macOS 13.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 35.95 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.11.1 - /usr/local/Cellar/nvm/0.38.0/versions/node/v16.11.1/bin/node
    Yarn: 1.22.17 - /usr/local/Cellar/nvm/0.38.0/versions/node/v16.11.1/bin/yarn
    npm: 9.3.0 - ~/Documents/git/booklist/svelte-kit/node_modules/.bin/npm
  Browsers:
    Chrome: 109.0.5414.119
    Firefox: 109.0.1

Reproduction URL

Not quite a repro, but this shows the error I’m reporting:

https://vercel.com/adam-rackis/booklist-svelte-kit/9KDXU5pMSbXbNsHS3nvk8MKNrqdi

EDIT - repro is here: https://github.com/arackaf/dynamo-adapter-edge-deploy

npm i then npm run build and there should be an error about crypto not existing. See comments below for more info

Describe the issue

When trying to deploy to edge with Vercel, the dynamo adapter causes things to fail

Error: Build failed with 2 errors: – 00:37:29.818 | .svelte-kit/output/server/entries/endpoints/api/cover-upload/_server.ts.js:1:17: ERROR: Could not resolve “path” 00:37:29.818 | node_modules/@next-auth/dynamodb-adapter/dist/index.js:4:25: ERROR: Could not resolve “crypto” 00:37:29.818 | at failureErrorWithLog (/vercel/path0/svelte-kit/node_modules/esbuild/lib/main.js:1604:15)

How to reproduce

Use the dynamo adapter, and try to deploy to the edge (with SvelteKit and the vercel adapter, but presumably with Next, too)

Expected behavior

Ideally it would be great if this worked. Is it possible for the adapter to use the web crypto api, instead of Node’s?

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 24 (10 by maintainers)

Most upvoted comments

@ThangHuuVu hmmm - not sure what repro of mine you added to. When I upgrade to experimental.1 and run npm run build I get this error

Error [ERR_MODULE_NOT_FOUND]: Cannot find module ‘/Users/arackis/Documents/git/booklist/svelte-kit/node_modules/@next-auth/dynamodb-adapter/utils’ imported from /Users/arackis/Documents/git/booklist/svelte-kit/node_modules/@next-auth/dynamodb-adapter/index.js

When I just run dev and try to run, the import

import { DynamoDBAdapter } from "@next-auth/dynamodb-adapter";

is undefined. Looking in the deploy of @next-auth/dynamodb-adapter it looks like the utils folder wasn’t published, but is referenced by the exports field in package.json

yeah, it seems to be an internal package. but I think there’s a good reason to get a new version of this package out anyway, will try to do it soon-ish, and you can test if it will work. 🙏

Agreed! I let the SvelteKit team now. 🙏

Well, we published 2.0.0 by accident and did not revert, and our release bot figured since we are doing breaking changes, it should go to 3.0.0 instead. 🤷

I can add a note about this in the release notes, thanks!

@balazsorban44 these Dynamo libs appear to work fine - this modified hooks file does build successfully.

I’m not familiar with the smithy-client package - is that used internally to the dynamo client/lib packages? I’m happy to install and import it and see if it still builds

import { sequence } from '@sveltejs/kit/hooks';

import { SvelteKitAuth } from '@auth/sveltekit';
import { DynamoDB } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
import GoogleProvider from '@auth/core/providers/google';

const dynamoConfig = {};

export const client = DynamoDBDocument.from(new DynamoDB(dynamoConfig), {
	marshallOptions: {
		convertEmptyValues: true,
		removeUndefinedValues: true,
		convertClassInstanceToMap: true
	}
});

const auth = SvelteKitAuth({
	providers: [
		// @ts-ignore
		GoogleProvider({
			clientId: 'GOOGLE_AUTH_CLIENT_ID',
			clientSecret: 'GOOGLE_AUTH_SECRET'
		})
	],
	session: {
		maxAge: 60 * 60 * 24 * 365,
		strategy: 'jwt'
	},

	secret: 'NEXTAUTH_SECRET'
});

export async function handleFn({ event, resolve }) {
	if (event.url.pathname.startsWith('/custom')) {
		return new Response('custom response');
	}

	const response = await resolve(event);
	return response;
}

export const handle = sequence(handleFn, auth);

@balazsorban44 good question - this would all be for naught of the underlying aws utils aren’t compatible.

Lemme check that now …

Really sorry - after pulling this into a minimal repro, the problem is my path import, not crypto being used in dynamo-adapter. That’s pretty obvious now, looking at it again. But geez the stack trace is easily misread.

image