How can I display a meaningful login error messages to user within python-social-auth?

Question:

I am using python-social-auth with Django 1.6 and things are working well.

I defined SOCIAL_AUTH_LOGIN_ERROR_URL to point to a view I have within my app to show an error to the user when the login does not go as planned.

My system does not allow creation of new users. You must first log into your account using the regular credentials, associate your account with a social account, and only then can you log in using a social auth mechanism.

If a user attempts to log into my system using a social account which is not registered to a user within my system, I want to display a custom message. The problem is, the view located at SOCIAL_AUTH_LOGIN_ERROR_URL does not receive any info about what the error could be. The best I can do is say “An error occurred while trying to log in.”

How would I trap such errors and display a custom message to my users when that particular error happens?

Asked By: Krystian Cybulski

||

Answers:

Take a look at the middleware from the django_app:

https://github.com/omab/python-social-auth/blob/526439ac66b00ce801e4fc5df89633a66eb8ffb2/social/apps/django_app/middleware.py

and either create your own based on this or extended by overriding. As mentioned there, probably the get_message method.

Answered By: Rush

to handle this, I do the following:

settings.py

...
SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
    # the custom pipeline step
    'accounts.views.user_details_after',
)
...

views.py

def user_details_after(strategy, details, user=None, *args, **kwargs):
    if not user:
        messages.info(strategy.request, "You have to sign up first.")

in the loging template I render the message to display it to the user.

Answered By: udo

A simpler way is to just render the error page with the info you want, like

return render(
        request,
        "error.html",
        {
        "error_title":"Custom Error",
        "error_message":"This is an error"
        }
    )

and your templates/error.html template

<h1>{{ error_title }}</h1>
<p>{{ error_message }}</p>
Answered By: Tiago Martins Peres