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

`/api/auth/login` failing with "secret" is required on AWS Amplify

Open ilarumk opened this issue 4 years ago • 12 comments

Description

I am deploying next.js application with auth0 authentication onto AWS Amplify. This is working on localhost as expected. I created "Environment variables" with AUTH0_SECRET and others in the amplify App Settings, and I am able to authenticate and it is working fine. Suddenly after one of the deployment, I keep getting this error

Browser Error

503 ERROR
The request could not be satisfied.

Logs:

ERROR	Invoke Error 	
{
    "errorType": "TypeError",
    "errorMessage": "\"secret\" is required",
    "stack": [
        "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.5862 (/var/task/pages/api/auth/[...auth0].js:214:129)",
        "    at __webpack_require__ (/var/task/webpack-api-runtime.js:25:42)",
        "    at Object.7416 (/var/task/pages/api/auth/[...auth0].js:189:23)",
        "    at __webpack_require__ (/var/task/webpack-api-runtime.js:25:42)",
        "    at __webpack_exec__ (/var/task/pages/api/auth/[...auth0].js:325:39)"
    ]
}

for debugging, I printed the secret using console.log and I am able to see it.

Reproduction

my [...auth0].js

import { handleAuth } from '@auth0/nextjs-auth0';
console.log('the AUTH0_SECRET env var is set: ', !!process.env.AUTH0_SECRET);
export default handleAuth();

Environment

Please provide the following:

  • Version of this library used:
"@auth0/auth0-react": "^1.9.0",
    "@auth0/nextjs-auth0": "^1.7.0",
    "axios": "^0.26.1",
    "jsonwebtoken": "^8.5.1",
    "next": "latest",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-is": "^18.0.0",
    "swr": "^1.3.0",

ilarumk avatar Apr 30 '22 19:04 ilarumk

Hi @ilarumk - thanks for raising this.

I can't think of a scenario where your AUTH0_SECRET environment variable is set correctly and handleAuth would give you a "secret" is required error.

Can you confirm that you're seeing this error on the same environment that your are confirming that the AUTH0_SECRET environment variable is correctly set?

If so, can you share a reproducible example? I can't see anything wrong with the code you've shared

adamjmcgrath avatar May 03 '22 09:05 adamjmcgrath

I can confirm that it is in the same environment. I can even print the environment variable AUTH0_SECRET in the cloudwatch logs

I deployed exact code onto Google Cloud Run , and it is working over there. Could be some settings related to Amplify

ilarumk avatar May 03 '22 18:05 ilarumk

Hi @ilarumk

I can't see any reason why you would get that error when process.env.AUTH0_SECRET is set correctly.

This doesn't look like an issue with the SDK, so there's not much I can suggest other than making sure process.env.AUTH0_SECRET is set before you call handleAuth

adamjmcgrath avatar May 04 '22 17:05 adamjmcgrath

Hi @ilarumk - I don't think there's anything for the SDK to do here so closing. Feel free to ping me if you want to reopen

adamjmcgrath avatar May 09 '22 09:05 adamjmcgrath

I'm also encountering this issue, I'm able to view the secret from the environment variable when it is logged in the console.

pramodgobburi3 avatar Jul 12 '22 21:07 pramodgobburi3

The way I solved this is my creating an .env file in build job from environment variables that are available in the build step and pushed that onto production during deployment.

On Tue, Jul 12, 2022 at 2:28 PM Pramod Gobburi @.***> wrote:

I'm also encountering this issue, I'm able to view the secret from the environment variable when it is logged in the console.

— Reply to this email directly, view it on GitHub https://github.com/auth0/nextjs-auth0/issues/654#issuecomment-1182516529, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2ROP5KDKMLKONPGDOSL3VTXPOJANCNFSM5UYYJNFA . You are receiving this because you were mentioned.Message ID: @.***>

ilarumk avatar Jul 12 '22 23:07 ilarumk

The way I solved this is my creating an .env file in build job from environment variables that are available in the build step and pushed that onto production during deployment. … On Tue, Jul 12, 2022 at 2:28 PM Pramod Gobburi @.> wrote: I'm also encountering this issue, I'm able to view the secret from the environment variable when it is logged in the console. — Reply to this email directly, view it on GitHub <#654 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2ROP5KDKMLKONPGDOSL3VTXPOJANCNFSM5UYYJNFA . You are receiving this because you were mentioned.Message ID: @.>

Thanks, hopefully, Amplify will issue a patch to fix this bug soon.

pramodgobburi3 avatar Jul 13 '22 02:07 pramodgobburi3

@ilarumk I am having the same issue. It feels weird having to write a script that will create a .env file when the vars can be logged and are visible....makes no sense.

do you think it's a problem with Amplify?

alexboulay avatar Oct 19 '22 17:10 alexboulay

How can I create prod env during the build stage?

axelmukwena avatar Nov 22 '22 19:11 axelmukwena

@axelmukwena after much trial and error, I stumbled upon these AWS help docs which helped a lot.

Specifically, going to the "Build settings" page of my app and updating the build section to:

        build:
          commands:
            - env | grep -e AUTH0_ >> .env.production
            - npm run build

noanflaherty avatar Dec 19 '22 03:12 noanflaherty

@axelmukwena after much trial and error, I stumbled upon these AWS help docs which helped a lot.

Specifically, going to the "Build settings" page of my app and updating the build section to:

        build:
          commands:
            - env | grep -e AUTH0_ >> .env.production
            - npm run build

I tried this, but it doesn't work Do I need to config the env variables in the nextjs.config,js too?

Funny, I have the project in another Amplify, using Web dynamic instead of Web compute, and it works

Lea23VC avatar Jan 31 '23 18:01 Lea23VC

@axelmukwena after much trial and error, I stumbled upon these AWS help docs which helped a lot.

Specifically, going to the "Build settings" page of my app and updating the build section to:

        build:
          commands:
            - env | grep -e AUTH0_ >> .env.production
            - npm run build

it was successful for me at the beginning but waited for a half-hour, seems to work, could be the browser cache.

also, my app builds on turborepo, a bit tricky on that part too.

max8hine avatar Feb 17 '23 01:02 max8hine