From 614ea04962425102100c15431ae9434726bebb14 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C3=ADas=20Aguirre?= Date: Sat, 12 Feb 2011 19:25:50 -0200 Subject: [PATCH] Trap errors and pass them by session if defined --- README.rst | 8 ++++++++ example/app/views.py | 5 ++++- example/local_settings.py.template | 1 + example/templates/base.html | 1 + example/templates/error.html | 6 +++++- social_auth/views.py | 10 +++++++++- 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 5849906..ef6ad44 100644 --- a/README.rst +++ b/README.rst @@ -146,6 +146,14 @@ Configuration Check Django documentation at `Login URL`_ and `Login redirect URL`_ + In case of authentication error, the message can be stored in session + if the following setting is defined:: + + SOCIAL_AUTH_ERROR_KEY = 'social_errors' + + This defined the desired session key where last error message should be + stored. It's disabled by default. + - Configure authentication and association complete URL names to avoid possible clashes:: diff --git a/example/app/views.py b/example/app/views.py index 849d898..a17a0cf 100644 --- a/example/app/views.py +++ b/example/app/views.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.http import HttpResponseRedirect from django.contrib.auth import logout as auth_logout from django.contrib.auth.decorators import login_required @@ -25,7 +26,9 @@ def done(request): def error(request): """Error view""" - return render_to_response('error.html', {'version': version}, + error_msg = request.session.pop(settings.SOCIAL_AUTH_ERROR_KEY, None) + return render_to_response('error.html', {'version': version, + 'error_msg': error_msg}, RequestContext(request)) def logout(request): diff --git a/example/local_settings.py.template b/example/local_settings.py.template index 1371f37..ded4b15 100644 --- a/example/local_settings.py.template +++ b/example/local_settings.py.template @@ -10,3 +10,4 @@ SOCIAL_AUTH_DEFAULT_USERNAME = 'socialauth_user' SOCIAL_AUTH_COMPLETE_URL_NAME = 'complete' LOGIN_ERROR_URL = '/login/error/' #SOCIAL_AUTH_USER_MODEL = 'app.CustomUser' +SOCIAL_AUTH_ERROR_KEY = 'socialauth_error' diff --git a/example/templates/base.html b/example/templates/base.html index 6dc7003..e2692f1 100644 --- a/example/templates/base.html +++ b/example/templates/base.html @@ -25,6 +25,7 @@ .helptext {font-size: 0.9em; color: #aaa; margin: 0; padding: 0;} .description {font-size: 1.0em; color: #333;} a.logout, .associated, .error {color: #c00;} + .error {padding-left: 30px;} #forkme {position: absolute; top: 0; right: 0;} #valid-badges {position: absolute; right: 10px; bottom: 10px;} #valid-badges p {display: inline;} diff --git a/example/templates/error.html b/example/templates/error.html index ed36127..e90ef46 100644 --- a/example/templates/error.html +++ b/example/templates/error.html @@ -4,7 +4,11 @@ {% block content %}
-

Sorry but some error made you impossible to login.

+

Sorry but some error made you impossible to login.

+ {% if error_msg %} +

Details:

+

{{ error_msg }}

+ {% endif %}

Please try again Home

{% endblock %} diff --git a/social_auth/views.py b/social_auth/views.py index ef62728..fc9466d 100644 --- a/social_auth/views.py +++ b/social_auth/views.py @@ -22,7 +22,15 @@ def complete(request, backend): backend = get_backend(backend, request, request.path) if not backend: return HttpResponseServerError('Incorrect authentication service') - user = backend.auth_complete() + + try: + user = backend.auth_complete() + except ValueError, e: # some Authentication error ocurred + user = None + error_key = getattr(settings, 'SOCIAL_AUTH_ERROR_KEY', None) + if error_key: # store error in session + request.session[error_key] = str(e) + if user and getattr(user, 'is_active', True): login(request, user) url = request.session.pop(REDIRECT_FIELD_NAME, '') or \ -- 2.39.5