-
Notifications
You must be signed in to change notification settings - Fork 410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v4: Session is expired, but after Server Action is called i dont get redirected #1934
Comments
Thank you for reporting this issue with server actions and expired sessions! This looks like an interesting edge case with how Next.js Server Actions handle redirects from middleware. Based on the logs you've provided, the middleware is correctly identifying the expired session and attempting to redirect, but the redirect isn't being properly handled in the context of a server action. We're investigating this issue to better understand how redirects should be handled in this scenario. In the meantime, you could work around this by:
import { redirect } from 'next/navigation';
async function myServerAction() {
const session = await auth0.getSession();
if (!session) {
redirect('/auth/login');
}
// Rest of your server action
}
Could you share a simplified version of your server action code? This would help us reproduce and fix the issue more effectively. |
Hi @tusharpandey13 , ty for your comment. ![]() ![]() ![]() The simplified version of my server action:
Keep in mind that I still used your code (just checking if I have session and then redirecting, but it doesnt work). Im not sure if all of this happens because of:
|
Checklist
Description
I wanted to see how my app behaves when the session is expired and i call a server action.
In prev version (v3.5) server action would be called and then
await getAccessToken
would throw an error after which I would just redirect toapi/auth/login
. But in the current v4 the error is thrown before an action has started. Which is ok, Im creating a POST request when i want to call a server action and Auth0 can see in the middleware that I dont have session.The issue is that the middleware sees the session is null and calls:
I can see in the terminal that the new path is
/auth/login
but browser is still showing page from which I initiated Server Action. In the browser Network I can see 3 requests:My middleware looks like this:
My logs in the terminal:
Browser logs:
Reproduction
Additional context
next-intl
nextjs-auth0 version
v4.02
Next.js version
v15.1.7
Node.js version
v20.10.0
The text was updated successfully, but these errors were encountered: