Merge pull request #385 from bluewave-labs/feat/register-hoc
Feat/register hoc
This commit is contained in:
3 files changed
+60
-37
No files matched your search
+8
-4
@@ -1,8 +1,10 @@
|
|||||||
import { Routes, Route } from "react-router-dom";
|
import { Routes, Route } from "react-router-dom";
|
||||||
|
import "react-toastify/dist/ReactToastify.css";
|
||||||
|
import { ToastContainer } from "react-toastify";
|
||||||
// import "./App.css";
|
// import "./App.css";
|
||||||
import NotFound from "./Pages/NotFound";
|
import NotFound from "./Pages/NotFound";
|
||||||
import Login from "./Pages/Auth/Login";
|
import Login from "./Pages/Auth/Login";
|
||||||
import Register from "./Pages/Auth/Register";
|
import Register from "./Pages/Auth/Register/Register";
|
||||||
import HomeLayout from "./Layouts/HomeLayout";
|
import HomeLayout from "./Layouts/HomeLayout";
|
||||||
import Account from "./Pages/Account";
|
import Account from "./Pages/Account";
|
||||||
import Monitors from "./Pages/Monitors";
|
import Monitors from "./Pages/Monitors";
|
||||||
@@ -18,10 +20,10 @@ import NewPasswordConfirmed from "./Pages/Auth/NewPasswordConfirmed";
|
|||||||
import ProtectedRoute from "./Components/ProtectedRoute";
|
import ProtectedRoute from "./Components/ProtectedRoute";
|
||||||
import Details from "./Pages/Monitors/Details";
|
import Details from "./Pages/Monitors/Details";
|
||||||
import Maintenance from "./Pages/Maintenance";
|
import Maintenance from "./Pages/Maintenance";
|
||||||
import "react-toastify/dist/ReactToastify.css";
|
import withAdminCheck from "./HOC/withAdminCheck";
|
||||||
import { ToastContainer } from "react-toastify";
|
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const AdminCheckedRegister = withAdminCheck(Register);
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Routes>
|
<Routes>
|
||||||
@@ -79,7 +81,9 @@ function App() {
|
|||||||
</Route>
|
</Route>
|
||||||
|
|
||||||
<Route exact path="/login" element={<Login />} />
|
<Route exact path="/login" element={<Login />} />
|
||||||
<Route exact path="/register" element={<Register />} />
|
|
||||||
|
<Route exact path="/register" element={<AdminCheckedRegister />} />
|
||||||
|
<Route exact path="/register/:token" element={<Register />} />
|
||||||
{/* <Route path="/toast" element={<ToastComponent />} /> */}
|
{/* <Route path="/toast" element={<ToastComponent />} /> */}
|
||||||
<Route path="*" element={<NotFound />} />
|
<Route path="*" element={<NotFound />} />
|
||||||
<Route path="/forgot-password" element={<ForgotPassword />} />
|
<Route path="/forgot-password" element={<ForgotPassword />} />
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import React, { useEffect } from "react";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import axiosInstance from "../Utils/axiosConfig";
|
||||||
|
|
||||||
|
const withAdminCheck = (WrappedComponent) => {
|
||||||
|
const WithAdminCheck = (props) => {
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
axiosInstance
|
||||||
|
.get("/auth/users/admin")
|
||||||
|
.then((response) => {
|
||||||
|
if (response.data.data === true) {
|
||||||
|
navigate("/login");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
}, [navigate]);
|
||||||
|
return <WrappedComponent {...props} isAdmin={true} />;
|
||||||
|
};
|
||||||
|
const wrappedComponentName =
|
||||||
|
WrappedComponent.displayName || WrappedComponent.name || "Component";
|
||||||
|
WithAdminCheck.displayName = `WithAdminCheck(${wrappedComponentName})`;
|
||||||
|
|
||||||
|
return WithAdminCheck;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default withAdminCheck;
|
||||||
@@ -1,21 +1,21 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
import "./index.css";
|
|
||||||
import background from "../../assets/Images/background_pattern_decorative.png";
|
|
||||||
import Logomark from "../../assets/Images/bwl-logo-2.svg?react";
|
|
||||||
import Check from "../../Components/Check/Check";
|
|
||||||
import Button from "../../Components/Button";
|
|
||||||
import { credentials } from "../../Validation/validation";
|
|
||||||
import axiosInstance from "../../Utils/axiosConfig";
|
|
||||||
import { useDispatch } from "react-redux";
|
|
||||||
import { register } from "../../Features/Auth/authSlice";
|
|
||||||
import { createToast } from "../../Utils/toastUtils";
|
|
||||||
import Field from "../../Components/Inputs/Field";
|
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import { Stack, Typography } from "@mui/material";
|
import { Stack, Typography } from "@mui/material";
|
||||||
|
import { useDispatch } from "react-redux";
|
||||||
|
import PropTypes from "prop-types";
|
||||||
|
|
||||||
const Register = () => {
|
import "../index.css";
|
||||||
|
import background from "../../../assets/Images/background_pattern_decorative.png";
|
||||||
|
import Logomark from "../../../assets/Images/bwl-logo-2.svg?react";
|
||||||
|
import Check from "../../../Components/Check/Check";
|
||||||
|
import Button from "../../../Components/Button";
|
||||||
|
import { credentials } from "../../../Validation/validation";
|
||||||
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
|
import Field from "../../../Components/Inputs/Field";
|
||||||
|
import { register } from "../../../Features/Auth/authSlice";
|
||||||
|
|
||||||
|
const Register = ({ isAdmin }) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
@@ -39,24 +39,11 @@ const Register = () => {
|
|||||||
});
|
});
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
axiosInstance
|
|
||||||
.get("/auth/users/admin")
|
|
||||||
.then((response) => {
|
|
||||||
if (response.data.data === true) {
|
|
||||||
navigate("/login");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.log(error);
|
|
||||||
});
|
|
||||||
}, [form, navigate]);
|
|
||||||
|
|
||||||
const handleSubmit = async (e) => {
|
const handleSubmit = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
const adminForm = { ...form, role: "admin" };
|
const form = { ...form, role: isAdmin ? "admin" : "user" };
|
||||||
const { error } = credentials.validate(adminForm, {
|
const { error } = credentials.validate(form, {
|
||||||
abortEarly: false,
|
abortEarly: false,
|
||||||
context: { password: form.password },
|
context: { password: form.password },
|
||||||
});
|
});
|
||||||
@@ -75,8 +62,8 @@ const Register = () => {
|
|||||||
: "Error validating data.",
|
: "Error validating data.",
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
delete adminForm.confirm;
|
delete form.confirm;
|
||||||
const action = await dispatch(register(adminForm));
|
const action = await dispatch(register(form));
|
||||||
if (action.payload.success) {
|
if (action.payload.success) {
|
||||||
const token = action.payload.data;
|
const token = action.payload.data;
|
||||||
localStorage.setItem("token", token);
|
localStorage.setItem("token", token);
|
||||||
@@ -132,7 +119,7 @@ const Register = () => {
|
|||||||
<Stack gap={theme.gap.small} alignItems="center">
|
<Stack gap={theme.gap.small} alignItems="center">
|
||||||
<Logomark alt="BlueWave Uptime Icon" />
|
<Logomark alt="BlueWave Uptime Icon" />
|
||||||
<Typography component="h1" sx={{ mt: theme.gap.xl }}>
|
<Typography component="h1" sx={{ mt: theme.gap.xl }}>
|
||||||
Create admin account
|
Create{isAdmin ? " admin " : " "}account
|
||||||
</Typography>
|
</Typography>
|
||||||
</Stack>
|
</Stack>
|
||||||
<Stack gap={theme.gap.large} sx={{ mt: `calc(${theme.gap.ml}*2)` }}>
|
<Stack gap={theme.gap.large} sx={{ mt: `calc(${theme.gap.ml}*2)` }}>
|
||||||
@@ -241,5 +228,7 @@ const Register = () => {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Register.propTypes = {
|
||||||
|
isAdmin: PropTypes.bool,
|
||||||
|
};
|
||||||
export default Register;
|
export default Register;
|
||||||
Reference in new issue
Block a user