Language not re-applied on login with social-app-django #11888

Open
opened 2025-12-29 21:51:14 +01:00 by adam · 2 comments
Owner

Originally created by @dminuoso on GitHub (Dec 3, 2025).

NetBox Edition

NetBox Community

NetBox Version

v4.2.9

Python Version

3.10

Steps to Reproduce

  1. Configure django-social-app with any provider
  2. Set your language under Preferences > Language to any language
  3. Log out
  4. Log in again

Expected Behavior

The language setting should be re-applied upon login

Observed Behavior

The language setting is not re-applied upon login.

Originally created by @dminuoso on GitHub (Dec 3, 2025). ### NetBox Edition NetBox Community ### NetBox Version v4.2.9 ### Python Version 3.10 ### Steps to Reproduce 1. Configure django-social-app with any provider 2. Set your language under `Preferences > Language` to any language 3. Log out 4. Log in again ### Expected Behavior The language setting should be re-applied upon login ### Observed Behavior The language setting is not re-applied upon login.
adam added the type: bugstatus: needs ownernetboxseverity: low labels 2025-12-29 21:51:14 +01:00
Author
Owner

@dminuoso commented on GitHub (Dec 3, 2025):

We have not updated to 4.4.7 yet due to some compatibility issues, but based on the source code I'm confident the problem will reproduce on 4.4.7

The regular language switch upon Login happens in LoginView

922b08c0ff/netbox/account/views.py (L129-L135)

But for django-social-auth this never happens because the views from social-app-django are used instead.

It seems impossible to do this from within SOCIAL_AUTH_PIPELINE - while the pipeline can shortcircuit with a HttpResponse (which could set the above cookie), it prevents completing authentication.

There seem to be two obvious solutions to this

  1. Schedule __set_lang_cookie in request.session in a user_logged_in signal, and then set the cookie in an extra Middleware
  2. Wrap the appropriate view from social-app-django
@dminuoso commented on GitHub (Dec 3, 2025): We have not updated to 4.4.7 yet due to some compatibility issues, but based on the source code I'm confident the problem will reproduce on 4.4.7 The regular language switch upon Login happens in LoginView https://github.com/netbox-community/netbox/blob/922b08c0ffaa3779f0cd9ce2314b701a38b6f7d0/netbox/account/views.py#L129-L135 But for django-social-auth this never happens because the views from social-app-django are used instead. It seems impossible to do this from within SOCIAL_AUTH_PIPELINE - while the pipeline can shortcircuit with a HttpResponse (which could set the above cookie), it prevents completing authentication. There seem to be two obvious solutions to this 1. Schedule `__set_lang_cookie` in request.session in a user_logged_in signal, and then set the cookie in an extra Middleware 2. Wrap the appropriate view from social-app-django
Author
Owner

@dminuoso commented on GitHub (Dec 3, 2025):

We're using 1) right now as a workaround.

from django.conf import settings

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

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

        lang = request.session.pop('__set_lang_cookie', None)
        if lang:
            response.set_cookie(
                key=settings.LANGUAGE_COOKIE_NAME,
                value=lang,
                max_age=request.session.get_expiry_age(),
                secure=settings.SESSION_COOKIE_SECURE,
            )

        return response
from django.contrib.auth.signals import user_logged_in
def set_language_cookie(sender, user, request, **kwargs):
    lang = user.config.get('locale.language')
    if lang:
        request.session['__set_lang_cookie'] = lang

user_logged_in.connect(set_language_cookie)
@dminuoso commented on GitHub (Dec 3, 2025): We're using 1) right now as a workaround. ```python from django.conf import settings class LanguageCookieMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): response = self.get_response(request) lang = request.session.pop('__set_lang_cookie', None) if lang: response.set_cookie( key=settings.LANGUAGE_COOKIE_NAME, value=lang, max_age=request.session.get_expiry_age(), secure=settings.SESSION_COOKIE_SECURE, ) return response ``` ```python from django.contrib.auth.signals import user_logged_in def set_language_cookie(sender, user, request, **kwargs): lang = user.config.get('locale.language') if lang: request.session['__set_lang_cookie'] = lang user_logged_in.connect(set_language_cookie) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11888