DEV Community

Cover image for How to get refresh token in Vite Vue 3
alin
alin

Posted on

How to get refresh token in Vite Vue 3

<script setup>

const accessrefresh = async () => {
    axios.defaults.headers.common['Authorization'] = storeaccesstoken.accesstoken;
    await axios.create({withCredentials: true}).post("http://localhost:81/auth/refresh/")
        .then((response) => {   axios.defaults.headers.common['Authorization'] = response.data.token;
            storeaccesstoken.stateAccessToken(response.data.token);
        })
        .catch((errors) => {
            console.log("errors access refresh: ",errors);
            if(errors?.response?.data?.message==='error') {
                Swal.fire({
                title: "Error!",
                text: "Access denied!",
                icon: "error",
                confirmButtonText: "Log in please!"
              });
            }
            storeaccesstoken.stateAccessToken(null);
            storeaccesstoken.setStateUserFalse();
            router.push({ path: '/login' })
        });
};

onBeforeMount(() => {
    window.addEventListener("beforeunload",accessrefresh());
})

<script>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)