Trying to setup NextAuth with middleware.tsx file - login is happening but always returns to login

Trying to setup NextAuth with middleware.tsx file - login is happening and session is created in database but page never returns to page that is behind matcher. Middleware.tsx File is: import { withAuth } from "next-auth/middleware"; export default withAuth({ secret: process.env.SECRET, }); export const config = { matcher: ["/dashboard"] } I've also tried: export { default } from "next-auth/middleware" export const config = { matcher: ["/dashboard"] } Without middleware file App Directory redirection is working fine
5 Replies
jps
jps16mo ago
should be middleware.ts and needs to be in the root of your directory. See here: https://nextjs.org/docs/pages/building-your-application/routing/middleware
Routing: Middleware
Learn how to use Middleware to run code before a request is completed.
DrewHack
DrewHack16mo ago
Seems like that is not the issue. without middleware.tsx the page renders without auth. With it, it renders with auth. I think the issue may be more related to my nextauth callbacks and I was looking in the wrong place for good measure testing and renaming to .ts from .tsx did not change the behaviour figured it out
tyler4949
tyler494914mo ago
@drewhack can you please share what you did to figure it out? I am running into this same issue. After some debugging it looks like my token is always null (following these docshttps://next-auth.js.org/configuration/nextjs#middleware)
DrewHack
DrewHack14mo ago
I believe it was setting the strategy to JWTs
tyler4949
tyler494914mo ago
Cool, thanks so much. Appreciate it
Want results from more Discord servers?
Add your server