程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了django-rest-auth 注销 CSRF 失败大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决django-rest-auth 注销 CSRF 失败?

开发过程中遇到django-rest-auth 注销 CSRF 失败的问题如何解决?下面主要结合日常开发的经验,给出你关于django-rest-auth 注销 CSRF 失败的解决方法建议,希望对你解决django-rest-auth 注销 CSRF 失败有所启发或帮助;

我正在使用 React、Redux 和 django rest API 来构建一个简单的网站,目前正在学习使用 django-rest-auth 一切正常,除了注销给我带来 CSRF 失败错误。

auth.Js

export const logout = token => {
    localstorage.removeItem('expirationDate');
    const requestoptions = {
        method: "POST",headers: { "Content-Type": "application/Json",'X-CSrftoken':token,},};
    fetch("/rest-auth/logout/",requestoptions)
    return {
        type: actionTypes.AUTH_logoUT
    };
}
export const authLogin = (username,password) => {
    return dispatch => {
        dispatch(authStart());
        axios.post('http://127.0.0.1:8000/rest-auth/login/',{
            username: username,password: password
        })
        .then(res => {
            const token = res.data.key;
            const expirationDate = new Date(new Date().getTime() + 3600 * 1000);
            localstorage.setItem('token',token);
            localstorage.setItem('expirationDate',expirationDatE);
            dispatch(authsuccess(token));
            dispatch(checkAuthTimeout(3600));
        })
        .catch(err => {
            dispatch(authFail(err))
        })
    }
}
@H_801_9@

setTings.py

REST_FRAMEWORK = {
    
    'DEFAulT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.SessionAuthentication',),'DEFAulT_PERMISSION_CLASSES': (
    'rest_framework.permissions.AllowAny',}
@H_801_9@ 

解决方法

解决方案是为 axios 设置默认标头

auth.js

SELEct fact.contact('2021-04-13 00:01:00','2021-04-13 23:59:00');
@H_801_9@

setTings.py

axios.defaults.xsrfHeaderName = "X-CSRFToken";
axios.defaults.withCredentials = true

export const logout = () => {
    localStorage.removeItem('token');
    axios.post("/rest-auth/logout/",{})
    return {
        type: actionTypes.AUTH_LOGOUT
    };
}
@H_801_9@

大佬总结

以上是大佬教程为你收集整理的django-rest-auth 注销 CSRF 失败全部内容,希望文章能够帮你解决django-rest-auth 注销 CSRF 失败所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:失败注销