python-social-auth AuthCanceled exception python-social-auth AuthCanceled exception django django

python-social-auth AuthCanceled exception


python-social-auth is a newer, derived version of django-social-auth.

AlexYar's answer can be slightly modified to work with python-social-auth by modify settings.py with following changes:

  1. Add a middleware to handle the SocialAuthException

    MIDDLEWARE_CLASSES += (    'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',)
  2. URL to redirect to, when an exception occurred

    SOCIAL_AUTH_LOGIN_ERROR_URL = '/'
  3. Note that you also need to set

    DEBUG = False

That's all or read http://python-social-auth.readthedocs.org/en/latest/configuration/django.html#exceptions-middleware


you can create a middleware and catch any exceptions, exception list: https://github.com/omab/python-social-auth/blob/master/social/exceptions.pyin this case your AuthCanceled Exception.

middleware.py

from social.apps.django_app.middleware import SocialAuthExceptionMiddlewarefrom django.shortcuts import HttpResponsefrom social import exceptions as social_exceptionsclass SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):    def process_exception(self, request, exception):        if hasattr(social_exceptions, 'AuthCanceled'):            return HttpResponse("I'm the Pony %s" % exception)        else:            raise exception

settings.py

MIDDLEWARE_CLASSES = (        .....        'pat_to_middleware.SocialAuthExceptionMiddleware',)


This is slight modification of @Nicolas answer and this works for me.

middleware.py

from social.apps.django_app.middleware import SocialAuthExceptionMiddlewarefrom django.shortcuts import renderfrom social.exceptions import AuthCanceledclass SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):    def process_exception(self, request, exception):        if type(exception) == AuthCanceled:            return render(request, "pysocial/authcancelled.html", {})        else:            pass

settings.py

MIDDLEWARE_CLASSES += ('myapp.middleware.SocialAuthExceptionMiddleware',)