Using middleware with NextAuth can't redirect out of the sign in page after logging in
Unanswered
Cape lion posted this in #help-forum
Cape lionOP
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:
And this is my middleware file:
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!
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!
10 Replies
remove '/api/auth/signin', from the list
@tafutada777 remove '/api/auth/signin', from the list
Cape lionOP
It's still the same.
you need to exclude auth.js path like this.
export const config = {
matcher: ["/((?!register|api|login).*)"],
};wait
Cape lionOP
here's a short video on the weird behavior
also, it's loading from the database when no user is logged in, but I'll worry about that later 😄 first I need to actually be able to login after I protect the pages with middleware
Cape lionOP
can someone please give any tips and ideas how to fix this? thanks
@nelson I am encountering the same problem. Please help
Cape lionOP
I didn't fix it, so I rage deleted everything related to nextauth, then tried again with github auth and custom credentials, and it works now 🤣
@Cape lion I didn't fix it, so I rage deleted everything related to nextauth, then tried again with github auth and custom credentials, and it works now 🤣
I just use route based auth for now ðŸ˜