问题
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:
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 %}
回答2:
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.
回答3:
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.
回答4:
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'
回答5:
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.
来源:https://stackoverflow.com/questions/26879011/check-if-current-user-is-logged-in-using-any-django-social-auth-provider