|
@@ -1,15 +1,54 @@
|
1
|
1
|
import { Navigate, useLocation } from 'react-router-dom';
|
2
|
|
-// import useAuth from '../Auth/useAuth';
|
3
|
2
|
import { useSelector } from 'react-redux';
|
|
3
|
+import jwt_decode from "jwt-decode";
|
|
4
|
+
|
|
5
|
+const Recluter = {
|
|
6
|
+ authorities: "Reclutador",
|
|
7
|
+ app_name: 'Dashboard',
|
|
8
|
+ type: 1
|
|
9
|
+}
|
|
10
|
+
|
|
11
|
+const User = {
|
|
12
|
+ authorities: "User",
|
|
13
|
+ app_name: 'User',
|
|
14
|
+ type: 2
|
|
15
|
+}
|
4
|
16
|
|
5
|
17
|
export function RequireToken({ children }) {
|
6
|
18
|
|
7
|
19
|
let token = useSelector((state) => state.token.token)
|
8
|
20
|
let location = useLocation();
|
|
21
|
+ let app_name = children?.type?.name
|
9
|
22
|
|
10
|
23
|
if (!token) {
|
11
|
|
- console.log('falta authorizacion user')
|
12
|
|
- return <Navigate to="/logincd" state={{ from: location }} replace />;
|
|
24
|
+ if (app_name === Recluter.app_name) {
|
|
25
|
+ return <Navigate to="/login" state={{ from: location }} replace />;
|
|
26
|
+ } else {
|
|
27
|
+ return <Navigate to="/logincd" state={{ from: location }} replace />;
|
|
28
|
+ }
|
|
29
|
+ } else {
|
|
30
|
+
|
|
31
|
+ let body_token = jwt_decode(token);
|
|
32
|
+ let token_access = body_token?.authorities;
|
|
33
|
+
|
|
34
|
+ if (app_name === User.app_name) {
|
|
35
|
+ let valid_user = token_access.includes(User.authorities);
|
|
36
|
+ if (!valid_user) {
|
|
37
|
+ return <Navigate to="/logincd" state={{ from: location }} replace />;
|
|
38
|
+ } else {
|
|
39
|
+ return children;
|
|
40
|
+ }
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ if (app_name === Recluter.app_name) {
|
|
44
|
+ let valid_user = token_access.includes(Recluter.authorities);
|
|
45
|
+ if (!valid_user) {
|
|
46
|
+ return <Navigate to="/logincd" state={{ from: location }} replace />;
|
|
47
|
+ } else {
|
|
48
|
+ return children;
|
|
49
|
+ }
|
|
50
|
+ }
|
|
51
|
+
|
13
|
52
|
}
|
14
|
53
|
|
15
|
54
|
return children;
|