Need new guidance for VERCEL_URL and AUTH0_BASE_URL
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)
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!
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}`;
};
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'
}
};
Thanks @darronj - that looks like the best solution, I'll test it out and update our guidance
@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)
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.
any idea of when this will be released? I'd love for my preview branches to work for my team.
@darronj - will plan to make a release this week
@adamjmcgrath - has this been released yet? 🙏 I'm also facing this issue at the moment.
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.
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.
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.
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
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?
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 Except that one doesn't work, because Vercel doesn't read the .env.production file at all.