我使用此python-social-auth/social-app-django将我的网络与社交媒体相关联 我想询问在使用同一帐户注册时如何处理错误?
例如,我使用facebook注册,然后使用twitter再次注册。两者都成功注册到两个单独的帐户,当我使用我的Facebook登录,然后在我的帐户设置页面上,我想连接我已经注册的推特,然后才会显示错误消息
“AuthAlreadyAssociated at / oauth / complete / twitter /”
当我在Twitter重定向页面上授权重定向回我的网站后,会显示此消息。
简而言之,如何处理已在其他帐户中注册的帐户?
这是我的views.py:
@login_required
def settings(request):
user = request.user
try:
github_login = user.social_auth.get(provider='github')
except UserSocialAuth.DoesNotExist:
github_login = None
try:
twitter_login = user.social_auth.get(provider='twitter')
except UserSocialAuth.DoesNotExist:
twitter_login = None
try:
facebook_login = user.social_auth.get(provider='facebook')
except UserSocialAuth.DoesNotExist:
facebook_login = None
can_disconnect = (user.social_auth.count() > 1 or
user.has_usable_password())
return render(request, 'profile/settings.html', {
'github_login': github_login,
'twitter_login': twitter_login,
'facebook_login': facebook_login,
'can_disconnect': can_disconnect
})
这是我的settings.html模板:
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block title %}Navhi Microblog - Profile{% endblock %}
{% block content %}
<div class="col-lg-12 mx-auto">
<br />
<div class="card">
<div class="card-body">
{% include 'profile/base_profile.html' %}
<!--Card Body goes here-->
<div class="card-body">
<div class="card">
<div class="card-body">
{% if github_login %}
{% if can_disconnect %}
<form method="post" action="{% url 'social:disconnect' 'github' %}">
{% csrf_token %}
<button class="btn btn-danger btn-block" type="submit">Disconnect from GitHub</button>
</form>
{% else %}
<p class="text-center" style="color: red">You must <a href="{% url 'password' %}">define a password</a> for your account before disconnecting from Github.</<p class="text-center">
{% endif %}
{% else %}
<a class="btn btn-sm btn-social btn-github btn-block" href="{% url 'social:begin' 'github' %}?next={{ request.path }}">
<span class="fa fa-github"></span> Connect to Github
</a>
{% endif %}
</div>
</div>
<br />
<div class="card">
<div class="card-body">
{% if twitter_login %}
{% if can_disconnect %}
<form method="post" action="{% url 'social:disconnect' 'twitter' %}">
{% csrf_token %}
<button class="btn btn-danger btn-block" type="submit">Disconnect from Twitter</button>
</form>
{% else %}
<p class="text-center" style="color: red">You must <a href="{% url 'password' %}">define a password</a> for your account before disconnecting from Twitter.</p>
{% endif %}
{% else %}
<a class="btn btn-sm btn-social btn-twitter btn-block" href="{% url 'social:begin' 'twitter' %}?next={{ request.path }}">
<span class="fa fa-twitter"></span> Connect to Twitter
</a>
{% endif %}
</div>
</div>
<br />
<div class="card">
<div class="card-body">
{% if facebook_login %}
{% if can_disconnect %}
<form method="post" action="{% url 'social:disconnect' 'facebook' %}">
{% csrf_token %}
<button class="btn btn-danger btn-block" type="submit">Disconnect from Facebook</button>
</form>
{% else %}
<p class="text-center" style="color: red">You must <a href="{% url 'password' %}">define a password</a> for your account before disconnecting from Facebook.</p>
{% endif %}
{% else %}
<a class="btn btn-sm btn-social btn-facebook btn-block" href="{% url 'social:begin' 'facebook' %}?next={{ request.path }}">
<span class="fa fa-facebook"></span> Connect to Facebook
</a>
{% endif %}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
谢谢,我希望有人可以为此提供解决方案,对不起我的英语不好。
答案 0 :(得分:0)
您的问题已在此处得到解答: AuthAlreadyAssociated Exception in Django Social Auth
基本上答案是覆盖process_exception()
类中的默认方法social_auth.middleware.SocialAuthExceptionMiddleware
,并将此中间件添加到settings.py。
有关如何覆盖此处的详情:How do I handle exceptions on Python Social Auth