auth-helpers icon indicating copy to clipboard operation
auth-helpers copied to clipboard

Upcoming Next.js middleware changes

Open ethndotsh opened this issue 2 years ago • 4 comments

In the Next.js package, there is a function which is withMiddlewareAuth. In the upcoming breaking changes to Next.js version 12.2, they completely alter how middleware works. I would just like to confirm that this update does not affect this package, as it is a crucial component of my app.

ethndotsh avatar Jun 22 '22 22:06 ethndotsh

Now that 12.2 is out it looks like some changes will be required given that there is only a single middleware per application. I'll look into what a new approach might look like when I can. For now, consider withMiddlewareAuth broken in 12.2

Saeris avatar Jun 28 '22 16:06 Saeris

A PR is welcomed if anyone has a fix for this change. I haven't had a chance to look into this change as yet so won't be able to work on a fix at the moment.

silentworks avatar Jun 29 '22 10:06 silentworks

Been looking at https://nextjs.org/docs/messages/middleware-upgrade-guide and I think placing the middleware.js at the same level as your pages folder will do the trick and than do something like:

middleware.js
import { withMiddlewareAuth } from '@supabase/auth-helpers-nextjs/middleware';

export function middleware(request) {
    withMiddlewareAuth({ redirectTo: '/login' });
}

export const config = {
    matcher: ['/dashboard/:path*'],
}

But weirdly enough I get the below error, which makes no sense to me

./middleware.js:1:0
Module not found: Can't resolve '@supabase/auth-helpers-nextjs/middleware'
> 1 | import { withMiddlewareAuth } from '@supabase/auth-helpers-nextjs/middleware';

Jordaneisenburger avatar Jul 07 '22 23:07 Jordaneisenburger

Hey @Jordaneisenberger,

Try importing it from @supabase/auth-helpers-nextjs/dist/middleware. This seems to be where it is being exported from at the moment.

ethndotsh avatar Jul 10 '22 01:07 ethndotsh