nextjs-auth0: Hosting via Vercel not working because of AUTH0_BASE_URL.

Description

Hi, I would like to reopen https://github.com/auth0/nextjs-auth0/issues/383, as https://github.com/auth0/nextjs-auth0/pull/404 did not fix the problem, as you can see in the last comments of https://github.com/auth0/nextjs-auth0/issues/383.

The documentation at https://github.com/auth0/nextjs-auth0/blob/main/examples/README.md#assigning-the-auth0_base_url seems wrong to me, as the env key in config and the webpack DefinePlugin don’t work for files in node_module, which are not touched by webpack.

Moreover, on a Vercel side, the VERCEL_URL reference the “by commit domain”, and not the “by PR domain”, so we can’t use auth0 with the standard PR domain.

We have to figure out another way to do this.

Any thoughts about this?

Thanks.

Environment

  • Version of this library used: v1.4.0
  • Version of the platform or framework used, if applicable: Next 10.2.3 with Vercel

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 32 (11 by maintainers)

Most upvoted comments

I can confirm that committing a .env.production with the following works.

# .env.production
AUTH0_BASE_URL=${VERCEL_URL}

Also, specifying AUTH0_BASE_URL for your production domain in the Vercel environment config (through GUI or CLI) will properly override what’s defined in the .env.production file.

Managed to have a work-around for this issue by overriding Vercel’s Build Command AUTH0_BASE_URL=“$VERCEL_URL” yarn build

@sioquim - I’ve been testing this and assiging AUTH0_BASE_URL for the build command (AUTH0_BASE_URL="$VERCEL_URL" npm build) doesn’t work because it only provides the AUTH0_BASE_URL during the Build Step not during Serverless Function execution when it’s required by the SDK. Am I missing something?

Ah, ok - thanks for sharing that example repo @tusbar. It looks like the example app was working differently because it was installing the SDK from a relative path

I’ll come up with a different way to resolve #383

I can confirm that the fix does not work.

I have the following entry in the env section of my next.config.js:

AUTH0_BASE_URL: process.env.AUTH0_BASE_URL ?? process.env.VERCEL_URL ?? 'http://localhost:4000',

When my app using VERCEL_URL=localhost:4000 yarn next start -p 4000 and navigating to the api/auth/login route, the joi validation fails with: TypeError: "baseURL" is required.

getConfig from nextjs-auth0 does not retrieve the environment variable value: in dist/config.js, AUTH0_BASE_URL is undefined, so baseURL is too.

ok, thanks @sioquim - I’m going to test that out on the example app and update the guidance if it works

Thanks @sioquim

But still not sure how to make PR Domains work.

Would this work? AUTH0_BASE_URL="$VERCEL_GIT_REPO_SLUG-$VERCEL_GIT_COMMIT_REF-$VERCEL_GIT_REPO_OWNER.vercel.app" yarn build

The issue is that process.env.AUTH0_BASE_URL will not be replaced within the SDK, and the configuration validation will fail at runtime.

Here’s a basic example: https://github.com/tusbar/nextjs-auth0-repro

All I did was export a getServerSideProps in pages/index.js

export const getServerSideProps = withPageAuthRequired({
  getServerSideProps: () => ({
    value: 'foo'
  })
})

https://github.com/tusbar/nextjs-auth0-repro/commit/fb1db699287111fc2a782d3db495fdcc3348fa7f