8

I would like to check if a user is logged in via social authentication or using the django default authentication.

something like

if user.social_auth = true?

1
  • Long time, no workable answer... Mar 5, 2021 at 18:44

7 Answers 7

4

Ok after doing some research i came up with this solution to make sure if a user is authenticated using any social provider or just the default django auth. Check here for moreinfo..

 {% if user.is_authenticated and not backends.associated %}

 #Do or show something if user is not authenticated with social provider but default auth

 {% elif user.is_authenticated and backends.associated %}

  #Do or show something if user is authenticated with social provider

 {% else %}

 #Do or show something if none of both

 {% endif %}
1
  • Hi, thanks for the reply.. I will try this method as well on tomorrow.
    – vinCi
    Nov 12, 2014 at 11:37
2

i was searching on that and the solution is user.social_auth.exists() it will return True if the user exists in the database else it will return false.

1
from social_auth.models import UserSocialAuth

try:
    UserSocialAuth.objects.get(user_id=user.id)
except UserSocialAuth.DoesNotExist:
    print "user is logged in using the django default authentication"
else:
    print "user is logged in via social authentication"

You may to add a method to User model.

0
1

From Python:

user.social_auth.exists()

will return True if user is social, False otherwise.

From template:

{% if not backends.associated %}
    <code if user is NOT social>
{% else %}
    <code if user is social>
{% endif %}

The backends context variable is automatically set in Django templates when you include the python-social-auth app in your Django project's config.

0

Currently, the django-social-auth is deprecated. You can use python-social-auth instead.

In that case, you should use:

user.social_auth.filter(provider='BACKEND_NAME')

For instance, if current user is authenticated by Google Account:

if user.is_authenticated:
    if user.social_auth.filter(provider='google-oauth2'):
        print 'user is using Google Account!'
    else:
        print 'user is using Django default authentication or another social provider'
0

you can query all the users that are using social authentication as follow, from UserSocialAuth app:

from social_django.models import UserSocialAuth

if user in [u.user for u in UserSocialAuth.objects.all()]:
     #dosomething
1
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. Jun 28, 2020 at 7:11
0

I have same problem to recognize social logged-in users by python-social-auth and I should specify a separate tab for them in navigation bar to complete their profile page. Of course if a user has logged-in through social auth, no password has been set for him\her in DB. So I used has_usable_password() method (django.contrib.auth) for this propose. For exp:

{% if user.has_usable_password %}
  <a class="dropdown-item" href="{% url 'password_change' %}">Change password</a>
{% elif not user.has_usable_password %}
  <a class="dropdown-item" href="{% url 'set_pass' %}">Set password</a>

Obviously this tip will helpful as long as the user doesn't set password.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.