next.jsnext-auth

NextAuth 5: Host must be trusted


I've just successfully configured NextAuth 5 with NextJs 14 app. Then I checked out the project on another PC, created the same .env file and now I get the following error when navigating to http://localhost:3000/api/auth/signin

[auth][error][N]: Host must be trusted. URL was: http://localhost:3000/api/auth/signin. Read more at https://errors.authjs.dev#n
    at C:\Users\me\projects\my-project\.next\server\chunks\687.js:364:39815
    at nc (C:\Users\me\projects\my-project\.next\server\chunks\687.js:364:41457)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async C:\Users\me\projects\my-project\node_modules\next\dist\compiled\next-server\app-route.runtime.prod.js:6:41304

with the following page

enter image description here

Any ideas what should be the case or which option am I missing?

.env

AUTH_GITHUB_ID=id
AUTH_GITHUB_SECRET=secret
AUTH_SECRET=secret generated using https://generate-secret.vercel.app/32

src/auth.ts

import type { NextAuthConfig } from "next-auth"
import NextAuth from "next-auth"
import GitHub from "next-auth/providers/GitHub"
import { DrizzleAdapter } from "@auth/drizzle-adapter"
import { db } from "@/db"

export const authConfig = {
  providers: [GitHub],
  adapter: DrizzleAdapter(db),
  callbacks: {
    /* does not return ID by default */
    async session({session, user}) {
      session.user.id = user.id
      return session
    },
  }
} satisfies NextAuthConfig

export const { 
  handlers, 
  auth, 
  signOut 
} = NextAuth(authConfig)

src/app/api/auth/[...nextauth]/route.ts

import { handlers } from "@/auth";

export const { GET, POST } = handlers;

Solution

  • I was running the npm run start command instead of the npm run dev, so that .env was not picked up.