How can i get a specific provider from Django social-auth in a template?

两盒软妹~` 提交于 2019-12-06 10:44:08

"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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!