Skip to content

Commit

Permalink
Feat: axios interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyCreeper committed Oct 31, 2023
1 parent 427d493 commit a4ff2d9
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/utils/requests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import axios, { type AxiosRequestConfig } from "axios";
import { useAsyncState } from "@vueuse/core";

axios.interceptors.response.use(
function (response) {
return response;
},
function (error) {
if (error.response.status === 401) {
localStorage.removeItem("userToken");
for (const i in localStorage) {
if (i.startsWith("room") && i.endsWith("token")) {
localStorage.removeItem(i);
}
}
setTimeout(() => (window.location.href = "/"), 1000);
}
return Promise.reject(error);
}
);

const _req = async <T>(config: AxiosRequestConfig): Promise<T | undefined> => {
const result = await axios(config);
let realData = result.data;
Expand Down

0 comments on commit a4ff2d9

Please sign in to comment.