Skip to content
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

💻 Use subdomains to navigate through languages #5829

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open

Conversation

jpelay
Copy link
Member

@jpelay jpelay commented Oct 3, 2024

Makes it possible to set the language through subdomains. In order to do this the following changes were made:

  • Every route in the page was duplicated to include a subdomain argument, otherwise pages wouldn't be accesible without being prefixed with the subdomain.
  • The set up language function was edited to take the subdomain as a parameter

Fixes #5720

How to test

In order to test this locally, we need to set an env variable that has a dot in it. The one I used was hedy.local, this name should be added to the hosts file in your system prefixed with the languages you want to test, for example

127.0.0.1 es.hedy.local hedy.local en.hedy.local

After doing this, before starting the server, you need to set the DOMAIN_NAME env variable export DOMAIN_NAME=hedy.local

After doing this you are set up to test the feature. You can navigate to nl.hedy.local:8080 and see that the language changes, and change it through the language selector. Also log in as a user, and change the preferred language there

@jpelay jpelay marked this pull request as draft October 3, 2024 22:58
@jpelay
Copy link
Member Author

jpelay commented Oct 3, 2024

I'm figuring out how to share sessions between the different subdomains.

@jpelay
Copy link
Member Author

jpelay commented Oct 8, 2024

I'm figuring out how to share sessions between the different subdomains.

Figured it out 😄

@jpelay jpelay marked this pull request as ready for review November 7, 2024 23:05
@jpelay
Copy link
Member Author

jpelay commented Nov 8, 2024

@boryanagoncharenko This is done 😄 maybe you could take a look at this and check that it doesn't break anything!

Copy link
Collaborator

@boryanagoncharenko boryanagoncharenko left a comment

Choose a reason for hiding this comment

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

There are 3 things, which seem off when I am testing locally with hedy.local set in the hosts file and as an env variable:

  1. When I am at a subdomain, e.g. ar.hedy.local, and I login with an account that has a preferred language set to Bulgarian, the language of the profile gets changed to Arabic.
  2. If I am still at ar.hedy.local, I am logged in and I try to change the language of the user profile (say, to Spanish), the language seems to be correctly selected in the drop-down, the page correctly reloads, but the subdomain is not changed so whole website still shows the content in the subdomain language, i.e. Arabic. If I reload the page, the language I selected is now reset to the subdomain language. So, effectively I have no means to change my profile language if I am within a specific subdomain.
  3. When I log out, it seems that I only log out for the current subdomain. So if I have a session in the es.hedy.local, then I switch to en.hedy.local and log out, I might be under the impression that I have logged out, but navigating to the es.hedy.local shows me I am have not logged out. This might be a particular issue for environments in which users share computers:
    https://github.com/user-attachments/assets/962dc72e-32d4-4a7f-89fb-58e3e6453112

Ah, and one more thing, not so important. When I have the DOMAIN_NAME set to hedy.local, navigating to localhost fails because the langauge is present but its valid is "". We could change def before_route(endpoint, values): to check for that specific value and in this way we would support that too. Definitely not that important, though.

config.py Show resolved Hide resolved
config.py Show resolved Hide resolved
@@ -653,14 +666,16 @@ def preview_teacher_mode(self):
return redirect("/for-teachers")

@route("/exit-preview-teacher-mode", methods=["GET"])
@route("/exit-preview-teacher-mode", methods=["GET"], subdomain="<subdomaon>")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think there is a typo: "<subdomaon>" should be "<language>"? Also, shouldn't there be a language="en" parameter to the exit_teacher_mode method?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! Thank you so much

@boryanagoncharenko boryanagoncharenko changed the title 💻 Use subdomains to navigate trhough languages 💻 Use subdomains to navigate through languages Nov 11, 2024
@boryanagoncharenko boryanagoncharenko self-assigned this Nov 11, 2024
@jpelay
Copy link
Member Author

jpelay commented Nov 12, 2024

Thank you for your review, Boryana!

When I am at a subdomain, e.g. ar.hedy.local, and I login with an account that has a preferred language set to Bulgarian, the language of the profile gets changed to Arabic.
If I am still at ar.hedy.local, I am logged in and I try to change the language of the user profile (say, to Spanish), the language seems to be correctly selected in the drop-down, the page correctly reloads, but the subdomain is not changed so whole website still shows the content in the subdomain language, i.e. Arabic. If I reload the page, the language I selected is now reset to the subdomain language. So, effectively I have no means to change my profile language if I am within a specific subdomain.

Yes you're right, thanks for pointing it out. It should be fixed by now.

When I log out, it seems that I only log out for the current subdomain. So if I have a session in the es.hedy.local, then I switch to en.hedy.local and log out, I might be under the impression that I have logged out, but navigating to the es.hedy.local shows me I am have not logged out. This might be a particular issue for environments in which users share computers:
https://github.com/user-attachments/assets/962dc72e-32d4-4a7f-89fb-58e3e6453112

You're right 😢 handling the session across the different subdomains is tricky, I managed to maintain the user logged in across sessions but didn't take into account logging out.

See how when changing subdomains a new session is created
image

its all very weird

Ah, and one more thing, not so important. When I have the DOMAIN_NAME set to hedy.local, navigating to localhost fails because the langauge is present but its valid is "". We could change def before_route(endpoint, values): to check for that specific value and in this way we would support that too. Definitely not that important, though.

will think about this!

@jpelay
Copy link
Member Author

jpelay commented Nov 12, 2024

Change the SESSION_COOKIE_SAMESITE improves things a bit, but Im not too sure how to logged out from all subdomains,

@jpelay
Copy link
Member Author

jpelay commented Nov 13, 2024

@boryanagoncharenko Thank you for your input, I fixed the problem with the lougout in different subdomains 😄

Copy link
Collaborator

@boryanagoncharenko boryanagoncharenko left a comment

Choose a reason for hiding this comment

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

Thank you for the explanation and the changes.

Now, if I am in a specific language (en.hedy.local) and I log in with a user that has a different language preference (bg.hedy.local), I get redirected to the language of the user (bg.hedy.local). Also, when I change the language of the user and the page reloads, I land on the right domain (changing en to es redirects me to es.hedy.local).

However, I am still having an issue with the logout. Here are my steps:

  • clear cookies, sessions, local storage, all of that
  • go to bg.hedy.local and login with a user that uses bg as a language
  • open en.hedy.local (directly changed in the webbrowser)
  • open es.hedy.local (again, by changing the URL directly in the browser)
  • logout
  • click the browser's back button to go to en.hedy.local. At this point, I look as if I am logged in, but if I try to navigate to my-profile, I will be logged out.
  • click the back button again (twice) to go to bg.hedy.local. Unfortunately, there I am logged in and I can access the account.

Here is a video:
https://github.com/user-attachments/assets/e559c935-d2c1-4444-94cf-ec8d2b8c5620

@jpelay
Copy link
Member Author

jpelay commented Nov 18, 2024

Here is a video: https://github.com/user-attachments/assets/e559c935-d2c1-4444-94cf-ec8d2b8c5620

Yes, you're right, for some reason in the browser I was trying, things did work, but not in Firefox or Chrome. Can you try again? I think I fixed it. There's a problem though, apparently one can't set or unset the main domain cookie from a subdomain, thus, if I log out from en.hedy.local I'm still logged in in hedy.local, do you have any ideas on how we can do that? I think perhaps some sort of redirection would work, although that's ugly!

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

Successfully merging this pull request may close these issues.

💻 change links for translated content
2 participants