Skip to content

Save last login method in a cookie #12286

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 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
35 changes: 35 additions & 0 deletions readthedocs/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,38 @@ def __call__(self, request):
response._csp_update = update_csp_headers[url_name]

return response


class LoginMethodCookie:
"""
Set a cookie with the login method used by the user.

This is used by the templates to put a small "Last used" label next to the method used.
"""

def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
return response

def process_template_response(self, request, response):
response.context_data = response.context_data or {}
last_login_method = request.COOKIES.get("last-login-method")
response.context_data["last_login_method"] = last_login_method

last_login_tab = None
if last_login_method == "email":
last_login_tab = "email"
if last_login_method in ("githubapp", "github", "gitlab", "bitbucket_oauth2", "google"):
last_login_tab = "vcs"
if last_login_method == "sso":
last_login_tab = "sso"
log.debug(
"Login method.",
last_login_method=last_login_method,
last_login_tab=last_login_tab,
)
response.context_data["last_login_tab"] = last_login_tab
return response
1 change: 1 addition & 0 deletions readthedocs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def MIDDLEWARE(self):
"readthedocs.core.middleware.UpdateCSPMiddleware",
"simple_history.middleware.HistoryRequestMiddleware",
"readthedocs.core.logs.ReadTheDocsRequestMiddleware",
"readthedocs.core.middleware.LoginMethodCookie",
"django_structlog.middlewares.RequestMiddleware",
]
if self.SHOW_DEBUG_TOOLBAR:
Expand Down