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?
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 %}
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.
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.
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.
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'
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.
© 2022 - 2024 — McMap. All rights reserved.