Skip to content

feat: validation in routes to redirect to login if user is not auth#183

Draft
jesusbalderramawgu wants to merge 4 commits intoopenedx:mainfrom
WGU-Open-edX:login-redirection
Draft

feat: validation in routes to redirect to login if user is not auth#183
jesusbalderramawgu wants to merge 4 commits intoopenedx:mainfrom
WGU-Open-edX:login-redirection

Conversation

@jesusbalderramawgu
Copy link

Description

PR to add validation in the different routes from the projects that consume the frontend-base package
this closes the issue142 and 154

How Has This Been Tested?

In the project that needs to use the mechanism and redirect any unauthenticated user to login page we need to add a flag to any route that required this validation. requireAuthenticatedUser: true,

Example from the learner dashboard
image

Steps to reproduce

1.- if you open the learner dashboard url without a user logged in, and without the new flag in the router you will see the learner dashboard page in an incorrect state because no user is authenticated.
Screenshot 2026-03-09 at 9 25 55 a m

2.- If you do the same but with the new flag added into the route you will see the correct redirection to the login page.
Screenshot 2026-03-09 at 9 33 34 a m

Note:

This works properly, but I marked this PR as draft because it shows the header and the footer for just a moment ( not sure if that's fine right now and we can tackle that in other PR or if that's a blocker to merge this)
This happens because basically the route controls the main-content but the header and footer are always visible.
Screenshot 2026-03-09 at 9 32 43 a m

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Mar 9, 2026
@openedx-webhooks
Copy link

openedx-webhooks commented Mar 9, 2026

Thanks for the pull request, @jesusbalderramawgu!

This repository is currently maintained by @openedx/axim-engineering.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

🔘 Update the status of your PR

Your PR is currently marked as a draft. After completing the steps above, update its status by clicking "Ready for Review", or removing "WIP" from the title, as appropriate.


Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@jesusbalderramawgu jesusbalderramawgu marked this pull request as draft March 9, 2026 15:34
@github-project-automation github-project-automation bot moved this to Needs Triage in Contributions Mar 9, 2026
return;
}

// user already authenticated
Copy link
Contributor

Choose a reason for hiding this comment

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

a bit confusing isn't it?

I mean, you are cheking if it's already authenticated....

Copy link
Contributor

@arbrandes arbrandes left a comment

Choose a reason for hiding this comment

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

Before I dig any deeper: I should have noted before that we already have a component that has the same purpose:

https://github.com/openedx/frontend-base/blob/login-redirection/runtime/react/AuthenticatedPageRoute.jsx

We should either use it as-is if possible, or modify it if not, instead of creating a new one. For instance, one problem I see with it is that it doesn't navigate(), it sets the location manually.

Other changes to the auth infrastructure might be necessary, but this is as good a time to make them as any!

@jesusbalderramawgu
Copy link
Author

Before I dig any deeper: I should have noted before that we already have a component that has the same purpose:

https://github.com/openedx/frontend-base/blob/login-redirection/runtime/react/AuthenticatedPageRoute.jsx

We should either use it as-is if possible, or modify it if not, instead of creating a new one. For instance, one problem I see with it is that it doesn't navigate(), it sets the location manually.

Other changes to the auth infrastructure might be necessary, but this is as good a time to make them as any!

I used the component you mentioned, I found it here
https://github.com/openedx/frontend-base/blob/main/runtime/react/AuthenticatedPageRoute.jsx
I removed my AuthGuard component and I use this instead, I checked that still works with this component.
Now it is easier because I just check if I have the property requireAuthenticatedUser in the current route if so I use the existing component to validate wether the user has or not a session to make the redirection.
If the property requireAuthenticatedUser is not sent it I do not use that component and continue the regular flow which can be useful for some paths.
Given that the existing component AuthenticatedPageRoute can receive an optional url to redirect (redirectUrl)
I made also a change to support that so we can send that prop as well per route if don't need it we can omit it and the default redirect url will be the login page.
Let me know if you like this approach and thank you for your feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

4 participants