Проблема с входом в Google с использованием Django allauth: не предлагается выбрать учетную запись Google в режиме инког ⇐ Python
Проблема с входом в Google с использованием Django allauth: не предлагается выбрать учетную запись Google в режиме инког
I'm using Django allauth for Google login in my web application. Everything works fine, but I've encountered an issue where when users try to log in using Google in Incognito mode or with certain browsers like Opera or Edge, they are not prompted to select a Google account. Instead, it automatically logs in with the previously logged-in Google account.
Here's a snippet of my settings in settings.py:
SITE_ID = 1 SOCIALACCOUNT_ADAPTER = 'microdistribution.users.adapters.CustomSocialAccountAdapter' ACCOUNT_USER_MODEL_USERNAME_FIELD = None ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_USERNAME_REQUIRED = False ACCOUNT_AUTHENTICATION_METHOD = 'email' SOCIALACCOUNT_LOGIN_ON_GET=True ACCOUNT_LOGOUT_ON_GET = True SOCIALACCOUNT_EMAIL_AUTHENTICATION = True ACCOUNT_SESSION_REMEMBER = False SOCIALACCOUNT_PROVIDERS = { 'google': { 'AUTH_PARAMS': {'access_type': 'online'}, } } MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'allauth.account.middleware.AccountMiddleware', #new 'microdistribution.utils.middleware.DynamicSiteMiddleware', #new ] INSTALLED_APPS = [ 'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount', 'allauth.socialaccount.providers.google', ] Custom Adapter im using class CustomSocialAccountAdapter(DefaultSocialAccountAdapter): def save_user(self, request, sociallogin, form=None): user = sociallogin.user # Check if the user already exists existing_user = self.get_existing_user(user) if existing_user: return existing_user # If the user doesn't exist, create a new one try: new_user = super().save_user(request, sociallogin, form) new_user.name = sociallogin.account.extra_data.get('name') new_user.client_uid_id = User.objects.get(id = settings.CLIENT[request.META['HTTP_HOST']].client_uid_id) new_user.save() return new_user except Exception as e: print(f"Error While Creating User: {e}") return None def get_existing_user(self, user): existing_user = None try: client_uid_id = User.objects.get(id = settings.CLIENT[self.request.META['HTTP_HOST']].client_uid_id) existing_user = User.objects.get(email=user.email, client_uid_id = client_uid_id) print("existing user", existing_user) except User.DoesNotExist: pass return existing_user Middleware class DynamicSiteMiddleware(MiddlewareMixin): def process_request(self, request): try: current_site = Site.objects.get(domain=request.get_host()) except Site.DoesNotExist: current_site = Site.objects.get(id=settings.SITE_ID) request.site = current_site settings.SITE_ID = current_site.id response = self.get_response(request) return response I tried using ACCOUNT_SESSION_REMEMBER = False and that did not fix anything.
Источник: https://stackoverflow.com/questions/781 ... oogle-acco
I'm using Django allauth for Google login in my web application. Everything works fine, but I've encountered an issue where when users try to log in using Google in Incognito mode or with certain browsers like Opera or Edge, they are not prompted to select a Google account. Instead, it automatically logs in with the previously logged-in Google account.
Here's a snippet of my settings in settings.py:
SITE_ID = 1 SOCIALACCOUNT_ADAPTER = 'microdistribution.users.adapters.CustomSocialAccountAdapter' ACCOUNT_USER_MODEL_USERNAME_FIELD = None ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_USERNAME_REQUIRED = False ACCOUNT_AUTHENTICATION_METHOD = 'email' SOCIALACCOUNT_LOGIN_ON_GET=True ACCOUNT_LOGOUT_ON_GET = True SOCIALACCOUNT_EMAIL_AUTHENTICATION = True ACCOUNT_SESSION_REMEMBER = False SOCIALACCOUNT_PROVIDERS = { 'google': { 'AUTH_PARAMS': {'access_type': 'online'}, } } MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'allauth.account.middleware.AccountMiddleware', #new 'microdistribution.utils.middleware.DynamicSiteMiddleware', #new ] INSTALLED_APPS = [ 'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount', 'allauth.socialaccount.providers.google', ] Custom Adapter im using class CustomSocialAccountAdapter(DefaultSocialAccountAdapter): def save_user(self, request, sociallogin, form=None): user = sociallogin.user # Check if the user already exists existing_user = self.get_existing_user(user) if existing_user: return existing_user # If the user doesn't exist, create a new one try: new_user = super().save_user(request, sociallogin, form) new_user.name = sociallogin.account.extra_data.get('name') new_user.client_uid_id = User.objects.get(id = settings.CLIENT[request.META['HTTP_HOST']].client_uid_id) new_user.save() return new_user except Exception as e: print(f"Error While Creating User: {e}") return None def get_existing_user(self, user): existing_user = None try: client_uid_id = User.objects.get(id = settings.CLIENT[self.request.META['HTTP_HOST']].client_uid_id) existing_user = User.objects.get(email=user.email, client_uid_id = client_uid_id) print("existing user", existing_user) except User.DoesNotExist: pass return existing_user Middleware class DynamicSiteMiddleware(MiddlewareMixin): def process_request(self, request): try: current_site = Site.objects.get(domain=request.get_host()) except Site.DoesNotExist: current_site = Site.objects.get(id=settings.SITE_ID) request.site = current_site settings.SITE_ID = current_site.id response = self.get_response(request) return response I tried using ACCOUNT_SESSION_REMEMBER = False and that did not fix anything.
Источник: https://stackoverflow.com/questions/781 ... oogle-acco
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение