From: Matías Aguirre Date: Mon, 15 Nov 2010 21:55:58 +0000 (-0200) Subject: Redirect user to an error url if configured X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=11d0d5dba09a2cdfecb17d8072034727c3eb8707;p=django-social-auth.git Redirect user to an error url if configured --- diff --git a/README.rst b/README.rst index 33f40b9..5b765e5 100644 --- a/README.rst +++ b/README.rst @@ -55,6 +55,7 @@ Installation LOGIN_URL = '/login-form/' LOGIN_REDIRECT_URL = '/logged-in/' + LOGIN_ERROR_URL = '/login-error/' Check Django documentation at `Login url`_ and `Login redirect url`_ diff --git a/example/app/views.py b/example/app/views.py index 535be2a..125227f 100644 --- a/example/app/views.py +++ b/example/app/views.py @@ -66,6 +66,23 @@ def done(request): """).render(Context({'user':user})), content_type='text/html;charset=UTF-8') + +def error(request): + return HttpResponse(Template( + """ + + + Error + + +

Error!

+

Sorry but some error made you impossible to login

+ Homepage + + + """).render(Context()), + content_type='text/html;charset=UTF-8') + def logout(request): auth_logout(request) return HttpResponseRedirect('/') diff --git a/example/local_settings.py.template b/example/local_settings.py.template index 6d4acc9..8a478bf 100644 --- a/example/local_settings.py.template +++ b/example/local_settings.py.template @@ -6,3 +6,4 @@ SOCIAL_AUTH_CREATE_USERS = True SOCIAL_AUTH_FORCE_RANDOM_USERNAME = False SOCIAL_AUTH_DEFAULT_USERNAME = 'socialauth_user' SOCIAL_AUTH_COMPLETE_URL_NAME = 'social:complete' +LOGIN_ERROR_URL = '/login/error/' diff --git a/example/urls.py b/example/urls.py index 150a676..35c5382 100644 --- a/example/urls.py +++ b/example/urls.py @@ -1,7 +1,7 @@ from django.conf.urls.defaults import * from django.contrib import admin -from app.views import home, done, logout +from app.views import home, done, logout, error admin.autodiscover() @@ -9,6 +9,7 @@ admin.autodiscover() urlpatterns = patterns('', url(r'^$', home, name='home'), url(r'^done/$', done, name='done'), + url(r'^error/$', error, name='error'), url(r'^logout/$', logout, name='logout'), url(r'', include('social_auth.urls', namespace='social')), url(r'^admin/', include(admin.site.urls)), diff --git a/social_auth/views.py b/social_auth/views.py index 7864638..f76b98f 100644 --- a/social_auth/views.py +++ b/social_auth/views.py @@ -43,5 +43,8 @@ def complete(request, backend): user = backend.auth_complete() if user and user.is_active: login(request, user) - return HttpResponseRedirect(request.session.pop(REDIRECT_FIELD_NAME, - settings.LOGIN_REDIRECT_URL)) + url = request.session.pop(REDIRECT_FIELD_NAME, + settings.LOGIN_REDIRECT_URL) + else: + url = getattr(settings, 'LOGIN_ERROR_URL', settings.LOGIN_URL) + return HttpResponseRedirect(url)