Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect user inside vue-resource http interceptor?

I want to redirect the user when I have an 401, but I can't access the router from this context:

  Vue.http.interceptors.push(function(request, next) {
    // continue to next interceptor
    next(function(response) {
        if(response.status == 401){
            //redirect user here
            //tried: Vue.$router and Vue.$route 
        }

    });
  });

How can I make this?

Using: Vue-router and Vue-resource

like image 496
thur Avatar asked Apr 03 '17 01:04

thur


1 Answers

const router = new VueRouter({
  routes
})

Vue.http.interceptors.push(function(request, next) {
    // continue to next interceptor
    next(function(response) {
        if(response.status == 401){
            router.push(...) 
        }
    });
 });
like image 64
Bert Avatar answered Nov 03 '22 19:11

Bert