]> git.parisson.com Git - django-social-auth.git/commitdiff
Trap errors and pass them by session if defined
authorMatías Aguirre <matiasaguirre@gmail.com>
Sat, 12 Feb 2011 21:25:50 +0000 (19:25 -0200)
committerMatías Aguirre <matiasaguirre@gmail.com>
Sat, 12 Feb 2011 21:25:50 +0000 (19:25 -0200)
README.rst
example/app/views.py
example/local_settings.py.template
example/templates/base.html
example/templates/error.html
social_auth/views.py

index 58499065a516e47a1c2dae4b930c7583617f8848..ef6ad44e984e36c9748100d64ed5930a93eea71d 100644 (file)
@@ -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::
 
index 849d8981f7394a436905c9172dec9957afd5b77f..a17a0cf54b7913b194acb9793bcf77081d8b0131 100644 (file)
@@ -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):
index 1371f3747a2626a25f23fe7cadf82877a8c8c0ac..ded4b1563b8d007c4b98526cc4fc8d20fd14a130 100644 (file)
@@ -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'
index 6dc7003f8b768c66ebb226b6708abbe78cbf3724..e2692f17cf164f315b360de81db058fa9a6b5bdc 100644 (file)
@@ -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;}
index ed361279cc69e59ae1e2851f0ee168b5624d6650..e90ef46ab2b56acb477b345707ea4e507319d37b 100644 (file)
@@ -4,7 +4,11 @@
 
 {% block content %}
 <div>
-  <p class="error">Sorry but some error made you impossible to login.</p>
+  <p>Sorry but some error made you impossible to login.</p>
+  {% if error_msg %}
+    <p>Details:</p>
+    <p class="error">{{ error_msg }}</p>
+  {% endif %}
   <p>Please try again <a href="/">Home</a></p>
 </div>
 {% endblock %}
index ef62728c1c210a73ecd61e5bd910a6200b0c4d3d..fc9466d016be7818c739e409c6408fbb2ba8ccb4 100644 (file)
@@ -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 \