简体   繁体   中英

Check if current user is logged in using any django social auth provider

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?

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 %}
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.

我正在搜索,解决方案是user.social_auth.exists()如果用户存在于数据库中,它将返回 True 否则它将返回 false。

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'

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM