nextjs-auth0 icon indicating copy to clipboard operation
nextjs-auth0 copied to clipboard

Need new guidance for VERCEL_URL and AUTH0_BASE_URL

Open adamjmcgrath opened this issue 5 years ago • 16 comments

The guidance in https://github.com/auth0/nextjs-auth0/blob/main/examples/README.md#environment-variables-1 needs to change.

We can no longer assign VERCEL_URL to AUTH0_BASE_URL using "Reference to System Environment Variable" in the Vercel dashboard.

We previously introduced the Provided by System option as some frameworks need to map system variables like VERCEL_URL to framework prefixed variables like NEXT_PUBLIC_VERCEL_URL. You no longer need to configure this mapping because the prefixed variables are added automatically based on your Framework Preset.

Source: https://vercel.com/changelog/environments-variables-per-git-branch

We will need to suggest a way that developers can assign VERCEL_URL to AUTH0_BASE_URL when the application is running (note: we don't need AUTH0_BASE_URL at build time)

adamjmcgrath avatar Apr 28 '21 13:04 adamjmcgrath

I was able to get around this by declaring

import { initAuth0 } from "@auth0/nextjs-auth0";
import { BASE_URL } from "./constants";

// Workaround for dynamic AUTH0_BASE_URL env variable
export default initAuth0({
  baseURL: BASE_URL
});

where

export const BASE_URL =
  isDev || !process.env.VERCEL_URL
    ? "http://localhost:3000"
    : `https://${process.env.VERCEL_URL}`;

and then create the SSR with

import client from "utils/auth0";

export default client.handleAuth();

Not sure this is a recommended solution but has worked for me. Open to feedback to get this better!

timnlupo avatar Apr 28 '21 17:04 timnlupo

export const getURL = (): string => {
  const url =
    process?.env?.URL && process.env.URL !== ''
      ? process.env.URL
      : process?.env?.VERCEL_URL && process.env.VERCEL_URL !== ''
      ? process.env.VERCEL_URL
      : 'http://localhost:3000';
  return url.includes('http') ? url : `https://${url}`;
};

dsbrianwebster avatar May 07 '21 03:05 dsbrianwebster

I solved this for our use case very simply. I added the following to my next.config.js exports.

module.exports = {
  env: {
    AUTH0_BASE_URL: process.env.VERCEL_URL || 'http://localhost:3000'
  }
};

darronj avatar May 22 '21 23:05 darronj

Thanks @darronj - that looks like the best solution, I'll test it out and update our guidance

adamjmcgrath avatar May 25 '21 09:05 adamjmcgrath

@adamjmcgrath I probably spoke too soon, I can’t get this to work on preview branches. Actually I can’t get either of these workarounds to work in preview. Failures occur in the serverless functions. Production works great.

ERROR TypeError: "baseURL" is required at Object.get (/var/task/apps/react-workspace/node_modules/@auth0/nextjs-auth0/dist/auth0-session/get-config.js:145:15)

darronj avatar May 26 '21 00:05 darronj

Hi @darronj - this is because we're destructuing the process.env here https://github.com/auth0/nextjs-auth0/blob/main/src/config.ts#L410-L437

And the DefinePlugin doesn't work with that:

Next.js will replace process.env.customKey with 'my-value' at build time. Trying to destructure process.env variables won't work due to the nature of webpack DefinePlugin.

https://nextjs.org/docs/api-reference/next.config.js/environment-variables

I'll raise a PR to fix this then your example should work.

adamjmcgrath avatar May 27 '21 10:05 adamjmcgrath

any idea of when this will be released? I'd love for my preview branches to work for my team.

darronj avatar May 28 '21 16:05 darronj

@darronj - will plan to make a release this week

adamjmcgrath avatar Jun 01 '21 12:06 adamjmcgrath

@adamjmcgrath - has this been released yet? 🙏 I'm also facing this issue at the moment.

thompson-ad avatar Jun 07 '21 09:06 thompson-ad

I am still seeing this error when on preview branches. My production branch with a set custom url runs fine. The problem only shows up on the /api/auth/[...] routes, as the pages load fine.

darronj avatar Jun 09 '21 16:06 darronj

I can be wrong but for me the define plugin will not work out of the box in node_module, so I'm afraid we actually can't rely on it.

mathieutu avatar Jun 09 '21 23:06 mathieutu

I couldn't get the

module.exports = {
  env: {
    AUTH0_BASE_URL: process.env.VERCEL_URL || process.env.AUTH0_BASE_URL,
  },
};

approach to work. I recognize this is the recommended approach but I get TypeError: "baseURL" is required when I visit /api/auth/login.

GuiSim avatar Nov 23 '21 17:11 GuiSim

Workaround

Using the env setting in next.config.js does not work as others have pointed out because Next uses Webpack's DefinePlugin to rewrite the process.env.AUTH0_BASE_URL variable in the emitted source, but this is only accessed inside the library in node_modules (which is not rewritten). We can work around this by manually setting process.env.AUTH0_BASE_URL ourselves, at runtime.

For the Auth0 hook pages/api/auth/[...auth0].ts and any other API hooks that will rely on Auth0, you must add this statement to the top of the context:

// Set to VERCEL_URL or override with AUTH0_BASE_URL 
process.env.AUTH0_BASE_URL = process.env.AUTH0_BASE_URL || process.env.VERCEL_URL;

// rest of file
// ...

As well as at the top of your application context in pages/_app.tsx. This will load the proper URL for all Vercel deploys, including Preview Builds. cc @adamjmcgrath

ctjlewis avatar Jan 31 '22 18:01 ctjlewis

This could actually be integrated into the library directly by doing this manually at the top of the library context. Have we considered that approach?

ctjlewis avatar Jan 31 '22 18:01 ctjlewis

For anyone arriving here from Google tying to make Vercel preview deployments work, there's a good explanation here: https://github.com/auth0/nextjs-auth0/blob/main/examples/README.md

victormier avatar Mar 21 '22 13:03 victormier

@victormier Except that one doesn't work, because Vercel doesn't read the .env.production file at all.

thexpand avatar Jun 28 '23 19:06 thexpand