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

7

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?

Skirr answered 12/11, 2014 at 3:58 Comment(1)
Long time, no workable answer...Emlyn
S
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 %}
Slashing answered 12/11, 2014 at 8:57 Comment(1)
Hi, thanks for the reply.. I will try this method as well on tomorrow.Skirr
S
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.

Staford answered 30/3, 2018 at 13:34 Comment(0)
U
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.

Understanding answered 12/11, 2014 at 7:40 Comment(0)
F
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.

Familial answered 29/1, 2018 at 13:35 Comment(0)
S
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'
Siena answered 4/10, 2015 at 16:15 Comment(0)
K
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
Kakapo answered 26/6, 2020 at 22:10 Comment(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.Gyrostabilizer
T
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.

Taneka answered 26/3, 2021 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.