The social account adapter allows you to customize core behaviors throughout the authentication flow. Create a custom adapter by subclassing DefaultSocialAccountAdapter.
from allauth.socialaccount.adapter import DefaultSocialAccountAdapterclass MySocialAccountAdapter(DefaultSocialAccountAdapter): def is_open_for_signup(self, request, socialaccount): """ Control whether new signups are allowed. Return False to disable social signups. """ return True def new_user(self, request, sociallogin): """ Instantiate a new User instance. Override to use a custom user model or set defaults. """ user = super().new_user(request, sociallogin) # Set custom defaults user.is_active = True return user def save_user(self, request, sociallogin, form=None): """ Save the user instance. Override to add custom logic before/after saving. """ user = super().save_user(request, sociallogin, form) # Add custom logic return user def populate_user(self, request, sociallogin, data): """ Populate user instance with data from provider. Args: data: Dictionary with common fields (first_name, last_name, email, username, name) """ user = super().populate_user(request, sociallogin, data) # Add custom field mapping if 'company' in sociallogin.account.extra_data: user.company = sociallogin.account.extra_data['company'] return user def get_connect_redirect_url(self, request, socialaccount): """ Return URL to redirect to after connecting a social account. """ return '/dashboard/accounts/'
Restrict signups based on provider, domain, or other criteria:
class MySocialAccountAdapter(DefaultSocialAccountAdapter): def is_open_for_signup(self, request, socialaccount): # Only allow signups from Google if socialaccount.provider != 'google': return False # Only allow company email domains email = socialaccount.account.extra_data.get('email', '') if not email.endswith('@company.com'): return False return True
from django.contrib.auth.models import Groupclass MySocialAccountAdapter(DefaultSocialAccountAdapter): def save_user(self, request, sociallogin, form=None): user = super().save_user(request, sociallogin, form) # Assign role based on provider if sociallogin.account.provider == 'google': group = Group.objects.get(name='Google Users') user.groups.add(group) # Assign role based on email domain email = user.email if email.endswith('@company.com'): group = Group.objects.get(name='Staff') user.groups.add(group) return user
class MySocialAccountAdapter(DefaultSocialAccountAdapter): def get_connect_redirect_url(self, request, socialaccount): """ Called after successfully connecting a social account. """ if socialaccount.provider == 'github': return '/dashboard/github-connected/' elif socialaccount.provider == 'google': return '/dashboard/google-connected/' return '/dashboard/accounts/'
class MySocialAccountAdapter(DefaultSocialAccountAdapter): def get_login_redirect_url(self, request): """ Called after successful social login. """ user = request.user # Redirect based on user type if user.is_staff: return '/admin/dashboard/' elif user.groups.filter(name='Premium').exists(): return '/premium/dashboard/' else: return '/dashboard/'
class CustomGoogleProvider(GoogleProvider): id = 'google_custom' def get_scope(self, request): scopes = super().get_scope(request) # Add calendar scope for premium users if request.user.is_authenticated and request.user.is_premium: scopes.append('https://www.googleapis.com/auth/calendar') return scopes
from allauth.account.models import EmailAddressclass MySocialAccountAdapter(DefaultSocialAccountAdapter): def save_user(self, request, sociallogin, form=None): user = super().save_user(request, sociallogin, form) # Mark email as verified for specific providers if sociallogin.account.provider in ['google', 'microsoft']: email = user.email EmailAddress.objects.filter( user=user, email=email ).update(verified=True) return user
from allauth.socialaccount.signals import ( pre_social_login, social_account_added, social_account_updated, social_account_removed,)from django.dispatch import receiver@receiver(pre_social_login)def before_social_login(sender, request, sociallogin, **kwargs): """ Called before a social login is processed. Raise an exception to prevent login. """ # Check if email is blacklisted email = sociallogin.account.extra_data.get('email') if email and is_blacklisted(email): raise ImmediateHttpResponse( HttpResponse('Email domain not allowed') )@receiver(social_account_added)def after_account_connected(sender, request, sociallogin, **kwargs): """ Called after a social account is connected to a user. """ user = sociallogin.user provider = sociallogin.account.provider # Log the connection logger.info(f'User {user.id} connected {provider} account') # Award points for connecting account user.points += 10 user.save()@receiver(social_account_updated)def after_account_updated(sender, request, sociallogin, **kwargs): """ Called when social account data is updated. """ # Sync profile data user = sociallogin.user extra_data = sociallogin.account.extra_data if 'avatar_url' in extra_data: user.avatar_url = extra_data['avatar_url'] user.save()@receiver(social_account_removed)def after_account_disconnected(sender, request, socialaccount, **kwargs): """ Called after a social account is disconnected. """ user = socialaccount.user provider = socialaccount.provider logger.info(f'User {user.id} disconnected {provider} account')
from django.http import HttpResponsefrom allauth.exceptions import ImmediateHttpResponse@receiver(pre_social_login)def check_user_eligibility(sender, request, sociallogin, **kwargs): # Only allow users from specific domain email = sociallogin.account.extra_data.get('email', '') if not email.endswith('@company.com'): raise ImmediateHttpResponse( HttpResponse('Only company employees can sign in', status=403) ) # Check against external API if not is_authorized_user(email): raise ImmediateHttpResponse( HttpResponse('User not authorized', status=403) )
class MySocialAccountAdapter(DefaultSocialAccountAdapter): def get_app(self, request, provider, client_id=None): """ Return the appropriate app based on request context. """ # Determine tenant from domain tenant = get_tenant_from_request(request) # Get app for this tenant from allauth.socialaccount.models import SocialApp return SocialApp.objects.get( provider=provider, settings__tenant=tenant.id )