|
@@ -1,15 +1,54 @@
|
|
|
import { Navigate, useLocation } from 'react-router-dom';
|
|
import { Navigate, useLocation } from 'react-router-dom';
|
|
|
-// import useAuth from '../Auth/useAuth';
|
|
|
|
|
import { useSelector } from 'react-redux';
|
|
import { useSelector } from 'react-redux';
|
|
|
|
|
+import jwt_decode from "jwt-decode";
|
|
|
|
|
+
|
|
|
|
|
+const Recluter = {
|
|
|
|
|
+ authorities: "Reclutador",
|
|
|
|
|
+ app_name: 'Dashboard',
|
|
|
|
|
+ type: 1
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const User = {
|
|
|
|
|
+ authorities: "User",
|
|
|
|
|
+ app_name: 'User',
|
|
|
|
|
+ type: 2
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
export function RequireToken({ children }) {
|
|
export function RequireToken({ children }) {
|
|
|
|
|
|
|
|
let token = useSelector((state) => state.token.token)
|
|
let token = useSelector((state) => state.token.token)
|
|
|
let location = useLocation();
|
|
let location = useLocation();
|
|
|
|
|
+ let app_name = children?.type?.name
|
|
|
|
|
|
|
|
if (!token) {
|
|
if (!token) {
|
|
|
- console.log('falta authorizacion user')
|
|
|
|
|
- return <Navigate to="/logincd" state={{ from: location }} replace />;
|
|
|
|
|
|
|
+ if (app_name === Recluter.app_name) {
|
|
|
|
|
+ return <Navigate to="/login" state={{ from: location }} replace />;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return <Navigate to="/logincd" state={{ from: location }} replace />;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+
|
|
|
|
|
+ let body_token = jwt_decode(token);
|
|
|
|
|
+ let token_access = body_token?.authorities;
|
|
|
|
|
+
|
|
|
|
|
+ if (app_name === User.app_name) {
|
|
|
|
|
+ let valid_user = token_access.includes(User.authorities);
|
|
|
|
|
+ if (!valid_user) {
|
|
|
|
|
+ return <Navigate to="/logincd" state={{ from: location }} replace />;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return children;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (app_name === Recluter.app_name) {
|
|
|
|
|
+ let valid_user = token_access.includes(Recluter.authorities);
|
|
|
|
|
+ if (!valid_user) {
|
|
|
|
|
+ return <Navigate to="/logincd" state={{ from: location }} replace />;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return children;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return children;
|
|
return children;
|