admin管理员组文章数量:1435823
Interface returns 401 will enter the onResponseError function, onResponseError inside the token will be refreshed and resend the request, but no matter what the result of this function returns the request handler will receive the request.
export default defineNuxtPlugin((nuxtApp) => {
const settings = useSettings();
const api: ReturnType<typeof $fetch.create> = $fetch.create({
onResponseError: async ({ request, options, error, response }) => {
const route = useRoute();
let data = response._data;
if (response.status === 401) {
const result = await api("/api/auth/refreshToken");
if (result.success) {
return api(request, options);
} else {
await nuxtApp.runWithContext(() =>
navigateTo(`/login/?redirectUri=${route.fullPath}`, { replace: true }),
);
}
} else {
const toast = useToast();
toast.add({
color: "red",
title: `${response.status} ${response.statusText}`,
description: data.message,
icon: "i-heroicons-x-mark-20-solid",
});
}
return Promise.reject(response._data);
},
});
return {
provide: {
api,
},
};
});
How to do a senseless refresh with ofetch in nuxtjs?
本文标签: nuxtjsHow to do a senseless refresh with ofetch in nuxtjsStack Overflow
版权声明:本文标题:nuxt.js - How to do a senseless refresh with ofetch in nuxtjs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745638129a2667686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论