Skip to content

Fix login redirection to requested page after authentication #1912

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Nowaker
Copy link

@Nowaker Nowaker commented May 8, 2025

Problem

When a user attempts to access a protected resource without being logged in, they are correctly redirected to the login page with a next parameter preserving their intended destination (e.g., /?next=%2Fwb0e9nB5T3qecF27Y4xlsg). However, after successful authentication, the application always redirects to the dashboard/homepage instead of returning the user to their originally requested URL.

This creates a poor user experience as users must manually navigate back to the content they were originally trying to access.

Solution

The root cause was identified in the email authentication implementation where the successReturnToOrRedirect parameter was hardcoded to always redirect to the server's homepage.

successReturnToOrRedirect: config.serverURL + '/',

This change properly utilizes the req.session.returnTo value that's already being correctly set by the setReturnToFromReferer function:

// After: Redirect to originally requested page or homepage if none
successReturnToOrRedirect: req.session.returnTo,

Testing

Manually tested the authentication flow by:

  1. Attempting to access a protected note while logged out
  2. Confirming redirection to login page with proper next parameter
  3. Logging in with valid credentials
  4. Verifying successful redirection to the originally requested note

This fix respects the standard behavior of Passport.js's successReturnToOrRedirect parameter, which will redirect to the URL stored in req.session.returnTo if available, or fall back to the configured redirect URL otherwise.

@jackycute
Copy link
Member

Hi @Nowaker,

Thank you for your PR!
Please also sign off you commit and force push again :)

git rebase --signoff -i develop

P.S. Glad to see you here, howdy!

@Nowaker Nowaker force-pushed the fix-redirect-after-login branch from 352d691 to d47cd95 Compare May 11, 2025 02:49
@Nowaker
Copy link
Author

Nowaker commented May 11, 2025

@jackycute Howdy howdy! :)

@jackycute jackycute requested a review from Yukaii May 15, 2025 06:15
@Yukaii Yukaii added this to the 2.6.0 milestone May 15, 2025
Copy link
Member

@Yukaii Yukaii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nowaker Could you also update other authentication provider, like

successReturnToOrRedirect: config.serverURL + '/',

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants