-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
134 lines (116 loc) · 3.3 KB
/
init.js
File metadata and controls
134 lines (116 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
window.addEventListener("load", async (event) => {
await loadEnvFile("/.env");
validateToken();
});
// Función para decodificar un JWT (base64)
function parseJwt(token) {
try {
const base64Url = token.split(".")[1]; // Obtener la parte del payload
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
const jsonPayload = decodeURIComponent(
atob(base64)
.split("")
.map(function (c) {
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
})
.join(""),
);
return JSON.parse(jsonPayload);
} catch (error) {
return null;
}
}
// Función para verificar el token
window.validateToken = () => {
const accessToken = localStorage.getItem("accessToken"); // O sessionStorage
if (!accessToken) {
const currentPath = window.location.pathname;
if (!currentPath.includes("login")) {
// Redirigir al login si no hay token
window.location.href = "/login";
}
return;
}
const decodedToken = parseJwt(accessToken);
if (decodedToken) {
const currentTime = Date.now() / 1000; // Tiempo actual en segundos
// Si el token ha expirado, redirigir al login
if (decodedToken.exp < currentTime) {
localStorage.removeItem("accessToken"); // Opcional: limpiar el token
window.location.href = "/login";
}
} else {
// Si no se puede decodificar el token, redirigir al login
localStorage.removeItem("accessToken"); // Opcional: limpiar el token
window.location.href = "/login";
}
};
async function loadEnvFile(filePath) {
try {
const response = await axios.get(filePath);
window.envVars = parseEnv(response.data);
} catch (error) {
console.error("Error al cargar el archivo .env:", error);
}
}
function parseEnv(envText) {
const envVars = {};
const lines = envText.split("\n");
// Parsear cada línea del archivo .env
lines.forEach((line) => {
const [key, value] = line.split("=");
if (key && value) {
envVars[key.trim()] = value.trim();
}
});
return envVars; // Devolver un objeto con las variables de entorno
}
async function apiRequest(method, url, data) {
try {
await loadEnvFile("/.env");
const api = axios.create({
baseURL: window.envVars.API_URL,
});
const response = await api({ method, url, data });
return response;
} catch (error) {
console.error(error);
throw error;
}
}
async function actionRequest(method, action, data) {
const actions = axios.create({
baseURL: "/actions/",
});
const headers = {
"Content-Type": "application/x-www-form-urlencoded",
};
const config = {
method: method,
url: action + ".php",
headers: headers,
data: new URLSearchParams(data).toString(),
};
try {
const response = await actions(config);
return response;
} catch (error) {
console.error("Error en la solicitud:", error);
throw error;
}
}
function formDataToJson(formData) {
const jsonObject = {};
for (const [key, value] of formData.entries()) {
// Si hay más de un valor para la misma clave, los almacenamos en un array
if (jsonObject[key]) {
if (!Array.isArray(jsonObject[key])) {
jsonObject[key] = [jsonObject[key]];
}
jsonObject[key].push(value);
} else {
jsonObject[key] = value;
}
}
return jsonObject;
}