-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
92 lines (70 loc) · 2.81 KB
/
Copy pathscript.js
File metadata and controls
92 lines (70 loc) · 2.81 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
document.getElementById('guardarButton')?.addEventListener('click', async function() {
try {
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
const data = {
username: username,
password: password
};
const response = await fetch('http://localhost:5208/users/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (!response.ok) {
console.error('Datos no validos del servidor:', response);
throw new Error('Error al ingresar usuario o contraseña');
}
const body = await response.json();
const phpResponse = await fetch('guardarSesion.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams(body)
});
if (!phpResponse.ok) {
console.error('Error al iniciar sesión en PHP:', phpResponse);
throw new Error('Error al iniciar sesión');
}
window.location.href = 'bienvenido.php';
} catch (error) {
console.error('Error en la autenticación:', error.message);
alert('No se pudo conectar en este momento');
}
});
document.getElementById('guardarButtonRegister')?.addEventListener('click', async function() {
try {
const fullnameRegister = document.getElementById('fullnameRegister').value;
const usernameRegister = document.getElementById('usernameRegister').value;
const passwordRegister = document.getElementById('passwordRegister').value;
const confirmPassword = document.getElementById('confirmPassword').value;
if (passwordRegister !== confirmPassword) {
alert('Las contraseñas no coinciden');
return;
}
const data = {
fullnameRegister: fullnameRegister,
usernameRegister: usernameRegister,
passwordRegister: passwordRegister
};
const response = await fetch('http://localhost:5208/users/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (!response.ok) {
console.error('Error al registrar:', response);
throw new Error('Error al registrarse');
}
const result = await response.json();
alert('Registro exitoso');
} catch (error) {
console.error('Error durante el registro:', error.message);
alert('No se pudo conectar en este momento');
}
});