Python-social-auth Authcanceled Exception
Solution 1:
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:
Add a middleware to handle the SocialAuthException
MIDDLEWARE_CLASSES += ( 'social.apps.django_app.middleware.SocialAuthExceptionMiddleware', )
URL to redirect to, when an exception occurred
SOCIAL_AUTH_LOGIN_ERROR_URL = '/'
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
Solution 2:
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
from social.apps.django_app.middleware import SocialAuthExceptionMiddleware
from django.shortcuts import HttpResponse
from social import exceptions as social_exceptions
classSocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
defprocess_exception(self, request, exception):
ifhasattr(social_exceptions, 'AuthCanceled'):
return HttpResponse("I'm the Pony %s" % exception)
else:
raise exception
settings.py
MIDDLEWARE_CLASSES = (
.....
'pat_to_middleware.SocialAuthExceptionMiddleware',
)
Solution 3:
This is slight modification of @Nicolas answer and this works for me.
middleware.py
from social.apps.django_app.middleware import SocialAuthExceptionMiddleware
from django.shortcuts import render
from social.exceptions import AuthCanceled
classSocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
defprocess_exception(self, request, exception):
iftype(exception) == AuthCanceled:
return render(request, "pysocial/authcancelled.html", {})
else:
pass
settings.py
MIDDLEWARE_CLASSES += (
'myapp.middleware.SocialAuthExceptionMiddleware',
)
Solution 4:
The 2018 answer:
Add
SocialAuthExceptionMiddleware
middleware to your config:MIDDLEWARE_CLASSES = [ ... 'social_django.middleware.SocialAuthExceptionMiddleware', ]
Set
SOCIAL_AUTH_LOGIN_ERROR_URL
in your config:SOCIAL_AUTH_LOGIN_ERROR_URL = '/login'
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.
Solution 5:
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
Post a Comment for "Python-social-auth Authcanceled Exception"