"""
Custom middleware for social authentication.
"""


class SocialAuthExceptionMiddleware:
    """
    Middleware to handle social auth exceptions and redirect properly.
    """
    
    def __init__(self, get_response):
        self.get_response = get_response
    
    def __call__(self, request):
        response = self.get_response(request)
        return response
    
    def process_exception(self, request, exception):
        """Handle social auth exceptions."""
        from social_core.exceptions import AuthException
        from django.http import HttpResponseRedirect
        
        if isinstance(exception, AuthException):
            return HttpResponseRedirect(f'/api/v1/auth/social/error/?error={str(exception)}')
        
        return None

