Skip to content

Commit

Permalink
fix: redirect to login while access check failed in each loader fn of…
Browse files Browse the repository at this point in the history
… each page
  • Loading branch information
BigSillyTiger committed May 7, 2024
1 parent d4c3ac4 commit 08fe49a
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 65 deletions.
4 changes: 2 additions & 2 deletions src/apis/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import axios from "axios";

const apis = axios.create({
withCredentials: true,
//baseURL: `http://localhost:8080`,
baseURL: `http://localhost:8080`,
// playground
//baseURL: `https://cpsoftware.com.au`,
//baseURL: `http://cpsoftware.com.au`,
// alex
//baseURL: `https://tool.srclandscaping.com.au`,
baseURL: `https://test.srclandscaping.com.au`,
//baseURL: `https://test.srclandscaping.com.au`,
});

export default apis;
24 changes: 18 additions & 6 deletions src/routerAccFns/loaders/clientPageLoader.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
import { API_ADMIN, API_CLIENT, API_SETTING, API_ORDER } from "@/apis";
import { Tadmin } from "@/configs/schema/staffSchema";
import { menuList } from "@/configs/utils/router";
import { routerStore } from "@/configs/zustore";
import { adminStore, routerStore } from "@/configs/zustore";
import { LoaderFunctionArgs, defer, redirect } from "react-router-dom";

/**
* @description client page loader
* @param param0 cid
* @returns
*/
export const clientLoader = async ({ params }: LoaderFunctionArgs) => {
export const clientLoader = async ({ request, params }: LoaderFunctionArgs) => {
const pname = new URL(request.url).pathname;
routerStore.setState({ currentRouter: "client" });
try {
await API_ADMIN.accessCheck(menuList[1].id)
const result = await API_ADMIN.accessCheck(menuList[1].id)
.then((res) => {
return !res.data && redirect("/login");
if (!res.data || !(res.data as Tadmin).clients) {
return false;
} else {
adminStore.setState({ currentAdmin: res.data as Tadmin });
return res.data;
}
})
.catch((error) => {
console.log("-> Error: orders page admin check: ", error);
return redirect("/login");
return false;
});

const cid = params.cid as string;
if (!result) {
return pname
? redirect(`/login?redirect=${pname}`)
: redirect("/login");
}

const cid = params.cid as string;
const allPromise = Promise.all([
API_CLIENT.clientInfo(cid).then((res) => res.data),
API_ORDER.orderWClient(cid).then((res) => res.data),
Expand Down
31 changes: 24 additions & 7 deletions src/routerAccFns/loaders/clientsPageLoader.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { API_ADMIN, API_CLIENT } from "@/apis";
import { Tadmin } from "@/configs/schema/staffSchema";
import { menuList } from "@/configs/utils/router";
import { routerStore } from "@/configs/zustore";
import { defer, redirect } from "react-router-dom";
import { adminStore, routerStore } from "@/configs/zustore";
import { defer, LoaderFunctionArgs, redirect } from "react-router-dom";

export const clientsLoader = async () => {
console.log("-> clients loader running...");
export const clientsLoader = async ({ request }: LoaderFunctionArgs) => {
const pname = new URL(request.url).pathname;
routerStore.setState({ currentRouter: "clients" });
try {
await API_ADMIN.accessCheck(menuList[1].id).then((res) => {
return !res.data && redirect("/login");
});
const result = await API_ADMIN.accessCheck(menuList[1].id)
.then((res) => {
if (!res.data || !(res.data as Tadmin).clients) {
return false;
} else {
adminStore.setState({ currentAdmin: res.data as Tadmin });
return res.data;
}
})
.catch((error) => {
console.log("-> Error: dashboard page admin check: ", error);
return false;
});

if (!result) {
return pname
? redirect(`/login?redirect=${pname}`)
: redirect("/login");
}

const allPromise: Promise<[Tresponse]> = Promise.all([
API_CLIENT.clientAll(),
Expand Down
19 changes: 13 additions & 6 deletions src/routerAccFns/loaders/dashboardLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@ import { API_ADMIN, API_CHART, API_ORDER, API_WORKLOGS } from "@/apis";
import { Tadmin } from "@/configs/schema/staffSchema";
import { menuList } from "@/configs/utils/router";
import { adminStore, routerStore } from "@/configs/zustore";
import { defer, redirect } from "react-router-dom";
import { defer, LoaderFunctionArgs, redirect } from "react-router-dom";

export const dashboardLoader = async () => {
export const dashboardLoader = async ({ request }: LoaderFunctionArgs) => {
const pname = new URL(request.url).pathname;
routerStore.setState({ currentRouter: "dashboard" });
try {
await API_ADMIN.accessCheck(menuList[0].id)
const result = await API_ADMIN.accessCheck(menuList[0].id)
.then((res) => {
if (!(res.data as Tadmin).dashboard) {
return redirect("/login");
if (!res.data || !(res.data as Tadmin).dashboard) {
return false;
} else {
adminStore.setState({ currentAdmin: res.data as Tadmin });
return res.data;
}
})
.catch((error) => {
console.log("-> Error: orders page admin check: ", error);
return redirect("/login");
return false;
});

if (!result) {
return pname
? redirect(`/login?redirect=${pname}`)
: redirect("/login");
}

const allPromise = Promise.all([
API_WORKLOGS.wlGetToday().then((res) => res.data),
API_CHART.chartOrderPayment().then((res) => res.data),
Expand Down
20 changes: 12 additions & 8 deletions src/routerAccFns/loaders/layoutLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ export const layoutLoader = async ({ request }: LoaderFunctionArgs) => {
//routerStore.setState({ currentRouter: "login" });

try {
await API_ADMIN.adminCheck().then((res) => {
if (res.status !== RES_STATUS.SUCCESS) {
//return res.data;
return pname
? redirect(`/login?redirect=${pname}`)
: redirect("/login");
}
});
const result = await API_ADMIN.adminCheck()
.then((res) => (res.status === RES_STATUS.SUCCESS ? true : false))
.catch((error) => {
console.log("-> Error: orders page admin check: ", error);
return false;
});

if (!result) {
return pname
? redirect(`/login?redirect=${pname}`)
: redirect("/login");
}

const allPromise = Promise.all([
API_ADMIN.adminCheck().then((res) => res.data),
Expand Down
21 changes: 8 additions & 13 deletions src/routerAccFns/loaders/loginPageLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ import { API_ADMIN } from "@/apis";
import { RES_STATUS } from "@/configs/types";

export const loginLoader = async ({ request }: LoaderFunctionArgs) => {
const pname = new URL(request.url).pathname;
//const pname = new URL(request.url).pathname;
const search = new URL(request.url).searchParams.get("redirect");
await API_ADMIN.adminCheck()
.then((res) => {
//return res.status === RES_STATUS.SUCCESS && redirect("/dashboard");
if (res.status === RES_STATUS.SUCCESS) {
return redirect("/dashboard");
}
return pname
? redirect(`/login?redirect=${pname}`)
: redirect("/login");
})
const result = await API_ADMIN.adminCheck()
.then((res) => (res.status === RES_STATUS.SUCCESS ? true : false))
.catch((error) => {
console.log("-> Error: login page admin check: ", error);
return redirect("/login");
return false;
});
return search;

console.log("---> login loader redirect to login");

return result ? redirect("/dashboard") : search;
};
25 changes: 19 additions & 6 deletions src/routerAccFns/loaders/ordersPageLoader.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import { API_ADMIN, API_SETTING, API_ORDER, API_STAFF } from "@/apis";
import { Tadmin } from "@/configs/schema/staffSchema";
import { menuList } from "@/configs/utils/router";
import { routerStore } from "@/configs/zustore";
import { defer, redirect } from "react-router-dom";
import { adminStore, routerStore } from "@/configs/zustore";
import { defer, LoaderFunctionArgs, redirect } from "react-router-dom";

export const ordersLoader = async () => {
export const ordersLoader = async ({ request }: LoaderFunctionArgs) => {
const pname = new URL(request.url).pathname;
routerStore.setState({ currentRouter: "orders" });
try {
await API_ADMIN.accessCheck(menuList[2].id)
const result = await API_ADMIN.accessCheck(menuList[2].id)
.then((res) => {
return !res.data && redirect("/login");
if (!res.data || !(res.data as Tadmin).orders) {
return false;
} else {
adminStore.setState({ currentAdmin: res.data as Tadmin });
return res.data;
}
})
.catch((error) => {
console.log("-> Error: orders page admin check: ", error);
return redirect("/login");
return false;
});

if (!result) {
return pname
? redirect(`/login?redirect=${pname}`)
: redirect("/login");
}

const allPromise = Promise.all([
API_ORDER.orderAll().then((res) => res.data),
API_STAFF.staffAll().then((res) => res.data),
Expand Down
23 changes: 18 additions & 5 deletions src/routerAccFns/loaders/settingPageLoader.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import { API_ADMIN, API_SETTING } from "@/apis";
import { Tadmin } from "@/configs/schema/staffSchema";
import { menuList } from "@/configs/utils/router";
import { routerStore } from "@/configs/zustore";
import { defer, redirect } from "react-router-dom";
import { adminStore, routerStore } from "@/configs/zustore";
import { defer, LoaderFunctionArgs, redirect } from "react-router-dom";

export const settingLoader = async () => {
export const settingLoader = async ({ request }: LoaderFunctionArgs) => {
const pname = new URL(request.url).pathname;
routerStore.setState({ currentRouter: "setting" });
try {
await API_ADMIN.accessCheck(menuList[6].id)
const result = await API_ADMIN.accessCheck(menuList[6].id)
.then((res) => {
return !res.data && redirect("/login");
if (!res.data || !(res.data as Tadmin).setting) {
return false;
} else {
adminStore.setState({ currentAdmin: res.data as Tadmin });
return res.data;
}
})
.catch((error) => {
console.log("-> Error: setting page admin check: ", error);
return redirect("/login");
});

if (!result) {
return pname
? redirect(`/login?redirect=${pname}`)
: redirect("/login");
}

const allPromise = Promise.all([
API_SETTING.uniAll().then((res) => res.data),
API_SETTING.companyGet().then((res) => res.data),
Expand Down
19 changes: 13 additions & 6 deletions src/routerAccFns/loaders/staffPageLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,33 @@ import {
import { Tadmin } from "@/configs/schema/staffSchema";
import { menuList } from "@/configs/utils/router";
import { adminStore, routerStore } from "@/configs/zustore";
import { defer, redirect } from "react-router-dom";
import { defer, LoaderFunctionArgs, redirect } from "react-router-dom";

export const staffLoader = async () => {
export const staffLoader = async ({ request }: LoaderFunctionArgs) => {
const pname = new URL(request.url).pathname;
routerStore.setState({ currentRouter: "staff" });
try {
await API_ADMIN.accessCheck(menuList[5].id)
const result = await API_ADMIN.accessCheck(menuList[5].id)
.then((res) => {
//return !res.data && redirect("/login");
if (!(res.data as Tadmin).staff) {
return redirect("/login");
if (!res.data || !(res.data as Tadmin).staff) {
return false;
} else {
adminStore.setState({ currentAdmin: res.data as Tadmin });
return res.data;
}
})
.catch((error) => {
console.log("-> Error: staff page admin check: ", error);
return redirect("/login");
return false;
});

if (!result) {
return pname
? redirect(`/login?redirect=${pname}`)
: redirect("/login");
}

const allPromise = Promise.all([
API_WORKLOGS.wlAll().then((res) => res.data),
API_STAFF.staffAll().then((res) => res.data),
Expand Down
19 changes: 13 additions & 6 deletions src/routerAccFns/loaders/worklogsLoader.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
import { API_ADMIN, API_WORKLOGS } from "@/apis";
import { menuList } from "@/configs/utils/router";
import { adminStore, routerStore } from "@/configs/zustore";
import { defer, redirect } from "react-router-dom";
import { defer, LoaderFunctionArgs, redirect } from "react-router-dom";
import { TwlTableRow } from "@/configs/schema/workSchema";
import { Tadmin } from "@/configs/schema/staffSchema";

export const wlLoader = async () => {
export const wlLoader = async ({ request }: LoaderFunctionArgs) => {
const pname = new URL(request.url).pathname;
routerStore.setState({ currentRouter: "workLogs" });
try {
await API_ADMIN.accessCheck(menuList[3].id)
const result = await API_ADMIN.accessCheck(menuList[3].id)
.then((res) => {
//return !res.data && redirect("/login");
if (!(res.data as Tadmin).worklogs) {
return redirect("/login");
if (!res.data || !(res.data as Tadmin).worklogs) {
return false;
} else {
adminStore.setState({ currentAdmin: res.data as Tadmin });
return res.data;
}
})
.catch((error) => {
console.log("-> Error: orders page admin check: ", error);
return redirect("/login");
return false;
});

if (!result) {
return pname
? redirect(`/login?redirect=${pname}`)
: redirect("/login");
}

const allPromise = Promise.all([
API_WORKLOGS.wlAll().then((res) => res.data as TwlTableRow[]),
]);
Expand Down

0 comments on commit 08fe49a

Please sign in to comment.