supabase: NextJS Middleware Auth Examples recommends using function that doesn't exist
Improve documentation
Link
https://supabase.com/docs/guides/auth/server-side/nextjs?queryGroups=router&router=app
Describe the problem
The main issue is that the current example is structured in a way were it assumes the only thing you will ever do in your middleware is authenticate with supabase, but often I would think that is not the case.
My current project has some complex redirection logic, so I can’t just simply return the supabaseResponse. The docs suggest making a new NextResponse and manually setting the cookies
const myNewResponse = NextResponse.next({ request })
myNewResponse.cookies.setAll(supabaseResponse.cookies.getAll())
The only problem is the cookies property on NextResponse is of type ResponseCookies which does not actually have a setAll method.
Describe the improvement
It would be fantastic if there was:
- A recommended way to pass on the Supabase cookies to new responses
- An example where the middleware does more than simply
return await updateSession - Maybe an example repo to go off of and could be linted as to avoid recommending non existent or depreciated apis
I’m upgrading my auth from 0.6 @supabase/auth-helpers-nextjs and I was actually in the process of writing a separate issue earlier asking for the docs to no longer recommend get, set and delete, but #27242 literally merged as I was writing it (thanks @hf), but I’ve run into this separate issue now. If any other additional information is needed just let me know- happy to help.
Thanks so much
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 8
- Comments: 16 (6 by maintainers)
Sounds more like a workaround than a definitive fix. Especially if you have a similar middleware that also needs to be at the end. I have fixed it by creating a response object in the first and pass it down the chain, so maybe you can use it or adapt it to your project:
middleware.ts:
stackMiddlewares.ts:
This is my first middleware, so it has to create a response object and returns it:
i18nMiddleware.ts
Here it receives the response from the previous middlewares, modifies it and once again returns it so the next middleware in the chain has access to it.
authenticationMiddleware.ts:
I hope this helps a bit.
@RowinVanAmsterdam just moved to your approach today to have something cleaner. it works perfectly, thanks!
@RowinVanAmsterdam I love the pattern you suggested 🎉 It feels nice and clean.
I had to slightly modify this pattern in order to get
next-intlto work. Without copying over the headers, routes will 404’s for me when the token is being resetThis is working for me locally, but not on Vercel 🤔 I’m trying to figure out what I’m messing up hereHad a cookie name conflict, now it’s working 🎉UPDATE: I think I have a solution for my headache:
This is what should have been put in the documentation
Other solution (async arrow function)