|
Running: Here's the permalink to the Stack Trace on Vercel |
Replies: 10 comments 16 replies
|
hi @ztanner In our application, Example of condition and redirect in const isMember = false
if (!isMember) {
return NextResponse.redirect(new URL('/plans', request.url));
}
Hoping we can get a patch soon! |
|
I came across to this problem as well, IMO, having the redirects at middleware level made more sense, instead of duplicating same logic in more than one place ( one at middle ware and others at each server action ). My dependencies: in the export function middleware(request: NextRequest) { } export const config = { |
|
We're in October now, and the docs are still not updated, it should clearly reflect this conflict between server actions and running auth in middleware. Would probably save other devs from running into this.. 🙏 Thanks to finding this closed thread we were able to solve a major headache in our auth flow. We disabled middleware for server actions by checking this condition in our conditional statement: And added explicit checks in each action instead. |
|
Thanks for this thread and the "work arrounds" I was pulling my hear on this one 😅 |
|
I solved redirection of server actions in middleware with setting the corresponding header: |
|
There are almost nothing in document mentioned that middleware can't control server action directly. |
|
@axten has a great proposal. However, I've noticed that the redirect with this header doesn't occur immediately. This could be due to our different setups. In my setup, I return an empty response and everything works fine. Also, using Here's a revised code: This code changes the return URL to pathname and search, and cleans up any session cookies if necessary. |
|
Having related issue with middleware and server action
So, your client doesn't receive { success, errorMessage } — it gets a redirect response instead → resulting in undefined state and no redirect login. What makes it tricky. The request never reaches server actions so using middleware for auth pages during page naviation works to redirect the user to logn but not during server action. |
|
Yes. I believe this is serious
Kind Regards,
[image: image]
Kenna Taddese
Software Developer
Phone. +1 (530) 712-3425
Web. Babure.rf.gd
Address. Level 9, Abraham Street, WA 6029
[image: facebook] <http://www.facebook.com/> [image: twitter]
<http://www.twitter.com/> [image: linkedin] <http://www.instagram.com/> [image:
google-plus] ***@***.***/>
Get your own signature
<https://gimm.io/en_US/email-signature-generator?utm_source=sent-emails&utm_medium=email&utm_campaign=get-your-own-signature>
…On Sat, Jul 12, 2025, 8:46 PM gmoniava ***@***.***> wrote:
@ztanner <https://github.com/ztanner> Currently I have similar issue a
client component executes a server action, which triggers middleware,
inside it, since the user is not authenticated - redirect is called. Which
results in the server function call returning undefined on the client
component. Basically redirect does not work with server functions. So what
should we do in such cases? I have auth check inside the server function
but it never reaches it due to redirect (but neither redirect happens).
IMHO server function don't start with api.
—
Reply to this email directly, view it on GitHub
<#64993 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A4EDG3G6V6J7QUG3VA4D7BL3IFCY7AVCNFSM6AAAAABGXNZMBKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTGNZUGAYTCMQ>
.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
@ehis, yep, no need to handle in middleware! You can think of it similar to an API route where you'd have the auth check at the top of the request handler.