nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
22.49k stars 2.98k forks source link

Cannot leverage platform attribute in SvelteKit during sign-in #10779

Open DakotaLarson opened 2 weeks ago

DakotaLarson commented 2 weeks ago

Environment

  System:
    OS: Windows 10 10.0.19045
    CPU: (4) x64 Intel(R) Core(TM) i5-6600K CPU @ 3.50GHz
    Memory: 5.84 GB / 15.94 GB
  Binaries:
    Node: 22.0.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.5.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (123.0.2420.97)
    Internet Explorer: 11.0.19041.3636
  npmPackages:
    @auth/sveltekit: ^1.0.1 => 1.0.1

Reproduction URL

https://github.com/DakotaLarson/authjs-bug

Describe the issue

When I sign in with google using SvelteKit & Auth.js, the event captured in the hook does not have the platform attribute present. I am hoping to leverage Cloudflare D1, which exposes an instance of the D1Database on the platform attribute under most requests. I need an instance of the D1Database to instantiate prisma client with the prisma d1 adapter, which takes the D1Database as an argument.

I have been getting around this issue using getPlatformProxy, a helper function from the wrangler package that returns the necessary data. This package is not compatible with cloudflare workers, so I need to get the D1Database object from another source.

How to reproduce

To reproduce, clone the authjs-bug repo that I linked, and update wrangler.toml with a real d1 database id. If you have wrangler cli installed, the command is wrangler d1 create . Then run npm run dev. If you navigate to localhost:5173, you should see a console statement indicating the platform attribute is present on the event captured by the hook. If you go through the sign in flow, you should see another console statement for the sign in event, which shows the platform attribute not being present.

Expected behavior

I expect the platform attribute to be present on the event captured by the hook.

DakotaLarson commented 2 weeks ago

Related discussion on Discord: https://discord.com/channels/1200116961590399008/1234528177246830734/1234528177246830734

ThangHuuVu commented 2 weeks ago

It seems like a binding issue with @sveltejs/adapter-cloudflare. Are you able to retrieve the platform attributes without using Auth.js, via a normal hook function?

nathanblair commented 2 weeks ago

Most of my reproductions and investigations lead me to believe this may indeed be a binding issue with the svelte cloudflare adapter. The event.platform is not populated at server-callback time either. @DakotaLarson I'll open a PR on your repro repo to help demonstrate that even in a raw sveltekit server hook handle function the event.platform attribute is empty.

DakotaLarson commented 2 weeks ago

Appreciate your contribution! I just merged it in and am seeing that event.platform is indeed empty with a normal hook function. The only offering I have currently is this try on Discord. Forcing prerender = false seemed to hydrate the platform attribute for sign in requests. Functionality was still broken, but it was interesting to me. Will update here if I find anything interesting.

nathanblair commented 2 weeks ago

@ThangHuuVu - I was able to fix the issue. I'm not sure if what I've done is the most idiomatic way to achieve the desired behavior though.

I think what is needed is perhaps just some documentation updates regarding the expected configuration for the how auth callbacks work with sveltekit and auth.js.

Can you take a look over the repro repo and see what you think? If need be, I can look into updating the relevant sveltekit-framework documentation.