Skip to content

Commit 6b64298

Browse files
authored
Save last login method in a cookie (#12286)
Initial attempt to save the "last login method" into a cookie so we can show a visual label next to the button to login. This is only the backend part. We need to write some code in the template to use this value next. I'm not happy with the implementation because looks pretty complex. I didn't find an easier way to do it and I'm open for suggestions. Required by readthedocs/ext-theme#421
1 parent 391a5a0 commit 6b64298

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

readthedocs/profiles/views.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from enum import StrEnum
44
from enum import auto
55

6+
import structlog
67
from allauth.account.views import LoginView as AllAuthLoginView
78
from allauth.account.views import LogoutView as AllAuthLogoutView
9+
from allauth.socialaccount import providers
810
from allauth.socialaccount.providers.github.provider import GitHubProvider
911
from django.conf import settings
1012
from django.contrib import messages
@@ -52,8 +54,29 @@
5254
from readthedocs.projects.utils import get_csv_file
5355

5456

57+
log = structlog.get_logger(__name__)
58+
59+
5560
class LoginViewBase(AllAuthLoginView):
56-
pass
61+
def get_context_data(self, **kwargs):
62+
context_data = super().get_context_data(**kwargs)
63+
last_login_method = self.request.COOKIES.get("last-login-method")
64+
context_data["last_login_method"] = last_login_method
65+
66+
last_login_tab = "vcs" # Default tab"
67+
if last_login_method == "email":
68+
last_login_tab = "email"
69+
if last_login_method in providers.registry.provider_map.keys():
70+
last_login_tab = "vcs"
71+
if last_login_method == "sso":
72+
last_login_tab = "sso"
73+
log.debug(
74+
"Login method.",
75+
last_login_method=last_login_method,
76+
last_login_tab=last_login_tab,
77+
)
78+
context_data["last_login_tab"] = last_login_tab
79+
return context_data
5780

5881

5982
class LoginView(SettingsOverrideObject):

0 commit comments

Comments
 (0)