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 1 commit
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
23 changes: 23 additions & 0 deletions readthedocs/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,26 @@ 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)
if request.user.is_authenticated and hasattr(request, "_last_login_method_used"):
response.set_cookie("last_login_method", request._last_login_method_used)
return response

def process_template_response(self, request, response):
response.context_data = response.context_data or {}
response.context_data["last_login_method"] = request.COOKIES.get("last_login_method")
# response.context_data["last_login_method"] = "github"
return response
25 changes: 25 additions & 0 deletions readthedocs/oauth/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,36 @@
from readthedocs.oauth.notifications import MESSAGE_PROJECTS_TO_MIGRATE_TO_GITHUB_APP
from readthedocs.oauth.tasks import sync_remote_repositories
from readthedocs.projects.models import Feature
from django.utils import timezone


log = structlog.get_logger(__name__)


@receiver(user_logged_in, sender=User)
def save_last_login_method_used(sender, request, user, *args, **kwargs):
"""Save the login method used by the user.

We don't know exactly what was the method used (regular user or social
account), so we check if any of the social account was used in the last 5
seconds, if so, we save it as last method used.

If we don't find a social account used in the last seconds, we set "email"
as login method.

The method is saved in `Request._last_login_method_used`. This attribute is
read in the middleware to set the cookie in the response.

Then, next time the user logs in, the middleware reads the cookie and
updates the context data.
"""
socialaccount = user.socialaccount_set.filter(last_login__gt=timezone.now() - timezone.timedelta(seconds=5)).first()
if socialaccount:
request._last_login_method_used = socialaccount.provider
else:
request._last_login_method_used = "email"


@receiver(user_logged_in, sender=User)
def sync_remote_repositories_on_login(sender, request, user, *args, **kwargs):
"""
Expand Down
1 change: 1 addition & 0 deletions readthedocs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ def MIDDLEWARE(self):
"readthedocs.core.middleware.UpdateCSPMiddleware",
"simple_history.middleware.HistoryRequestMiddleware",
"readthedocs.core.logs.ReadTheDocsRequestMiddleware",
"readthedocs.core.middleware.LoginMethodCookie",
"django_structlog.middlewares.CeleryMiddleware",
]
if self.SHOW_DEBUG_TOOLBAR:
Expand Down