nuxt3 navigate to other page inside useFetch interceptors

139 views Asked by At
    options.onResponseError = (error) => {
        if (process.client) {
            if (typeof error.response._data.message === "string") {
                nuxtApp.$toast.error(error.response._data.message ?? "Something wrong");
            } else {
                nuxtApp.$toast.error("Something wrong");
            }
        }

        if (error.response.status === 403 && error.response._data.message === "jwt expired") {
            token.value = null;

            navigateTo("/login");
        }

        if (error.response.status === 404 && error.response._data.message === "user not found") {
            token.value = null;

            navigateTo("/login");
        }
    };

i try to navigate user to login page if there an error with jwt, but since nuxt doenst allow me to use composable inside event handler, i cant navigate the user using navigateTo

        if (error.response.status === 403 && error.response._data.message === "jwt expired") {
            token.value = null;

            nuxtApp.$router.push("/login"); // not running after token set to null
        }

        if (error.response.status === 404 && error.response._data.message === "user not found") {
            token.value = null;

            nuxtApp.$router.push("/login"); // not running after token set to null
        }

so im using the nuxtApp instance to get $router object, it work to navigate, but if i put the navigate after making the token null, its not executed

0

There are 0 answers