首页 > 解决方案 > 对 django logout 的 api 调用未将用户注销

问题描述

我是 Django 新手,正在尝试实现登录/注销功能。我使用 React 作为我的前端。即使在向 /auth/logout/ 发出 Axios 请求后,我仍然可以看到用户在我的后端登录。它没有给出任何错误。 在这里我可以看到用户

这些是其余的框架设置

REST_FRAMEWORK = {

    'DEFAULT_PARSER_CLASSES': [
        'rest_framework.parsers.JSONParser',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.AllowAny',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.SessionAuthentication',

    ]
}

网址.py

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^auth/', include('rest_auth.urls')),
    url(r'users/', UserListView.as_view(), name='userListView'),

]

注销视图.js

class LogoutView extends React.Component{

    componentDidMount (){

        Axios.post("http://127.0.0.1:8000/myaccount/auth/logout/", {withCredentials: true})

        .then(response => {
            console.log(response)
            })

    }

    render(){
        return(
                <Logout />
            )
    }
}

这是我在注销调用后得到的响应对象

Object
config: {url: "http://127.0.0.1:8000/myaccount/auth/logout/", method: "post", data: "{"withCredentials":true}", headers: {…}, transformRequest: Array(1), …}
data: {detail: "Successfully logged out."}
headers: {content-length: "37", content-type: "application/json"}
request: XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …}
status: 200
statusText: "OK"

标签: reactjsdjango-rest-framework

解决方案


推荐阅读