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

[NEXTjs] app router route handler / middleware wrapper auth((req) => {}) not working properly when initializing NextAuth() using a function. #12485

Open
jwwhangbo opened this issue Jan 9, 2025 · 0 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@jwwhangbo
Copy link

jwwhangbo commented Jan 9, 2025

Environment

System:
OS: macOS 15.2
CPU: (8) arm64 Apple M2
Memory: 167.84 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 23.5.0 - /usr/local/bin/node
npm: 11.0.0 - /opt/homebrew/bin/npm
pnpm: 9.15.2 - ~/Documents/GitHub/chatgpt-cooking/node_modules/.bin/pnpm
Watchman: 2024.09.02.00 - /opt/homebrew/bin/watchman
Browsers:
Chrome: 131.0.6778.205
Safari: 18.2
npmPackages:
@auth/core: ^0.37.4 => 0.37.4
@auth/pg-adapter: ^1.7.4 => 1.7.4
@auth/prisma-adapter: ^2.7.4 => 2.7.4
next: 15.1.2 => 15.1.2
next-auth: 5.0.0-beta.25 => 5.0.0-beta.25
react: 19.0.0 => 19.0.0

Reproduction URL

https://github.com/jwwhangbo/NibbleAI/blob/PROTOTYPE/app/api/collections/route.ts

Describe the issue

When initializing NextAuth using a function that doesn't take any parameters,

// @/auth.ts
export const { handlers, auth, signIn, signOut } = NextAuth(() => {
  const pool = new Pool({ connectionString: process.env.DATABASE_URL });
  return {
    adapter: PostgresAdapter(pool),
    callbacks: {
      session({ session, user }) {
        // ...
        return session;
      },
      authorized: async ({ request: { nextUrl }, auth }) => {
        // ...
    },
    providers,
    pages: {
      // ...
    },
  };
});

it returns an auth function that doesn't work when used in middleware.ts or route.ts.

// app/api/collections/route.ts
export const POST = auth(function (req) {
  if (req.auth) return NextResponse.json(req.auth)
  return NextResponse.json({ message: "Not authenticated" }, { status: 401 })
}) 

trying to fetch() from the route immediately returns a 500 internal error.

There were no issues when initializing NextAuth with an object instead of a function. Instead of creating a new PostgreSQL Pool instance within NextAuth (as shown in the database integration tutorial), an existing Pool object was imported and used.

// from Auth.js documentation
import NextAuth from "next-auth"
import PostgresAdapter from "@auth/pg-adapter"
import { Pool } from "@neondatabase/serverless"
 
// *DO NOT* create a `Pool` here, outside the request handler.
// Neon's Postgres cannot keep a pool alive between requests.
 
export const { handlers, auth, signIn, signOut } = NextAuth(() => {
  // Create a `Pool` inside the request handler.
  const pool = new Pool({ connectionString: process.env.DATABASE_URL })
  return {
    adapter: PostgresAdapter(pool),
    providers: [],
  }
})

How to reproduce

Instantiate NextAuth using a function that doesn’t take any parameters.

Expected behavior

auth() wrapper for route handlers and middleware should work normally.

@jwwhangbo jwwhangbo added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Jan 9, 2025
@jwwhangbo jwwhangbo changed the title [NEXTjs] app router route handler / middleware wrapper for auth((req) => {}) not working correctly when instantiating NextAuth() using a function. [NEXTjs] app router route handler / middleware wrapper for auth((req) => {}) not working correctly when initializing NextAuth() using a function. Jan 9, 2025
@jwwhangbo jwwhangbo changed the title [NEXTjs] app router route handler / middleware wrapper for auth((req) => {}) not working correctly when initializing NextAuth() using a function. [NEXTjs] app router route handler / middleware wrapper auth((req) => {}) not working correctly when initializing NextAuth() using a function. Jan 9, 2025
@jwwhangbo jwwhangbo changed the title [NEXTjs] app router route handler / middleware wrapper auth((req) => {}) not working correctly when initializing NextAuth() using a function. [NEXTjs] app router route handler / middleware wrapper auth((req) => {}) not working properly when initializing NextAuth() using a function. Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

1 participant