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

bug: can't build docker image with NextAuth.js #992

Closed
vojtechmares opened this issue Dec 19, 2022 · 11 comments
Closed

bug: can't build docker image with NextAuth.js #992

vojtechmares opened this issue Dec 19, 2022 · 11 comments

Comments

@vojtechmares
Copy link

vojtechmares commented Dec 19, 2022

Provide environment information

System:
OS: macOS 13.1
CPU: (10) arm64 Apple M1 Pro
Memory: 111.53 MB / 32.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 19.3.0 - /opt/homebrew/bin/node
npm: 9.2.0 - /opt/homebrew/bin/npm
pnpm: 7.18.2

ct3aMetadata.initVersion: 6.11.2

Describe the bug

Build of my Next app fails due to missing env variables (I am using the SKIP_ENV_VALIDATION=1 env var) and Error: Failed to collect page data for /sign/in

To reproduce

  1. clone https://github.com/vojtechmares/training-platform
  2. checkout to branch ci-docker-build
  3. run docker build -t platform . or SKIP_ENV_VALIDATION=1 pnpm run build

Additional information

I believe that the SKIP_ENV_VALIDATION env var is not being passed to the app.

If I run the pnpn run build outside docker without /.env (/ is project root) file, I get the same error. Altho I was unable to turn off the validation even when I commented out the import in /next.config.mjs.

I believe that the environment schema is checked when page data collection starts, since I was able to get this error for different pages.

Unfortunately I was unable to get around this issue.

I am using Next.js version 13.0.6

@juliusmarminge
Copy link
Member

juliusmarminge commented Dec 20, 2022

Your error isn't from the environment variable validation from next.config (hence turning on the flag wont do anything)

Not sure why it doesn't recognize the custom /sign/in route though...

@vojtechmares
Copy link
Author

vojtechmares commented Dec 20, 2022

From what I googled, it looks like that Next is trying to run getServerSideProps during build time (page collection data?), which results in error.

Unfortunately, I was unable to find out why is this happening and how to fix that. I checked the example from Next documentation for authentication (with NextAuth.js), if I am not missing something and I believe I am not.

I find a discussion at vercel/next.js (see vercel/next.js#35534), yet I did not find a solution to this issue.

EDIT:
I did some more investigations and googling and I think I am confident in the cause. Just no idea how to fix that.

The getServerSideProps method is being called during build time, resulting in chain call up to unstable_getServerSession, which requires authOptions where is then environment used - which I believe is the reason when providing environment variables, everything passes, but when not, it fails. There is the check and I can't do anything about that, from what I see and know. Yet I am not Next/NextAuth/JS expert.

I find it super weird, since this is exactly the way it is shown as an example in NextAuth.js's docs: https://next-auth.js.org/tutorials/securing-pages-and-api-routes#server-side 👨 🔫

@juliusmarminge
Copy link
Member

cc @c-ehrlich @ajcwebdev You have more Docker experience than I have. Any ideas?

@c-ehrlich
Copy link
Member

The gssp => env vars theory sounds plausible. Will try to take a closer look at this sometime soon but might end up being after Christmas.

@vojtechmares
Copy link
Author

@c-ehrlich Ok, thank you. Completely understood, happy holidays 🎄

@vojtechmares
Copy link
Author

bump, cc @c-ehrlich

@vojtechmares
Copy link
Author

reminder cc @c-ehrlich

Can you please have a look? Thank you

@sebakocz
Copy link

sebakocz commented Jan 24, 2023

hey I stumbled across this thread because of invalid NEXTAUTH_URL on my static generated pages when deploying to digital ocean after upgrading my t3 app

what worked for me was changing 'mywebside.com' to 'http://www.mywebside.com' in my env variables

@vojtechmares
Copy link
Author

@sebakocz well I know for sure that the value of NEXTAUTH_URL is invalid, since during build time the env validation should be skipped and not validated and I want to deploy this as a container to Kubernetes, where NEXTAUTH_URL will be provided at runtime.

Your solution does not solve my problem. Thanks tho.

@c-ehrlich
Copy link
Member

Sorry for the delay. I was able to reproduce your issue and then got it working including GSSP. The reason it was breaking is that we were only checking for SKIP_ENV_VALIDATION in next.config.cjs but not in the schema files, which are called directly when building routes with GSSP.

Here's the env schema file of my working repo: https://github.com/c-ehrlich/docker-next-auth/blob/main/src/env.mjs - it's a slightly modified version of what Julius is working on in #1092

Hope it's ok that I built it from scratch instead of using your project, but I'm fairly sure it covers the exact thing you're running into (I copy pasted some bits from your project). Let me know if it works for you :)

@vojtechmares
Copy link
Author

Thanks @c-ehrlich, the issue is now resolved. Thank you for looking into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants