nuxt-auth icon indicating copy to clipboard operation
nuxt-auth copied to clipboard

Getting an AUTH_NO_ORIGIN on PM2 instance

Open matthewairalo opened this issue 2 years ago • 7 comments

Environment

  • Operating System: Linux
  • Node Version: v18.19.0
  • Nuxt Version: 3.8.2
  • CLI Version: 3.10.0
  • Nitro Version: 2.8.0
  • Package Manager: [email protected]
  • Builder: -
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

No response

Describe the bug

I am using PM2 to run the Nuxt3 build with sidebase/nuxt-auth in a production-like AWS instance. After the latest update, the app no longer runs successfully as it throws an AUTH_NO_ORIGIN error in runtime.

My .env file has it:


API_BASE_URL=https://dev.mysite.com/api
APP_URL=https://nuxt3-dev.mysite.com
AUTH_ORIGIN=https://nuxt3-dev.mysite.com

I've also tried setting it separately in nuxt.config.js, but the error seems to remain.

Additional context

No response

Logs

No response

matthewairalo avatar Feb 15 '24 16:02 matthewairalo

So, I had this problem and changing the system variable to NUXT_AUTH_ORIGIN fixed the issue. This was both locally running Windows 11, but also in Kubernetes stack with an Ubuntu guest.

codetheorist avatar Feb 18 '24 15:02 codetheorist

I use set -a && source .env && set +a in the project directory to export env variables and then use pm2 start ecosystem.config.cjs to start.

In case of updates

# Load env variables
set -a && source .env && set +a

# Restart the application
pm2 restart EXAMPLE.com --update-env

tberk avatar Feb 19 '24 07:02 tberk

@tberk - your solution helped. thank you! I'm leaving this as an open issue because the library should look for .env variables defined on the application level.

matthewairalo avatar Feb 20 '24 11:02 matthewairalo

@matthewairalo nuxt itself doesn't read .env files in production either if that's what you're suggesting

warflash avatar Feb 22 '24 02:02 warflash

Yes, I believe this issue isn't with the library itself but rather relates to how Nuxt manages environment variables.

From the Nuxt 3 documentation: https://nuxt.com/docs/guide/directory-structure/env

After your server is built, you are responsible for setting environment variables when you run the server. Your .env file will not be read at this point. How you do this is different for every environment.

tberk avatar Feb 22 '24 11:02 tberk

I answered here, you can view it here. I answered here, you can view it here.

FerhatCengz avatar May 16 '24 23:05 FerhatCengz

  1. I set the baseURL instead of origin in auth option on nuxt.config.ts
  2. setup AUTH_ORIGIN=http://localhost:3000 in .env
  3. setup NEXTAUTH_SECRET=<key> in .env
  4. setup process.env.NEXTAUTH_SECRET in NuxtAuthHandler's against secret key development run is ok, but while running the build, getting the error
nuxt] [request error] [unhandled] [500] AUTH_NO_SECRET: No `secret` - this is an error in production, see https://sidebase.io/nuxt-auth/resources/errors. You can ignore this during development
  at NuxtAuthHandler (./.output/server/chunks/_..._.mjs:40:13)  
  at ./.output/server/chunks/_..._.mjs:134:15  
  at ModuleJob.run (node:internal/modules/esm/module_job:217:25)  
  at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)  
  at async Object.handler (./.output/server/chunks/nitro/node-server.mjs:3078:19)  
  at async Server.toNodeHandle (./.output/server/chunks/nitro/node-server.mjs:3267:7)

VISHNU-P-M avatar May 28 '24 11:05 VISHNU-P-M