2017-01-11 98 views
0

即使我明白了这个问题,但我不确定如何解决此问题。我有一个django驱动的api,它有一个让用户更改电子邮件的端点。如果登录用户A输入已存在的电子邮件,它将检查登录的用户A是否输入了与已存在的用户B对象相对应的密码(即他拥有另一个较旧的帐户)。如果是这种情况,我必须注销实际用户A并再次登录已经存在的B帐户。注销登录后,Django csrf失败登录新用户

... 
if User.objects.filter(email=email).exists(): 
    # If the email already belongs to another account 
    user = authenticate(username=email, password=password) 
    if user is not None: 
     # The user is the owner of the existing account, he has the password 
     # Get already existing client object 
     client_existing_user_obj = Client.objects.get(user=user) 

     # get clients actual cart 
     actual_cart = client.cart 
     # update existing clients cart with newly created cart 
     client_existing_user_obj.cart = actual_cart 

     # logout user with the old email, login already existing user 
     logout(request) 
     login(request, user) 

     ... 

端点工作正常,返回200元。但是,接下来的文章中& PUT请求回答403 - “细节”:“CSRF失败:CSRF令牌丢失或不正确。”

我该如何解决这个问题?任何建议都会有帮助。

回答

1

Django rotates the CSRF token当用户登录。这是一个安全措施。

在提交更多POST/PUT请求之前,您必须在登录后刷新令牌(例如刷新页面)。