3

I'm adding google login to my django app for use in my office only. Logins work fine, but I'm trying to add better error pages. I've written a login error page at /login/error that gives a bit of information about what went wrong, but I can't seem to get django to display that page when its needed.

Logins will fail when the gmail account is not on our companies account (*@myoffice.com) or when the user denies permission. I would like it to redirect to /login/error but the redirect never works.

I have tried adding

SOCIAL_AUTH_LOGIN_ERROR_URL = '/login/error/'
SOCIAL_AUTH_BACKEND_ERROR_URL = '/login/error/'

to my settings.py file. Neither catches the error that I am looking to handle with this page. I can confirm that /login/error works correctly and the issue is just with getting the correct redirect.

Any help would be greatly appreciated.

Update: Heres some more code since this wasn't as quick a fix as I had hoped

From settings.py (key and secret is omitted, whitelisted url is changed)

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'thelibrary',
    'home',
    'social.apps.django_app.default',
)

AUTHENTICATION_BACKENDS = (
   'social.backends.google.GoogleOAuth2',
   'django.contrib.auth.backends.ModelBackend',
)


LOGIN_REDIRECT_URL = '/'


SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['example.com']

SOCIAL_AUTH_LOGIN_ERROR_URL = '/login/error/'
SOCIAL_AUTH_BACKEND_ERROR_URL = '/login/error/'

From my template that asks a user to login

{% if user and not user.is_anonymous %}

     Not yet implemented

{% else %}
    Welcome guest!
    <br />
    Please  <a href="{% url 'social:begin' 'google-oauth2' %}?next={{ request.path   }}">Login with Google</a>
{% endif %}

Heres the error output I get in the console when the request fails

[21/Oct/2014 13:44:42] "GET /login/google-oauth2/ HTTP/1.1" 302 0
[21/Oct/2014 13:44:51] "GET /complete/google-oauth2/?state=3YjuxWntctsrg6emGEPfMjzeeP4NSazX&code=4/lobir0Y2ZZnEPnJRE99gjqhjZh2RbwJC2wfCrs5Zy9A.Envfd1PM7OcaXmXvfARQvtiwnetlkgI HTTP/1.1" 500 157344
Colin Murphy
  • 1,105
  • 3
  • 12
  • 22
  • 1
    Your problem might be that ``python-social-auth`` is catching your URL ``/login/error/`` as if it was a ``backend`` URL (like ``/login/google-oauth2/``), so it's trying to attempt a login using the ``error`` backend, which obviously doesn't exist. Try changing the URL format to ``/login-error`` to see if that works. – omab Oct 22 '14 at 12:48

1 Answers1

0

SOCIAL_AUTH_LOGIN_ERROR_URL seems to be the right choice, it worked for me. Probably the error is somewhere else on your application, but without any code it is impossible to tell.

Where the browser gets redirected to, instead?

You can check your browser developer tools to see the network history (just make sure it wont get cleared on redirect/reload). Reading through all requests and trying to understand their order and point of failure might give you some hint.

Nopik
  • 542
  • 1
  • 4
  • 15
  • I added code and the error I get in the console of the test server. – Colin Murphy Oct 21 '14 at 13:47
  • So, your server is returning error code 500 on /complete/google-oauth2/.. request, you should investigate why. Check your developer tools panel in the browser to see what was the content of the response to that request. If you can run Django in debug mode, it should contain some stack trace, or at least error message. – Nopik Oct 22 '14 at 11:06