Check if current user is registered using any django social network provider

I would like to check if a user is registered through social authentication or using standard django authentication.

sort of

if user.social_auth = true?

+4
source share
5 answers

Well, after some research, I came up with this solution to make sure the user is authenticated using any social provider or only django auth by default. Check here 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 %}
+2
source
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 can add a method to the user model.

+1

django-social-auth . python-social-auth.

:

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

, Google:

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

Python:

user.social_auth.exists()

True, , False .

:

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

backends Django, python-social-auth Django.

0

, user.social_auth.exists() True, , false.

0
source

All Articles