11

How do I handle this kind of exception AuthAlreadyAssociated on Python Social Auth?

All the answers I found are for Django Social Auth but it seems plenty has changed ever since they were written.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Juliana
  • 578
  • 7
  • 18

1 Answers1

16

You can create a new middleware in your app's middleware.py:

from social_django.middleware import SocialAuthExceptionMiddleware
from social_core import exceptions as social_exceptions     
from django.http import HttpResponse

class MySocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
    def process_exception(self, request, exception):
        if hasattr(social_exceptions, exception.__class__.__name__):
            # Here you can handle the exception as you wish
            return HttpResponse("Exception %s while processing your social account." % exception)
        else:
            return super(MySocialAuthExceptionMiddleware, self).process_exception(request, exception)

and add its path to settings.py

MIDDLEWARE_CLASSES = (
    ...
    'path.to.MySocialAuthExceptionMiddleware',
   )
Ícaro
  • 1,432
  • 3
  • 18
  • 36
pvieytes
  • 420
  • 5
  • 8
  • if suppose the problem in some place but it will raise here .. – Raja Simon Dec 09 '14 at 07:09
  • Use the following example if you want to explicitly handle the AuthAlreadyAssociated exception. ```if isinstance(exception, social_exceptions.AuthAlreadyAssociated):```. And redirect for instance: ```return redirect(reverse('index'))```. – Tim Aug 07 '19 at 19:04