0

How to implement lazy initialization of authjs (v5 beta) config in nextjs 14 without encountering the error Error: The Middleware "/src/middleware" must export a middleware or a default function?

My middleware:

import { auth } from "@/auth"

export default auth((req) => {
  // my custom logic here
})

And my auth config file has this code:

import NextAuth from "next-auth"
import GitHub from "next-auth/providers/github"
export const { handlers, auth } = NextAuth(req => {
 if (req) {
  console.log(req) // I do something with the request's custom cookie
 }
 return { providers: [ GitHub ] }
})

If I go with the static method, it works but I lose the access to request req as I have to access a custom cookie in the NextAuth() and do something with it.

1 Answer 1

0

https://github.com/nextauthjs/next-auth/issues/11544#issuecomment-2538494101

I used this fix. Change your middleware to be

export default await auth((req) => {
  // my custom logic here
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.