python-social-auth AuthCanceled exception

前端 未结 7 693
礼貌的吻别
礼貌的吻别 2020-12-15 17:24

I\'m using python-social-auth in my Django application for authentication via Facebook. But when a user tries to login, they have been redirected to the Facebook app page, a

相关标签:
7条回答
  • 2020-12-15 17:36

    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

    0 讨论(0)
  • 2020-12-15 17:46

    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
    
    class 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',
    )
    
    0 讨论(0)
  • 2020-12-15 17:47

    The 2018 answer:

    1. Add SocialAuthExceptionMiddleware middleware to your config:

      MIDDLEWARE_CLASSES = [
          ...
          'social_django.middleware.SocialAuthExceptionMiddleware',
      ]
      
    2. 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.

    0 讨论(0)
  • 2020-12-15 17:50

    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

    0 讨论(0)
  • 2020-12-15 17:52

    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?

    0 讨论(0)
  • 2020-12-15 17:52

    This is a updated imports middleware using social_django

    from social_django.middleware import SocialAuthExceptionMiddleware
    from social_core import exceptions as social_exceptions
    from django.shortcuts import HttpResponse
    from django.shortcuts import render, redirect
    
    class FacebookAuthCanceledExceptionMiddleware(SocialAuthExceptionMiddleware):
    
        def process_exception(self, request, exception):
            if hasattr(social_exceptions, 'AuthCanceled'):
                return redirect('auth_login')
            else:
                raise exception
    
    0 讨论(0)
提交回复
热议问题