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

auth.js tries to import $env/dynamic/private in SvelteKit frontend #11166

Open
ssebastianoo opened this issue Jun 14, 2024 · 7 comments
Open
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@ssebastianoo
Copy link

Environment

 System:
    OS: macOS 15.0
    CPU: (8) arm64 Apple M2
    Memory: 103.59 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.9.0 - /usr/local/bin/node
    Yarn: 1.22.21 - /usr/local/bin/yarn
    npm: 10.5.1 - /usr/local/bin/npm
    pnpm: 9.0.3 - /usr/local/bin/pnpm
    bun: 1.1.12 - ~/.bun/bin/bun
  Browsers:
    Chrome: 126.0.6478.61
    Safari: 18.0
  npmPackages:
    @auth/sveltekit: ^1.2.0 => 1.2.0 

Reproduction URL

https://github.com/ssebastianoo/SelfStats/blob/authjs-bug/src/routes/signin/%2Bpage.svelte

Describe the issue

When trying to set up a custom sign in page the authjs library for SvelteKit tries, for some reason, to import { env } from "$env/dynamic/private"; in the client, breaking the whole app

image

image

How to reproduce

Add a custom login page following the docs

Expected behavior

$env/dynamic/private shouldn't be imported on the client

@ssebastianoo ssebastianoo added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Jun 14, 2024
@Conceptiks
Copy link

Conceptiks commented Jul 3, 2024

I have the same issue here. @ssebastianoo Could you add back your reproduction branch? I think you deleted it by accident.

Edit:
If you didn't get this to work, this bug is caused by the inclusion of the import { env } from "$env/dynamic/private" in the file you mentioned. This file is imported by the auth.js file that (as per the guide you followed) contains this:

// lib/auth.ts
const providers: Provider[] = [
  GitHub,
  Credentials({
    credentials: { password: { label: "Password", type: "password" } },
    authorize(c) {
      if (c.password !== "password") return null
      return {
        id: "test",
        name: "Test User",
        email: "[email protected]",
      }
    },
  }),
]
 
export const providerMap = providers.map((provider) => {
  if (typeof provider === "function") {
    const providerData = provider()
    return { id: providerData.id, name: providerData.name }
  } else {
    return { id: provider.id, name: provider.name }
  }
})
 
export const { handle, signIn, signOut } = SvelteKitAuth({
  providers,
  pages: {
    signIn: "/signin",
  },
})

If you now import providerMap (or anything else exported by this file) inside a client route, this will attempt to load $env/dynamic/private from the @auth/sveltekit/dist/index.js.

Fix this by simply not consuming anything from the auth.ts file in your client routes, for me, this was as simple as not using the providerMap and hard coding <SignIn provider="" /> in my client.

@ssebastianoo
Copy link
Author

ssebastianoo commented Jul 3, 2024

@Conceptiks yeah I deleted the branch forgetting about this issue.
I solved it the same way you did, hardcoding it

Do you still need the branch? I can try to recreate it

@ssebastianoo
Copy link
Author

Even if I solved it I'm not gonna close it because this is still a bug that pops up following the official documentation

@Conceptiks
Copy link

Totally agree, this should not be happening.

@rishi-raj-jain
Copy link

Screenshot 2024-07-10 at 12 20 24 AM Screenshot 2024-07-10 at 12 20 55 AM Screenshot 2024-07-10 at 12 20 44 AM

here's how you can load on the server-side and then load in the frontend.

@ssebastianoo
Copy link
Author

that's actually so smart

@Padi2312
Copy link

Will this ever be fixed ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

4 participants