Hi guys, new here, so I'm not sure what I'm doing wrong. Basically, without the middleware file, the authentication is working and I'm getting the user ID, email (using Google as provider) after I log in.
This is my api/auth/[...nextauth]/route.ts file:
import NextAuth from 'next-auth'
import GoogleProvider from 'next-auth/providers/google'
import clientPromise from '@/lib/mongodb'
import { MongoDBAdapter } from '@next-auth/mongodb-adapter'
const handler = NextAuth({
secret: process.env.NEXTAUTH_SECRET,
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID!,
clientSecret: process.env.GOOGLE_SECRET!
})
],
adapter: MongoDBAdapter(clientPromise),
})
export { handler as GET, handler as POST }
And this is my middleware file:
export { default } from 'next-auth/middleware'
export const config = {
matcher: [
'/',
'/api/auth/signin',
'/api/products',
'/api/upload',
'/api/events',
'/products',
'/products/new',
'/products/edit',
'/events'
]
}
With this, all of those pages are locked and when I sign in with Google, I get back to the same page for signin (http://localhost:3000/api/auth/signin?callbackUrl=%2Fproducts). When going manually to localhost:3000, I still get redirected to sign in, so I'm guessing somehow the session is not being saved or something. But without the middleware it's working and I'm getting the data for the logged in user.
I've wrapped my layout with a SessionProvider. I'm not sure what I'm missing. Can you please help?
Much appreciated!