Next-auth: Does the `site` option still exist?

Created on 15 Sep 2020  Â·  6Comments  Â·  Source: nextauthjs/next-auth

Your question
I'm wondering if the site field in options exists such as is referenced in #423 ?

What are you trying to do
I'm deploying on Vercel and am attempting to set NEXTAUTH_URL dynamically based on the current deployment URL. Vercel exposes (VERCEL_URL)[https://vercel.com/docs/build-step?query=environment#system-environment-variables] which holds the current deployment URL. I was going to pass this to the site option to dynamically set the redirect/callback URL based on the deployment.

I'm also trying to find if Vercel supports setting an env var to equal another, i.e. I could just do NEXTAUTH_URL=VERCEL_URL, but I haven't found this yet.

Feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • [ ] Found the documentation helpful
  • [ ] Found documentation but was incomplete
  • [x] Could not find relevant documentation
  • [ ] Found the example project helpful
  • [ ] Did not find the example project helpful
question

All 6 comments

The site field has been removed in v3 and has been replaced with NEXTAUTH_URL. I'm not sure about the dynamic URL @iaincollins?

I'm actually not sure this is even relevant because I would also need to have the hostname/callback set up to support those dynamic ones on the OAuth side of things (for OAuth providers). So it probably doesn't make sense to do this anyways

Oh interesting! That’s great to know, I just need to unset that variable on my other deployments 👌 thanks!

Hi there!

The site and baseUrl options were removed in v3 and replaced with the NEXTAUTH_URL environment variable.

The primary reason for this was that, as pages and API routes run as isolated functions under Serverless, there wasn't a practical way to share configuration for the canonical site URL between them and that the approach in v2 (of using secure cookie with a payload to handle basic URL configuration) didn't work 100% of the time.

I'd wanted to avoid using an environment variable for something like this, but the only other option would be to split configuration off into a separate file and include it in both the front end and the back end. However, this would require MULTIPLE configuration files (as you wouldn't want things like SECRET or CLIENT ID values leaking into the front end bundle) so wasn't a great solution either.

Using an environment variable for this seems like the least worst option for now, as it's available to ALL pages and API routes in an app automatically, even if it is a bit weird. I do have thoughts on how we might improve the developer experience in future, but I don't see us ever breaking this option.

Note you can ad a custom path to this value e.g. NEXTAUTH_URL=https://ww.example.com is valid (with the default base ath of /api/auth) but you could also specify NEXTAUTH_URL=https://ww.example.com/custom-base-path/api/auth to use a custom base path for the API route.

The VERCEL_URL is a fallback but it's not currently a great experience as it's a random hostname for an instance rather than the canonical URL for a deployment. So, it might work well for email or a credentials provider, but you probably want to set it explicitly for OAuth, as typically URLs for callback URLs have to be very explicit (and can't just be at a domain).

This is developer pain I'm also interested in easing in future releases! The Vercel team might also drop a solution to this at some point, which we can then take advantage of.

The one thing that confused me about the VERCEL_URL is that I had to go and set it explicitly for it to be populated - I wasn't seeing it set by default, so still wasn't ideal for tihings like Pull Request environments.

@iaincollins regarding your last sentence (in case you haven't seen this already):

Vercel provides a set of Environment Variables that are optionally populated by the System, such as the URL of the Deployment or the name of the Git branch deployed. To apply a System Environment Variable to your Deployment, enter its Name, leave the Value empty, and click Add.
– https://vercel.com/docs/build-step#system-environment-variables

Was this page helpful?
0 / 5 - 0 ratings