Skip to content
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

D1 configuration page includes non-working examples #12044

Open
britt opened this issue Oct 14, 2024 · 7 comments · May be fixed by #12164
Open

D1 configuration page includes non-working examples #12044

britt opened this issue Oct 14, 2024 · 7 comments · May be fixed by #12164
Labels
good first issue Good issue to take for first time contributors

Comments

@britt
Copy link

britt commented Oct 14, 2024

What is the improvement or update you wish to see?

I would like information on how to initialize Auth.js using D1 that helps me deal with the fact that I can't get access to any of the environment bindings outside of a request.

The example given doesn't compile or work.

Is there any context that might help us understand?

The setup procedure for the D1 Adpater on https://authjs.dev/getting-started/adapters/d1#configuration includes code examples that don't work. In the example below env is undefined.

import NextAuth from "next-auth"
import { D1Adapter } from "@auth/d1-adapter"
 
export const { handlers, auth, signIn, signOut } = NextAuth({
  providers: [],
  adapter: D1Adapter(env.db),
})

None of the packages for running Next.js on Cloudflare, neither workers nor pages, provide access to the Cloudflare Env and bindings ourside the context of a request.

Does the docs page already exist? Please link to it.

https://authjs.dev/getting-started/adapters/d1#configuration

@britt britt added the triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. label Oct 14, 2024
@balazsorban44 balazsorban44 added good first issue Good issue to take for first time contributors and removed triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Oct 15, 2024
@balazsorban44
Copy link
Member

Should use the convention of your framework. eg process.env in Next.js.

@britt
Copy link
Author

britt commented Oct 15, 2024

It's not process.env that it needs to access. It's the Cloudflare workers bindings which are also exposed through a variable named env. It also does not change the fact that the example shown in the docs will not compile.

@rskbansal
Copy link

rskbansal commented Oct 16, 2024

env is de-structured from context, which is passed in by the Cloudflare runtime every time a request is made.
You may refer this - https://developers.cloudflare.com/d1/get-started/#write-queries-within-your-worker

export const onRequest = async (context) => {
  const { request, env } = context;
  
  return NextAuth({
    providers: [],
    adapter: D1Adapter(env.db),
  })(request);
};

You may try using the above snippet once & let me know if it works.

@Ross-Fan
Copy link

I have the same issue with the D1adapter, the original code doesn't work. When I change the code to process.env.db, it's not work neither.
20241017-222307
In my view, the D1Adapter (function D1Adapter(db: D1Database)) needs a D1Database object, not the configuration 'env.DB'
btw, the D1 could work in my nextjs app, I test in submit some text to D1, it's ok

@Ross-Fan
Copy link

It's not process.env that it needs to access. It's the Cloudflare workers bindings which are also exposed through a variable named env. It also does not change the fact that the example shown in the docs will not compile.

hi, britt
I have some problems when using next-auth in my nextjs+cloudflarePages+D1, it's about the D1 adapter,
截屏2024-10-17 22 51 46
It's my code in auth.ts , but it not working
Could you please provide guidance on how to configure the D1Adapter? I would greatly appreciate your help. Thank you!

@lane711
Copy link

lane711 commented Nov 1, 2024

I am stuck on this as well. I thought I almost had it working with the below code as it does give me access to the D1 instances but getting the error "vite_ssr_import_0.default is not a function"

src/middleware/index.ts

import NextAuth from "next-auth";
import { D1Adapter } from "@auth/d1-adapter";
import Email from "@auth/core/providers/email";

export function onRequest(context, next) {
  const env = context.locals.runtime.env;

  const auth = NextAuth({
    providers: [Email],
    adapter: D1Adapter(env.D1),
  })(context.request);

  return next();
}

@krismartin krismartin linked a pull request Nov 1, 2024 that will close this issue
3 tasks
@krismartin
Copy link

I managed to get the D1 adapter working on a next app and opened a PR to update the documentation #12164

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good issue to take for first time contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants