Next Auth middleware redirects me to the login page, even if there's a session

I am busting my head around this thing. I tried to setup NextJS with NextAuth, Drizzle and PlanetScale. So I am not using the T3-scaffold. The thing is that I can see that there's a user returned from getServerSession. But when I add my middleware.ts file. It just keeps redirecting me to the login page. As if there would be no session the middleware could recognize. Any ideas to why?
3 Replies
Sturlen
Sturlen17mo ago
what does your middleware.ts look like?
Chill Guy
Chill GuyOP17mo ago
Well right now the regular exported function with the withAuth stuff. Also have matcher. But if I remove the matcher, then I can't (of course) visit any page.
export { default } from "next-auth/middleware"

export const config = { matcher: ["/dashboard"] }
export { default } from "next-auth/middleware"

export const config = { matcher: ["/dashboard"] }
Shoodey
Shoodey16mo ago
you cannot use next-auth's session with nextjs's middleware, you have to use jwt
// auth.ts
session: {
strategy: "jwt",
},
// auth.ts
session: {
strategy: "jwt",
},
// middleware
const token = await getToken({ req });
// middleware
const token = await getToken({ req });

Did you find this page helpful?