Search code examples
python-social-auth

python social auth redirect to an error page conditionally


Am using python social auth with django to create authentication and registration via social media. I've defined the

LOGIN_ERROR_URL = '/account/auth-failed/'

it works well when there is a problem, it would redirect there correctly. Yet, I want to conditionally specify the LOGIN_ERROR_PAGE at run time because I have a scneario where its invoked via JSON or simple HTML.

Any suggestion on how to do that?


Solution

  • It's not really simple to implement, but you can override the default strategy and in your custom version override the get_setting() method, like this:

    from django.conf import settings
    
    from social.strategies.django_strategy import DjangoStrategy
    
    
    class CustomDjangoStrategy(DjangoStrategy):
        def get_setting(self, name):
            if name == 'LOGIN_ERROR_URL' and self.request.is_ajax():
                return '/auth/error/ajax'
            else:
                return getattr(settings, name)
    

    Put that into a module and define SOCIAL_AUTH_STRATEGY = app.module.CustomDjangoStrategy in your settings.