I'm using python-social-auth in my Django application for authentication via Facebook. But when a user tries to login and when it's been refirected to Facebook app page clicks on "Cancel" button, appears the following exception:
ERROR 2014-01-03 15:32:15,308 base :: Internal Server Error: /complete/facebook/
Traceback (most recent call last):
File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
return view_func(*args, **kwargs)
File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/apps/django_app/utils.py", line 45, in wrapper
return func(request, backend, *args, **kwargs)
File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/apps/django_app/views.py", line 21, in complete
redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/actions.py", line 54, in do_complete
*args, **kwargs)
File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/strategies/base.py", line 62, in complete
return self.backend.auth_complete(*args, **kwargs)
File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/backends/facebook.py", line 63, in auth_complete
self.process_error(self.data)
File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/backends/facebook.py", line 56, in process_error
super(FacebookOAuth2, self).process_error(data)
File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/backends/oauth.py", line 312, in process_error
raise AuthCanceled(self, data.get('error_description', ''))
AuthCanceled: Authentication process canceled
Is the any way to catch it Django?
This is slight modification of @Nicolas answer and this works for me.
middleware.py
settings.py
Just add in
MIDDLEWARE_CLASSES = ( 'social_auth.middleware.SocialAuthExceptionMiddleware', )
and something like
LOGIN_ERROR_URL = '/'
That's all or read http://django-social-auth.readthedocs.org/en/latest/configuration.html#exceptions-middleware
The 2018 answer:
Add
SocialAuthExceptionMiddleware
middleware to your config:Set
SOCIAL_AUTH_LOGIN_ERROR_URL
in your config:Now when you have
DEBUG = False
, your users will get redirected to your login page when they click cancel in social auth provider's page.When
DEBUG = True
you will still see the error page in your browser during development.This is a updated imports middleware using social_django
you can create a middleware and catch any exceptions, exception list: https://github.com/omab/python-social-auth/blob/master/social/exceptions.py in this case your AuthCanceled Exception.
middleware.py
settings.py
If you don't care about handling the exception do the following in your
settings.py
SOCIAL_AUTH_RAISE_EXCEPTIONS = False
See this answer: How to solve Authentication process canceled error?