Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 455
vinCi Avatar asked Nov 12 '14 03:11

vinCi


3 Answers

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 %}
like image 85
Andrés Da Viá Avatar answered Oct 16 '22 00:10

Andrés Da Viá


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.

like image 1
lollo Avatar answered Oct 16 '22 01:10

lollo


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.

like image 1
Mohamed Ali Mimouni Avatar answered Oct 16 '22 00:10

Mohamed Ali Mimouni