django-allauth从州不工作得到next_url

时间:2015-10-05 10:47:30

标签: django django-allauth

如果登录失败,我正尝试将用户重定向到next网址。

我修改了代码:

class OAuth2CallbackView(OAuth2View):
    def dispatch(self, request):
        if 'error' in request.GET or 'code' not in request.GET:
            # Distinguish cancel from error
            auth_error = request.GET.get('error', None)
            if auth_error == self.adapter.login_cancelled_error:
                error = AuthError.CANCELLED
            else:
                error = AuthError.UNKNOWN
            return render_authentication_error(
                request,
                self.adapter.provider_id,
                error=error)
        app = self.adapter.get_provider().get_app(self.request)
        client = self.get_client(request, app)
        try:            
            access_token = client.get_access_token(request.GET['code'])
            token = self.adapter.parse_token(access_token)
            token.app = app
            login = self.adapter.complete_login(request,
                                            app,
                                            token,
                                            response=access_token)
            login.token = token
            if self.adapter.supports_state:
                login.state = SocialLogin \
                    .verify_and_unstash_state(
                        request,
                        request.REQUEST.get('state'))
            else:
                login.state = SocialLogin.unstash_state(request)
            return complete_social_login(request, login)
        except (PermissionDenied, OAuth2Error, requests.RequestException) as e:
            try:
                next_url = SocialLogin.unstash_state(request).get('next', '') # <---- here state is not available anymore, why?
                if next_url:
                    redir_url = '%s&loginerr=y' % next_url.split('&loginerr=y')[0]
                else: 
                    raise PermissionDenied()
            except PermissionDenied: # <--- thats why, this part gets executed
                next_url = request.META.get('HTTP_REFERER', '/')
                redir_url = '%s?loginerr=y' % next_url
            return redirect(redir_url)

0 个答案:

没有答案