Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getServerSession() throws a [nuxt] [request error] [unhandled] [500] No Origin found even though origin is set #107

Closed
jimezesinachi opened this issue Sep 20, 2023 · 6 comments

Comments

@jimezesinachi
Copy link

jimezesinachi commented Sep 20, 2023

Environment

  • Operating System: Darwin
  • Node Version: v18.16.0
  • Nuxt Version: 3.7.3
  • CLI Version: 3.8.4
  • Nitro Version: 2.6.3
  • Package Manager: [email protected]
  • Builder: -
  • User Config: devtools, modules, ui, alias, authJs, runtimeConfig
  • Runtime Modules: @nuxt/[email protected], @hebilicious/[email protected], vue-email/nuxt
  • Build Modules: -

Reproduction

Full description of problem below

Describe the bug

I am making a call to getServerSession() in my API route, like this:

import { APIResponseData } from "~/server/utils/types";

import { prisma } from "../../../prisma/db";
import {
  GetUsersInputSchema,
  GlobalQueryResponseOutputSchemaInputType,
} from "~/server/utils/validators";
// import { getAuthSession } from "~/server/utils/admin";
import { getServerSession, getServerToken } from "#auth";
import { authOptions } from "../auth/[...]";

// import protectAdmin from "../../utils/validators/admin";

export default defineEventHandler(async (event) => {
  // await protectAdmin(event);
  const session = await getServerSession(event, authOptions);
  const jwt = await getServerToken(event, authOptions);

  console.log(session, jwt);

  return `API route`;
});

It throws this error:

[nuxt] [request error] [unhandled] [500] No Origin found ...
  at getServerOrigin (./node_modules/.pnpm/@[email protected]_@[email protected][email protected]/node_modules/@hebilicious/authjs-nuxt/dist/runtime/utils.mjs:15:11)
  at getServerToken (./node_modules/.pnpm/@[email protected]_@[email protected][email protected]/node_modules/@hebilicious/authjs-nuxt/dist/runtime/lib/server.mjs:47:19)
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  at <anonymous> (./server/api/users/index.get.ts:17:1)
  at async Object.handler (./node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1630:19)
  at async Server.toNodeHandle (./node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1840:7)

I have set NEXTAUTH_SECRET and NEXTAUTH_URL in my .env.

And here is my nuxt.config.ts:

import { resolve } from "node:path";

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: ["@nuxt/ui", "@hebilicious/authjs-nuxt", "vue-email/nuxt"],
  ui: {
    icons: ["logos", "mdi"],
  },

  alias: {
    cookie: resolve(__dirname, "node_modules/cookie"),
  },

  //   Optional default config
  authJs: {
    baseUrl: process.env.NUXT_NEXTAUTH_URL, // should be something like https://www.my-app.com
    verifyClientOnEveryRequest: true,
    //   guestRedirectTo: "/", // where to redirect if the user is authenticated
    //   authenticatedRedirectTo: "/", // where to redirect if the user is not authenticated
  },
  runtimeConfig: {
    authJs: {
      secret: process.env.NUXT_NEXTAUTH_SECRET, // You can generate one with `openssl rand -base64 32`
    },
    discord: {
      clientId: process.env.NUXT_DISCORD_CLIENT_ID,
      clientSecret: process.env.NUXT_DISCORD_CLIENT_SECRET,
    },
    email: {
      senderAddress: process.env.NUXT_EMAIL_SENDER_ADDRESS,
      serverUsername: process.env.NUXT_EMAIL_SERVER_USERNAME,
      serverPassword: process.env.NUXT_EMAIL_SERVER_PASSWORD,
      serverHost: process.env.NUXT_EMAIL_SERVER_HOST,
      serverPort: Number(process.env.NUXT_EMAIL_SERVER_PORT),
    },
    google: {
      clientId: process.env.NUXT_GOOGLE_CLIENT_ID,
      clientSecret: process.env.NUXT_GOOGLE_CLIENT_SECRET,
    },
    organization: {
      name: process.env.NUXT_ORGANIZATION_NAME,
      domain: process.env.NUXT_ORGANIZATION_HOST_DOMAIN,
    },
    public: {
      authJs: {
        baseUrl: process.env.NUXT_NEXTAUTH_URL, // The URL of your deployed app (used for origin Check in production)
        verifyClientOnEveryRequest: true, // whether to hit the /auth/session endpoint on every client request
      },
    },
  },
});

I can't figure out why it's still throwing the error.

Additional context

No response

Logs

No response

@jimezesinachi jimezesinachi changed the title getServerSession() returns null getServerSession() throws a [nuxt] [request error] [unhandled] [500] No Origin found even though origin is set Sep 20, 2023
@tobychidi
Copy link

There seems to be a missing .env Variable that is required

AUTH_ORIGIN

The docs needs updating.

@Hebilicious
Copy link
Owner

Hebilicious commented Sep 22, 2023

Do you have a reproduction ?
https://github.com/Hebilicious/authjs-nuxt/blob/e00d2046f79eb28c300adba6d4be471b8ae05d22/packages/authjs-nuxt/src/runtime/utils.ts#L22C1-L22C1

If you look at the logic here, it should work if you have a baseUrl properly set.
I assume it's because you did not prefix your env variables with NUXT_.

@tobychidi AUTH_ORIGIN is undocumented because it's unstable, the name might change later so I wouldn't rely on it too much.

@jimezesinachi
Copy link
Author

Do you have a reproduction ? https://github.com/Hebilicious/authjs-nuxt/blob/e00d2046f79eb28c300adba6d4be471b8ae05d22/packages/authjs-nuxt/src/runtime/utils.ts#L22C1-L22C1

If you look at the logic here, it should work if you have a baseUrl properly set. I assume it's because you did not prefix your env variables with NUXT_.

@tobychidi AUTH_ORIGIN is undocumented because it's unstable, the name might change later so I wouldn't rely on it too much.

Indeed. Adding the prefixes and the AUTH_ORIGIN env variable fixed it for me. Thanks!

@jimezesinachi
Copy link
Author

Closing this now, as this is not an issue with the plugin.

@orenmizr
Copy link

orenmizr commented May 8, 2024

got this error - i seem to have same config. i use pnpm.

added the env:
NUXT_NEXTAUTH_SECRET=something
NUXT_NEXTAUTH_URL=http://localhost:3000
NUXT_NEXTAUTH_AUTH_ORIGIN=http://localhost:3000

the alias in nuxt config
alias: {
cookie: "cookie",
},

the ERROR:
ERROR [nuxt] [request error] [unhandled] [500] No Origin found ...
at getServerOrigin (./node_modules/.pnpm/@Hebilicious+authjs-nuxt@0.3.5_@auth[email protected]_nuxt@3.11.2_@parcel+watcher@2.4.1_@types+nod_rmmh3nryu6ms5w6ogod5ukuutq/node_modules/@hebilicious/authjs-nuxt/dist/runtime/utils.mjs:15:11)
at getServerToken (./node_modules/.pnpm/@Hebilicious+authjs-nuxt@0.3.5_@auth[email protected]_nuxt@3.11.2_@parcel+watcher@2.4.1_@types+nod_rmmh3nryu6ms5w6ogod5ukuutq/node_modules/@hebilicious/authjs-nuxt/dist/runtime/lib/server.mjs:47:19)
at Object.handler (./server/api/bookmark.post.ts:23:1)
at async ./node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1962:19
at async Object.callAsync (./node_modules/.pnpm/[email protected]/node_modules/unctx/dist/index.mjs:72:16)
at async Server.toNodeHandle (./node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:2249:7)

@Corentin7301
Copy link

Hello,
I had the same issue as @jimezesinachi, adding the environment variable AUTH_ORIGIN also fixed the issue for me.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants