Implemented protected route component, added protected routes. Fixed redircts. Removed token from localstorage, not being used
This commit is contained in:
1 parent
5beaa8dfe1
commit
b84a5e017d
4 files changed
+26
-14
No files matched your search
+18
-7
@@ -27,14 +27,25 @@ function App() {
|
|||||||
path="/monitors"
|
path="/monitors"
|
||||||
element={<ProtectedRoute Component={Monitors} />}
|
element={<ProtectedRoute Component={Monitors} />}
|
||||||
/>
|
/>
|
||||||
|
<Route
|
||||||
<Route path="incidents" element={<Incidents />} />
|
path="incidents"
|
||||||
<Route path="status" element={<Status />} />
|
element={<ProtectedRoute Component={Incidents} />}
|
||||||
<Route path="integrations" element={<Integrations />} />
|
/>
|
||||||
<Route path="settings" element={<Settings />} />
|
<Route
|
||||||
|
path="status"
|
||||||
|
element={<ProtectedRoute Component={Status} />}
|
||||||
|
/>
|
||||||
|
<Route
|
||||||
|
path="integrations"
|
||||||
|
element={<ProtectedRoute Component={Integrations} />}
|
||||||
|
/>
|
||||||
|
<Route
|
||||||
|
path="settings"
|
||||||
|
element={<ProtectedRoute Component={Settings} />}
|
||||||
|
/>
|
||||||
|
<Route exact path="/login" element={<Login />} />
|
||||||
|
<Route exact path="/register" element={<Register />} />
|
||||||
</Route>
|
</Route>
|
||||||
<Route exact path="/register" element={<Register />} />
|
|
||||||
<Route exact path="/login" element={<Login />} />
|
|
||||||
<Route exact path="/demo" element={<Demo />} />
|
<Route exact path="/demo" element={<Demo />} />
|
||||||
{/* <Route path="/toast" element={<ToastComponent />} /> */}
|
{/* <Route path="/toast" element={<ToastComponent />} /> */}
|
||||||
<Route path="*" element={<NotFound />} />
|
<Route path="*" element={<NotFound />} />
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ function NavBar() {
|
|||||||
// Clear auth state
|
// Clear auth state
|
||||||
dispatch(clearAuthState());
|
dispatch(clearAuthState());
|
||||||
dispatch(clearMonitorState());
|
dispatch(clearMonitorState());
|
||||||
navigate("/");
|
navigate("/login");
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Route, Navigate } from "react-router-dom";
|
import { Navigate } from "react-router-dom";
|
||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
|
|
||||||
@@ -15,9 +15,12 @@ import PropTypes from "prop-types";
|
|||||||
|
|
||||||
const ProtectedRoute = ({ Component, ...rest }) => {
|
const ProtectedRoute = ({ Component, ...rest }) => {
|
||||||
const authState = useSelector((state) => state.auth);
|
const authState = useSelector((state) => state.auth);
|
||||||
console.log(authState);
|
|
||||||
|
|
||||||
return true ? <Component {...rest} /> : <Navigate to="/login" replace />;
|
return authState.authToken ? (
|
||||||
|
<Component {...rest} />
|
||||||
|
) : (
|
||||||
|
<Navigate to="/login" replace />
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
ProtectedRoute.propTypes = {
|
ProtectedRoute.propTypes = {
|
||||||
|
|||||||
@@ -52,9 +52,7 @@ const Login = () => {
|
|||||||
await loginValidation.validateAsync(form, { abortEarly: false });
|
await loginValidation.validateAsync(form, { abortEarly: false });
|
||||||
const action = await dispatch(login(form));
|
const action = await dispatch(login(form));
|
||||||
if (action.meta.requestStatus === "fulfilled") {
|
if (action.meta.requestStatus === "fulfilled") {
|
||||||
const token = action.payload.data;
|
navigate("/monitors");
|
||||||
localStorage.setItem("token", token);
|
|
||||||
navigate("/");
|
|
||||||
}
|
}
|
||||||
if (action.meta.requestStatus === "rejected") {
|
if (action.meta.requestStatus === "rejected") {
|
||||||
const error = new Error("Request rejected");
|
const error = new Error("Request rejected");
|
||||||
|
|||||||
Reference in new issue
Block a user