问题
After installing and setting up social-auth i'm playing around a bit, trying to get the hang of it. I have read the docs, and have used the example project to get it running.
But until now i have no idea how to get information on a certain provider. In the example project the templatetags are always used this way:
{% for type, accounts in social_auth.associated.items %}
{% for account in accounts %}
{{account.provider}} is connected.
{% endfor %}
{% endfor %}
What i want to do now is not list all the providers, but check if someone has connected his account to (ie.) facebook. That way i could do something like this:
if user==connected_to_facebook
provide some functionality
endif
From the example above i know that social_auth.associated.items contains tuples of (type,account)
, where "facebook" would then be in a list with all values for account.provider
.
What comes to my mind is this:
{% if "facebook" in social_auth.associated.items.accounts.provider %}
which, obviously, is not going to work. I think this one will work, but not return the result i'm after:
{% if "facebook" in social_auth.associated.items[1].provider %}
Is there some functionality in Django that i can do this with? Maybe some special templatetag i'm missing? Or is this functionality already built into social_auth, and i somehow missed the documentation? Or, my worst suspicion, is it really really obvious, and i'm just ...
Any help extremely welcome.
回答1:
"social_auth" is not some tuples inside of tuples, it is a dictionairy:
{'not_associated': {}, 'backends': {'oauth2': ['facebook']},
'associated': {'oauth2': [<UserSocialAuth:testuser>]}}
That certainly makes a lot more sense, but still does not lead anywhere. So i took a look at a user that has not associated his account yet, and there that dictionary looks like this:
{'not_associated': {'oauth2': ['facebook']}, 'backends': {'oauth2': ['facebook']},
'associated': {}}
Now i found something usefull:
{% if "facebook" in social_auth.not_associated.oauth2 %}
{% else %}
provide facebook functionality
{% endif %}
That works. All you have to know is what type of authentication the backend you are looking for uses, then make sure it is not in the not_associated field of social_auth.
回答2:
In case someone needs to create a disconnect URL, here's the hack-y code I came up with, where providers
is a list I'm passing in like ["Facebook", "Twitter"]
{% for p in providers %}
<h2>Link {{ p }} Account</h2>
<p>Use your {{ p }} account to log in
{% if p|lower in social_auth.not_associated %}
<a href="{% url socialauth_associate_begin p|lower %}?next={{ request.path }}" class="off">No</a>
{% else %}
{% for item in social_auth.associated.all %}{% if item.provider == p|lower %}
<a href="{% url socialauth_disconnect_individual p|lower item.id %}" class="on">Yes</a>
{% endif %}{% endfor %}
{% endif %}
</p>
{% endfor %}
It seems like there should be a dictionary of associated accounts instead of doing this, but I couldn't figure it out. Not sure what will happen if a user managed to authorize their account twice.
来源:https://stackoverflow.com/questions/10014501/how-can-i-get-a-specific-provider-from-django-social-auth-in-a-template