最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

nuxt.js - How to do a senseless refresh with ofetch in nuxtjs? - Stack Overflow

matteradmin8PV0评论

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?

Post a comment

comment list (0)

  1. No comments so far