如何解决Django:为什么通过社交身份验证登录后关联了错误的用户?
我正在尝试在 Django 中实现社交身份验证。这个想法是,第一个用户必须登录 Django,然后才能登录 google 或 facebook 以进一步请求数据。但出于某种原因,Django 将“User social auths”表中的所有社交登录与 admin 相关联。前端用Vue,后端用Django。
我的views.py
@api_view(('GET',))
def test_login(request):
permission_classes = [IsAuthenticated,]
user = request.user
print(user) #prints the right user
print(user.id)
return render(request,'connectors/registration/login-popup-url.html')
我的 urls.py
path('test_login',views.test_login,name="test_login"),
connectors/registration/login-popup-url.html 模板 该 url 将在 Vue 函数生成的新窗口中呈现。 Vue 拒绝打开 HTML 模板。
{% block content %}{% url 'social:begin' 'google-oauth2' %}{% endblock %}
testLogin: function() {
var that = this;
console.log(that.accesstokenJWT); //Django simplejwt token
fetch('http://localhost:8000/test_login',{ method: 'GET',headers: {
'Accept': 'application/json','Content-Type': 'application/json','Authorization': 'Bearer ' + that.accesstokenJWT
}})
.then(function(response) {
console.log(response);
return response.text();
}).then(
function(response) {
console.log(response);
console.log('http://localhost:8000' + response);
window.open('http://localhost:8000' + response,"","_blank"); //open
// social auth in a new window
}
);
}
loginDjango: function() {
var that = this;
fetch('http://localhost:8000/users/login',{
method: 'POST',headers: {
'Accept': 'application/json','Content-Type': 'application/json'
},body: JSON.stringify({
username: that.django.username,password: that.django.password
})
}).then(response => response.json()
).then(function(response) {
console.log('auth',response);
});
},
我的管道
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details','social_core.pipeline.social_auth.social_uid','social_core.pipeline.social_auth.auth_allowed','social_core.pipeline.social_auth.social_user','social_core.pipeline.user.get_username','social_core.pipeline.social_auth.associate_by_email',# tried with / without this step
'social_core.pipeline.social_auth.associate_user','social_core.pipeline.social_auth.load_extra_data','social_core.pipeline.user.user_details',)
当我第一次在 Vue 中通过表单登录 Django 时,将用户名和密码发送给 Django,并返回一个 accesstoken 并存储在 localStorage 和变量 accesstokenJWT 中。
然后当我尝试调用 testLogin 函数时,令牌在 Headers 中发送到 test_login 视图。 test_login "print(user) and print(user.id)",返回刚刚登录Vue的正确用户。
在弹出一个谷歌登录窗口之后,我输入电子邮件,授予我的应用程序的权限,点击确定。但是在 Django 的管理面板中的“用户社交身份验证”中,无论用户是谁,所有登录都与管理员相关联。
test_login 视图中的print(user) 和print(user.id) 显示是用户,登录Vue
我的理想结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。