Skip to content

Condiontally homepage redirect based on domain and logged in status #12147

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

Merged
merged 1 commit into from
May 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions readthedocs/core/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,45 @@ def get(self, request, *_, **__):

class HomepageView(TemplateView):
"""
Conditionally show the home page or redirect to the login page.
Conditionally redirect to website home page or to dashboard.

On the current dashboard, this shows the application homepage. However, we
no longer require this page in our application as we have a similar page on
our website. Instead, redirect to our login page on the new dashboard.

User hitting readthedocs.org / readthedocs.com
1. when user is logged in, redirect to dashboard
2. when user is logged off, redirect to https://about.readthedocs.com/

User hitting app.readthedocs.org / app.readthedocs.com
1. when user is logged in, redirect to dashboard
2. when user is logged off, redirect to login page
"""

template_name = "homepage.html"

def get(self, request, *args, **kwargs):
# Redirect to login page
return redirect(reverse("account_login"))

# Request hitting the old domain (readthedocs.org / readthedocs.com)
if request.get_host() == settings.PRODUCTION_DOMAIN.replace("app.", ""):
# Redirect to user dashboard for logged in users
if request.user.is_authenticated:
return redirect("projects_dashboard")
Comment on lines +59 to +60
Copy link
Member Author

Choose a reason for hiding this comment

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

Does this work? I mean, how do we check if the user is logged in under app.readthedocs.org if they are hitting readthedocs.org? cc @agjohnson

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, with no web instances anymore, and only instances set up with a production domain of app.readthedocs.org, those instances are likely going to handle requests for readthedocs.org as a project custom domain through proxito.

So I'm guessing this approach won't work, but I'd still test this to make sure.

But we probably needed the web ASG, with instances listening for the domain readthedocs.org, to support doing anything dynamic with requests to the old domain.

If this does work, any redirect() probably should have an explicit domain.

Copy link
Member Author

Choose a reason for hiding this comment

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

app.readthedocs.org and readthedocs.org will hit web-ext-theme proxito instances. I understand that to check if the user is logged in or not, the setting SESSION_COOKIE_DOMAIN and SESSION_COOKIE_NAME are used and on both requests they will have the same values -- so, I think this should work as expected.


# Redirect to ``about.`` in production
if not settings.DEBUG:
query_string = f"?ref={settings.PRODUCTION_DOMAIN}"
if request.META["QUERY_STRING"]:
# Small hack to not append `&` to URLs without a query_string
query_string += "&" + request.META["QUERY_STRING"]

# Do a 302 here so that it varies on logged in status
return redirect(f"https://about.readthedocs.com/{query_string}", permanent=False)

# Request hitting app.readthedocs.org / app.readthedocs.com
#
# Redirect to user dashboard for logged in users
if request.user.is_authenticated:
return redirect("projects_dashboard")

# Redirect to ``about.`` in production
if not settings.DEBUG:
query_string = f"?ref={settings.PRODUCTION_DOMAIN}"
if request.META["QUERY_STRING"]:
# Small hack to not append `&` to URLs without a query_string
query_string += "&" + request.META["QUERY_STRING"]

# Do a 302 here so that it varies on logged in status
return redirect(f"https://about.readthedocs.com{query_string}", permanent=False)

# Show the homepage for local dev
return super().get(request, *args, **kwargs)
# Redirect to login page
return redirect(reverse("account_login"))


class SupportView(PrivateViewMixin, TemplateView):
Expand Down