Directory subset Auth Middleware
Answered
Catla posted this in #help-forum
CatlaOP
Hi. I currently have
However, when I navigate to /manage/accounts (the page.tsx is a client component with server actions on edge), instead of redirecting me to signin, it allows me through to the page.
How do I get this 'working'?
middleware.ts in my next-js project which is positioned in /manage/accounts:export { auth as default } from '@/auth';However, when I navigate to /manage/accounts (the page.tsx is a client component with server actions on edge), instead of redirecting me to signin, it allows me through to the page.
How do I get this 'working'?
Answered by Komondor
That is not the correct directory to place middleware.ts. Refer to the documentation on middleware it mentions that it must be placed at the root of your project
11 Replies
Komondor
That is not the correct directory to place middleware.ts. Refer to the documentation on middleware it mentions that it must be placed at the root of your project
Answer
Komondor
Middleware is a Nextjs feature, not authjs.
https://nextjs.org/docs/app/building-your-application/routing/middleware
https://nextjs.org/docs/app/building-your-application/routing/middleware
@Komondor Middleware is a Nextjs feature, not authjs.
https://nextjs.org/docs/app/building-your-application/routing/middleware
CatlaOP
Did you look at the link I sent you?
This question is about next-auth/authjs middleware, not nextjs middleware.
Or maybe I'm missing something?
Komondor
I just looked at that link and it does not specify where to put middleware.ts. The link I sent does
@Komondor I just looked at that link and it does not specify where to put middleware.ts. The link I sent does
CatlaOP
Why did you reply to me without checking the sources I sent?
In next-auth v4, it states middleware can be put in a directory to protect its subsets. I am assuming that is still the case, because it is undocumented if it's changed. However, I will try and move it to the root with a config matcher.
In next-auth v4, it states middleware can be put in a directory to protect its subsets. I am assuming that is still the case, because it is undocumented if it's changed. However, I will try and move it to the root with a config matcher.
Komondor
I didn't need to lol I know how it works. I'm using the same library
@Catla Why did you reply to me without checking the sources I sent?
In next-auth v4, it states middleware can be put in a directory to protect its subsets. I am assuming that is still the case, because it is undocumented if it's changed. However, I will try and move it to the root with a config matcher.
Sigh...
NextAuth uses the middleware functionality of NextJS. What nextauth is providing is merely a function thats going to protect your routes through the nextjs middleware.
So read the nextjs documentation on where to put the middleware file.
It needs to be on the same level as your app directory
NextAuth uses the middleware functionality of NextJS. What nextauth is providing is merely a function thats going to protect your routes through the nextjs middleware.
So read the nextjs documentation on where to put the middleware file.
It needs to be on the same level as your app directory
CatlaOP
Thanks. I got confused with this quote.
Otherwise, if you only want to protect a subset of pages, you could put it in a subdirectory as well, for example in /pages/admin/_middleware.js would protect all pages under /admin.
Though, I see now, that applies to next.js 12.
You are both correct @Clown @Komondor
Otherwise, if you only want to protect a subset of pages, you could put it in a subdirectory as well, for example in /pages/admin/_middleware.js would protect all pages under /admin.
Though, I see now, that applies to next.js 12.
You are both correct @Clown @Komondor