nextjs-auth0: Auth0 login fails with '"secret" is required' when deployed to Netlify

Description

I have followed the Auth0 for Next.js instructions. This works fine locally, and I can sign in and out. ~I also need an email allowlist, so I’ve used the template “Whitelist” rule. My rule ended up locking out users who created an account without being on the allowlist, so I adapted code from this Community post to solve that problem.~ Update: I have turned off the Email Allowlist to simplify matters, but I still get the same error.

Everything works fine when I sign in locally. But when I deploy to Netlify and sign in, I get this error:

{
  "errorType": "TypeError",
  "errorMessage": "\"secret\" is required",
  "trace": [
    "TypeError: \"secret\" is required",
    "    at Object.get (/var/task/src/nextPage/index.js:25502:15)",
    "    at Object.getConfig (/var/task/src/nextPage/index.js:14641:40)",
    "    at Object.initAuth0 (/var/task/src/nextPage/index.js:46736:49)",
    "    at getInstance (/var/task/src/nextPage/index.js:46732:24)",
    "    at handleAuth (/var/task/src/nextPage/index.js:46784:33)",
    "    at Module.HJ75 (/var/task/src/nextPage/index.js:16032:134)",
    "    at __webpack_require__ (/var/task/src/nextPage/index.js:23:31)",
    "    at Module.JR3O (/var/task/src/nextPage/index.js:18752:23)",
    "    at __webpack_require__ (/var/task/src/nextPage/index.js:23:31)",
    "    at /var/task/src/nextPage/index.js:91:18"
  ]
}

I have these variables set in the Netlify environment:

AUTH0_SECRET='xxx'
AUTH0_BASE_URL='https://deployed-site-url.netlify.app'
AUTH0_ISSUER_BASE_URL='https://xxx-xxx-xxx.xx.auth0.com'
AUTH0_CLIENT_ID='xxx'
AUTH0_CLIENT_SECRET='xxx'

I have the same ones set locally using .env.local, but the AUTH0_BASE_URL is set to http://localhost:3000.

I’m never explicitly initing Auth0, I’m just using useUser and withPageAuthRequired as needed. My api/auth/[...auth0].js file looks the same as the tutorial:

import { handleAuth } from '@auth0/nextjs-auth0';

export default handleAuth();

What am I doing wrong??

Environment

Please provide the following:

  • Version of this library used: @auth0/nextjs-auth0@1.3.0
  • Version of the platform or framework used, if applicable: @next/env@10.0.9
  • Other relevant versions (language, server software, OS, browser): Mac, but the same failure happens on Firefox and Brave (Chromium).
  • Other modules/plugins/libraries that might be involved: Netlify

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (5 by maintainers)

Most upvoted comments

Just to add in here, I was also getting this same error when deploying to vercel

i was having the same problem… make sure you have the file named .env.local and not env.local 😃 i won’t say how long it took me to figure that one out lol…

i was having the same problem… make sure you have the file named .env.local and not env.local 😃 i won’t say how long it took me to figure that one out lol…

And make sure that the .env.local file is in the root of the project AND NOT IN THE src FILE! 😃

LATE EDIT

Apparently, something happened to the environment variables being passed to vercel. That config file was not successfully parsed by vercel.

When I set the environment variables manually in Vercel, suddenly it worked.

Dropping in to report the same issue, with deployment of a Next.js RWA to vercel.

For the auth/api/me and auth/api/login routes I get the TypeError: "secret" is required.

my config:

const { PHASE_DEVELOPMENT_SERVER } = require("next/constants");

module.exports = (phase) => {
  if (phase === PHASE_DEVELOPMENT_SERVER) {
    return {
      env: {
        mongodb_username: "...",
        [...]
        mongodb_database: "...",
        AUTH0_SECRET: "...",
        AUTH0_BASE_URL: "http://localhost:3000",
        AUTH0_ISSUER_BASE_URL: "https://<tenent>.eu.auth0.com",
        AUTH0_CLIENT_ID: "...",
        AUTH0_CLIENT_SECRET: "...",
      },
    };
  }

  return {
    env: {
      mongodb_username: "...",
      mongodb_password: "...",
      mongodb_clustername: "...",
      mongodb_database: "...",
      AUTH0_SECRET: "....",
      AUTH0_BASE_URL: "https://<...>.vercel.app",
      AUTH0_ISSUER_BASE_URL: "https://<tenent>.eu.auth0.com",
      AUTH0_CLIENT_ID: "...",
      AUTH0_CLIENT_SECRET: "...",
    },
  };
};

A 500 Internal server error is shown on the client after clicking log in, and the following function error is displayed in the server (or serverless function in vercel):

the AUTH0_SECRET env var is set:  true
2021-08-16T11:18:06.699Z	7b606fbe-0d63-489d-8bf8-6c3d2e290d2a	ERROR	TypeError: "secret" is required
    at Object.get (/var/task/node_modules/@auth0/nextjs-auth0/dist/auth0-session/get-config.js:147:15)
    at Object.getConfig (/var/task/node_modules/@auth0/nextjs-auth0/dist/config.js:66:38)
    at Object.initAuth0 (/var/task/node_modules/@auth0/nextjs-auth0/dist/index.js:22:23)
    at getInstance (/var/task/node_modules/@auth0/nextjs-auth0/dist/index.js:18:24)
    at handleAuth (/var/task/node_modules/@auth0/nextjs-auth0/dist/index.js:124:18)
    at Object.4987 (/var/task/.next/server/pages/api/auth/[...auth0].js:25:129)
    at __webpack_require__ (/var/task/.next/server/webpack-runtime.js:25:42)
    at __webpack_exec__ (/var/task/.next/server/pages/api/auth/[...auth0].js:42:39)
    at /var/task/.next/server/pages/api/auth/[...auth0].js:43:28
    at Object.<anonymous> (/var/task/.next/server/pages/api/auth/[...auth0].js:46:3)
RequestId: 7b606fbe-0d63-489d-8bf8-6c3d2e290d2a Error: Runtime exited with error: exit status 1
Runtime.ExitError

What I tried:

  • Generate another ‘secret’ and using different keys for localhost and domain - didn’t work
  • Log the env variable - works as expected
  • Verify Allowed Callback URLs - all set as expected, a `http://localhost:3000/api/auth/callback’ for local, and another for the vercel deployment URL
  • Wait - I’ve waited 12 hours to see if anything changes

@adamjmcgrath any thoughts?

Thanks

Hi do you remember how you solved this?

Hii Answer (My Solution)

Don’t put the *(Auth0) Environmental variable in Next.config.js .env.local worked for me

Is there a way to get more verbose logging in the next_api_auth_auth0 function which is running the SDK code?

Can you log that env var out in your handler to see if it’s set? Perhaps in the api/auth/[...auth0].js file

import { handleAuth } from '@auth0/nextjs-auth0';

console.log('the AUTH0_SECRET env var is set: ', !!process.env.AUTH0_SECRET);

export default handleAuth();

Thanks for the reply. I have put the environment variables in the Build & deploy section of the Site settings.

Screenshot 2021-05-10 at 12 42 46

The link you provide above says this should be sufficient:

Environment variables set in the UI are also accessible in other environments associated with your site, including serverless functions at execution time

I’ve included a “SECRET” variable as well, just to see if “AUTH0_” was causing the issue. This didn’t change anything, though.

Is there a way to get more verbose logging in the next_api_auth_auth0 function which is running the SDK code?