0

well I have my code, a few days ago it works perfectly, but now when I try to use two method it says me "CSRF Failed: CSRF token missing or incorrect". Here is my code:

/* Logout Function I'm using rest_auth */ 
   function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);

                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    var csrftoken = getCookie('csrftoken');     
    const router = new VueRouter({
        routes: [
            {path: '/logout', alias: '/logout'}
        ],
    });
    new Vue({
        el: '#logout',
        router: router,
        delimiters: ['${','}'],
        headers: {'HTTP_X_XSRF_TOKEN': csrftoken},
        data:{      
            loading: false,
            newUser: { 'username': null, 'password': null},
        },
        mounted: function(){

        },
        methods: {
            logout: function(){
                this.$http.post('/auth/logout/').then((response) => {               
                    loading = true;
                    sessionStorage.removeItem("auth");
                    router.go('/login');
                }).catch((err) => {
                    console.log(err);
                })
            }
        }
    });    

this a few days ago works without a problem. Please help me! and forward thanks!

Dibu Escobedo
  • 165
  • 1
  • 3
  • 13
  • Is the cookie still there? You probally need to obtain a new CSRF cookie: https://stackoverflow.com/questions/30539259/how-long-should-the-lifetime-of-a-csrf-token-be – Jelmer Jan 07 '19 at 18:22
  • @Jelmer thanks for the help, yes it still there... it expires at 2020, sometimes it works and sometimes it doesn't. – Dibu Escobedo Jan 08 '19 at 14:09

0 Answers0