Skip to content

Commit

Permalink
Merge pull request #30 from ninamartila/indra-rout-guard
Browse files Browse the repository at this point in the history
Indra rout guard
  • Loading branch information
dharmasatrya authored Sep 22, 2021
2 parents b6e28db + dd1b46a commit 32ee2e4
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 48 deletions.
126 changes: 87 additions & 39 deletions client/pinjamanku/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,69 +20,117 @@ import {
import AddLoan from "./pages/Lender/formAddLoan";
import store from "./store";
export default function App() {
console.log(localStorage.getItem("access_token"));
// console.log(localStorage.getItem("access_token"));
// console.log(localStorage.getItem("role"));
const Guard = (to, from, next) => {
console.log(to);
if (localStorage.getItem("role") === "admin" && to.meta.name === "admin") {
next();
}
if (localStorage.getItem("role") !== "admin" && to.meta.name === "admin") {
next.redirect("/");
}
if (localStorage.getItem("access_token") && to.meta.name === "register") {
next.redirect("/");
}
if (localStorage.getItem("access_token") && to.meta.name === "login") {
next.redirect("/");
}
if (
localStorage.getItem("access_token") &&
to.match.path === "/register"
localStorage.getItem("role") === "lender" &&
to.meta.name === "borrower"
) {
next.redirect("/");
}
if (localStorage.getItem("access_token") && to.match.path === "/login") {
if (
localStorage.getItem("role") === "borrower" &&
to.meta.name === "lender"
) {
next.redirect("/");
}
if (
localStorage.getItem("role") === "borrower" &&
to.meta.name === "addLoan"
) {
next.redirect("/");
}
if (!localStorage.getItem("access_token") && to.meta.name === "addLoan") {
next.redirect("/login");
}
if (!localStorage.getItem("access_token") && to.meta.name === "lender") {
next.redirect("/login");
}
if (!localStorage.getItem("access_token") && to.meta.name === "borrower") {
next.redirect("/login");
}
next();
// if (localStorage.getItem("access_token")) {
// console.log("masuk");
// next("/login");
// }
// if (localStorage.getItem("access_token") && to === "/register") {
// next().redirect("/");
// }
// next();
};
return (
<div>
<Provider store={store}>
<BrowserRouter>
<GuardProvider guards={Guard} loading={""} error={""}>
<Switch>
<Route path="/admin-dashboard/borrowerDetail/:userId">
<DetailBorrower />
</Route>
<Route path="/admin-dashboard/borrower">
<ListUserBorrower />
</Route>
<Route path="/admin-dashboard/lender">
<ListUserLender />
</Route>
<Route path="/admin-dashboard/pendingborrower">
<ListUserPendingBorrower />
</Route>
<Route path="/admin-dashboard/listLoan">
<ListLoan />
</Route>
<Route path="/lender/add-loan">
<AddLoan />
</Route>
<Route path="/lender">
<Lender />
</Route>
<Route path="/borrower">
<DasboardBorower />
</Route>
<GuardedRoute
path="/admin-dashboard/borrowerDetail/:userId"
component={DetailBorrower}
meta={{ name: "admin" }}
/>

<GuardedRoute
path="/admin-dashboard/borrower"
exact
component={ListUserBorrower}
meta={{ name: "admin" }}
/>

<GuardedRoute
path="/admin-dashboard/lender"
exact
component={ListUserLender}
meta={{ name: "admin" }}
/>

<GuardedRoute
path="/admin-dashboard/pendingborrower"
exact
component={ListUserPendingBorrower}
meta={{ name: "admin" }}
/>
<GuardedRoute
path="/admin-dashboard/listLoan"
exact
component={ListLoan}
meta={{ name: "admin" }}
/>
<GuardedRoute
path="/lender/add-loan"
exact
component={AddLoan}
meta={{ name: "addLoan" }}
/>
<GuardedRoute
path="/lender"
exact
component={Lender}
meta={{ name: "lender" }}
/>
<GuardedRoute
path="/borrower"
exact
component={DasboardBorower}
meta={{ name: "borrower" }}
/>
<GuardedRoute
path="/register"
exact
component={Register}
meta={{ auth: true }}
meta={{ name: "register" }}
/>
<GuardedRoute
path="/login"
exact
component={Login}
meta={{ auth: true }}
meta={{ name: "login" }}
/>

<GuardedRoute path="/" exact component={Home} />
Expand Down
7 changes: 3 additions & 4 deletions client/pinjamanku/src/pages/Login/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export default function Login() {
title: "Login success!",
text: "Wellcome Back!",
icon: "success",
confirmButtonText: "Cool",
confirmButtonText: "OK",
});
history.push("/borrower");
} else if (data.role === "lender") {
Swal.fire({
title: "Login success!",
text: "Wellcome Back!",
icon: "success",
confirmButtonText: "Cool",
confirmButtonText: "OK",
});
history.push("/lender");
} else {
Expand All @@ -54,9 +54,8 @@ export default function Login() {
title: "error!",
text: "Email/Password Incorect",
icon: "error",
confirmButtonText: "Cool",
confirmButtonText: "OK",
});
console.log(err);
});
}
function goRegister(e) {
Expand Down
36 changes: 32 additions & 4 deletions client/pinjamanku/src/pages/Register/Register.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function Register() {
title: "Registered!",
text: "You can now activate your account by having an interview with our team",
icon: "success",
confirmButtonText: "Cool",
confirmButtonText: "OK",
});
history.push("/");
} else if (role === "lender") {
Expand All @@ -70,19 +70,47 @@ export default function Register() {
title: "success!",
text: "Register Success GO Login",
icon: "success",
confirmButtonText: "Cool",
confirmButtonText: "OK",
});
}
} else {
throw "error";
}
})
.catch(() => {
let error = "";
if (email === "") {
error = "email canot be emty";
} else if (firstName === "") {
error = "first name canot be emty";
} else if (lastName === "") {
error = "last name canot be emty";
} else if (password === "") {
error = "password canot be emty";
} else if (address === "") {
error = "addres canot be emty";
} else if (phoneNumber === "") {
error = "phone number canot be emty";
} else if (birthDate === "") {
error = "birth date canot be emty";
} else if (accountNumber === "") {
error = "bank account number canot be emty";
} else if (bankCode === "") {
error = "please select bank";
} else if (occupation === "") {
error = "occupation canot be emty";
} else if (holderName === "") {
error = "bank holder name canot be emty";
} else if (role === "") {
error = "please select register as";
} else {
error = "something went wrong";
}
Swal.fire({
title: "error!",
text: "Fill all entire Field",
text: error,
icon: "error",
confirmButtonText: "Cool",
confirmButtonText: "OK",
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion client/pinjamanku/src/store/user/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export function fetchUserById(id, role) {
}
})
.then((data) => {
console.log(data, '========');
console.log(data, "========");
dispatch(getUserByIdSuccess(data));
return data;
})
Expand Down

0 comments on commit 32ee2e4

Please sign in to comment.