Move admin check for register page to HOC
This commit is contained in:
1 parent
a2cc514754
commit
61b16147cb
2 files changed
+36
-17
No files matched your search
@@ -0,0 +1,31 @@
|
|||||||
|
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(() => {
|
||||||
|
console.log("admin check");
|
||||||
|
axiosInstance
|
||||||
|
.get("/auth/users/admin")
|
||||||
|
.then((response) => {
|
||||||
|
if (response.data.data === true) {
|
||||||
|
navigate("/login");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
}, [navigate]);
|
||||||
|
return <WrappedComponent {...props} />;
|
||||||
|
};
|
||||||
|
const wrappedComponentName =
|
||||||
|
WrappedComponent.displayName || WrappedComponent.name || "Component";
|
||||||
|
WithAdminCheck.displayName = `WithAdminCheck(${wrappedComponentName})`;
|
||||||
|
|
||||||
|
return WithAdminCheck;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default withAdminCheck;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import { Stack, Typography } from "@mui/material";
|
import { Stack, Typography } from "@mui/material";
|
||||||
@@ -10,10 +10,10 @@ import Logomark from "../../../assets/Images/bwl-logo-2.svg?react";
|
|||||||
import Check from "../../../Components/Check/Check";
|
import Check from "../../../Components/Check/Check";
|
||||||
import Button from "../../../Components/Button";
|
import Button from "../../../Components/Button";
|
||||||
import { credentials } from "../../../Validation/validation";
|
import { credentials } from "../../../Validation/validation";
|
||||||
import axiosInstance from "../../../Utils/axiosConfig";
|
|
||||||
import { register } from "../../../Features/Auth/authSlice";
|
|
||||||
import { createToast } from "../../../Utils/toastUtils";
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
import Field from "../../../Components/Inputs/Field";
|
import Field from "../../../Components/Inputs/Field";
|
||||||
|
import withAdminCheck from "../../../HOC/withAdminCheck";
|
||||||
|
import { register } from "../../../Features/Auth/authSlice";
|
||||||
|
|
||||||
const Register = () => {
|
const Register = () => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
@@ -39,19 +39,6 @@ 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();
|
||||||
|
|
||||||
@@ -242,4 +229,5 @@ const Register = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Register;
|
const WrappedRegister = withAdminCheck(Register);
|
||||||
|
export default WrappedRegister;
|
||||||
Reference in new issue
Block a user