refactor(app): changed ways we get data so we can have better reasons why app no worky
This commit is contained in:
40
frontend/src/lib/apiHelper.ts
Normal file
40
frontend/src/lib/apiHelper.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { Router } from "@tanstack/react-router";
|
||||
import axios from "axios";
|
||||
import { toast } from "sonner";
|
||||
|
||||
let appRouter: Router<any, any> | null = null;
|
||||
|
||||
export function setApiRouter(router: Router<any, any>) {
|
||||
appRouter = router;
|
||||
}
|
||||
|
||||
export const api = axios.create({
|
||||
baseURL: "/lst/api",
|
||||
withCredentials: true,
|
||||
timeout: 15000,
|
||||
});
|
||||
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
const isNetworkError =
|
||||
error.code === "ERR_NETWORK" ||
|
||||
error.code === "ECONNABORTED" ||
|
||||
error.message === "Network Error" ||
|
||||
error.message === "Failed to fetch" ||
|
||||
!error.response;
|
||||
|
||||
if (error.response?.status === 403) {
|
||||
// redirect, toast, or show forbidden page
|
||||
toast.error("Unauthorized to be here");
|
||||
|
||||
appRouter?.navigate({ to: "/forbidden", replace: true });
|
||||
}
|
||||
|
||||
if (isNetworkError) {
|
||||
appRouter?.navigate({ to: "/app-down", replace: true });
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
@@ -1,3 +1,4 @@
|
||||
import { redirect } from "@tanstack/react-router";
|
||||
import { adminClient, genericOAuthClient } from "better-auth/client/plugins";
|
||||
import { createAuthClient } from "better-auth/react";
|
||||
import { ac, admin, manager, systemAdmin, user } from "./auth-permissions";
|
||||
@@ -16,6 +17,14 @@ export const authClient = createAuthClient({
|
||||
}),
|
||||
genericOAuthClient(),
|
||||
],
|
||||
fetchOptions: {
|
||||
onError() {
|
||||
redirect({
|
||||
to: "/app-down",
|
||||
replace: true,
|
||||
});
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const { useSession, signUp, signIn, signOut } = authClient;
|
||||
|
||||
Reference in New Issue
Block a user