Next.js Discord

Discord Forum

How to use middleware in auth js V5

Unanswered
Macao paper wasp posted this in #help-forum
Open in Discord
Macao paper waspOP
HI! I've been experimenting with AuthJs V5. I would like to protect some of my routes, but auth js v5 doesn't support middleware anymore. Can you guys please help what's the alternative in v5?

40 Replies

Bicknell's Thrush
Did you manage to find something?
Sun bear
I think thats the way to go:

https://github.com/nextauthjs/next-auth/blob/main/apps/examples/nextjs/middleware.ts

I migrated just now. Not yet tested from my side but thats how I am going to do it
Bicknell's Thrush
Yeah I have no idea why it's not working for me. Maybe I am overlooking something small
Bicknell's Thrush
Any idea @Sun bear?
@Bicknell's Thrush Any idea <@908094633660268555>?
Sun bear
Based on your auth.ts

callbacks: {
        authorized({ request, auth }) {
            const { pathname } = request.nextUrl
            if (pathname === "/tester") return !!auth
            return true
        },
    }


I would expect only /tester to be affected by the middleware.

If I were you I would change the middleware for testing to:

export default auth((req) => {
console.log("auth middleware test")
console.log(req.auth)
})

export const config = {
  matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
}


Then first visit /tester and then the other pages just to see if the middleware is executed and when exactly
Bicknell's Thrush
I dont even see in the logs that the middleware is compiled, whereas with the example it is. I copied package.json, removed node_modules and everything. TSConfig is identical...
Bicknell's Thrush
Ok so I found the culprit
It only seems to work with app/ dir, not src/app
Is that a potential bug?
Nope nevermind, docs clearly state that it should live next to the app directory
Moving the middleware and auth.ts to src/ directory works!
hold on, that's not the OP
@Bicknell's Thrush Did you manage to find something?
Macao paper waspOP
Not sadly
@Macao paper wasp Not sadly
Bicknell's Thrush
I got it working, it matters where the middleware is located. You got a way to reproduce your situation?
@Bicknell's Thrush I got it working, it matters where the middleware is located. You got a way to reproduce your situation?
Macao paper waspOP
Are you using prisma as well? Because I found out edge doesn't support prisma
@Macao paper wasp Are you using prisma as well? Because I found out edge doesn't support prisma
Bicknell's Thrush
I don't, I don't use edge as well
@Bicknell's Thrush I don't, I don't use edge as well
Macao paper waspOP
Yeah, that's my problem sadly, but found a workaround
Where are you putting the middleware file?
Since you're here, can I please ask you another question?
Macao paper waspOP
I'm learning next js and following a tutorial. He's importing a server component into a client component, and I don't understand how is this acceptable, when something imported into a client component will become a client component as well. In this {addTodo} component, he fetches the database and creates entries, isn't this unsafe?
This is basically what he imports into the client component:
@Macao paper wasp Where are you putting the middleware file?
Bicknell's Thrush
in the same folder as where my app folder is
so either next to package.json or in the src directory if the app directory is in the src dir
@Bicknell's Thrush in the same folder as where my `app` folder is
Macao paper waspOP
okay, thank you very much!
Bicknell's Thrush
Hopefully it will help you! 😄
@Bicknell's Thrush Hopefully it will help you! 😄
Macao paper waspOP
Yeah! Are you a solo dev btw?
Bicknell's Thrush
yeah
just tinkering around
Macao paper waspOP
Yeah me too, just started
@Bicknell's Thrush yeah
Macao paper waspOP
Did you have any success with saas or something similar?
Bicknell's Thrush
I've have my fair share of SaaS experience, yes
Macao paper waspOP
That's great, how do you promote your stuff? I tried launching a few smaller projects, but no one found it basically
Bicknell's Thrush
Oh yeah I don't really, I just work at SaaS companies lol
I have no idea how I will promote my next upcoming product, I guess Twitter?
@Bicknell's Thrush Oh yeah I don't really, I just work at SaaS companies lol
Macao paper waspOP
got it, that's cool also
@Bicknell's Thrush I have no idea how I will promote my next upcoming product, I guess Twitter?
Macao paper waspOP
yeah, might try to do that, thanks
And thank you for your help!
Have a nice day