1
1
<?php
2
2
require_once __DIR__ . '/../config/db.php ' ;
3
3
require_once __DIR__ . '/../vendor/autoload.php ' ;
4
+ require_once __DIR__ . '/../response.php ' ;
4
5
5
6
class AuthController
6
7
{
@@ -14,46 +15,59 @@ public function __construct()
14
15
15
16
public function generarToken ($ user_auth_id )
16
17
{
17
- // Generar un token aleatorio
18
- $ token = substr (md5 ( uniqid ( rand (), true )), 0 , 5 );
18
+ // GENERAR EL TOKEN 5 CARACTERES ALEATORIOS
19
+ $ token = substr (bin2hex ( random_bytes ( 3 )), 0 , 5 );
19
20
20
- // Insertar el token en la base de datos
21
+ // GUARDAR EL TOKEN EN LA BASE DE DATOS
21
22
$ sql = "INSERT INTO verification_tokens (user_auth_id, token, created_at) VALUES ( $ user_auth_id, ' $ token', NOW()) " ;
22
23
if ($ this ->conn ->query ($ sql ) === TRUE ) {
23
24
return $ token ;
24
25
} else {
25
- return false ;
26
+ jsonResponse (["message " => "Error al generar el token. Por favor, intente nuevamente. " ], 500 );
27
+ exit ;
26
28
}
27
29
}
28
30
29
31
public function verifyToken ($ user_auth_id , $ token )
30
32
{
31
- // Verificar si el token es válido y no ha expirado
33
+ // VERIFICAR QUE EL TOKEN SEA VÁLIDO
32
34
$ sql = "SELECT token FROM verification_tokens WHERE user_auth_id = $ user_auth_id AND token = ' $ token' " ;
33
35
$ result = $ this ->conn ->query ($ sql );
36
+ if (!$ result ) {
37
+ // SI HAY ERROR AL EJECUTAR LA CONSULTA
38
+ jsonResponse (["message " => "Error al verificar el token. Por favor, intente nuevamente. " ], 500 );
39
+ exit ;
40
+ }
34
41
if ($ result ->num_rows > 0 ) {
35
42
$ row = $ result ->fetch_assoc ();
43
+
44
+ // ESTO ES PARA CALCULAR LOS TIEMPOS DE LOS TOKENS CREADOS ANTERIORMENTE
36
45
$ tokenCreatedAt = new DateTime ($ row ['created_at ' ]);
37
46
$ currentTime = new DateTime ();
38
47
39
48
$ interval = $ currentTime ->diff ($ tokenCreatedAt );
40
49
$ minutesPassed = ($ interval ->days * 24 * 60 ) + ($ interval ->h * 60 ) + $ interval ->i ;
41
50
51
+ // SI EL TOKEN SE CREO HACE MAS DE 30 MIN SE BORRA DE LA BASE DE DATOS
42
52
if ($ minutesPassed <= 30 ) {
43
- // Eliminar tokens antiguos ya usados de este usuario
44
53
$ sqlDelete = "DELETE FROM verification_tokens WHERE user_auth_id = $ user_auth_id " ;
45
- $ this ->conn ->query ($ sqlDelete );
54
+ if ($ this ->conn ->query ($ sqlDelete ) === FALSE ) {
55
+ jsonResponse (["message " => "Error al eliminar el token antiguo. Por favor, intente nuevamente. " ], 500 );
56
+ exit ;
57
+ }
46
58
return true ;
47
59
}
48
60
}
49
- // Token expiro o no se hallo ninguno
50
61
return false ;
51
62
}
52
63
64
+ // ESTA FUNCIÓN ES PARA BORRAR TODOS LOS TOKENS QUE SE CREARON HACE MAS DE 30 MIN EN LA BASE DE DATOS
53
65
public function deleteOldToken ()
54
66
{
55
- // Eliminar tokens que tengan más de 30 minutos de haber sido creados
56
67
$ sql = "DELETE FROM verification_tokens WHERE created_at < DATE_SUB(NOW(), INTERVAL 30 MINUTE) " ;
57
- $ this ->conn ->query ($ sql );
68
+ if ($ this ->conn ->query ($ sql ) === FALSE ) {
69
+ jsonResponse (["message " => "Error al eliminar tokens antiguos. Por favor, intente nuevamente. " ], 500 );
70
+ exit ;
71
+ }
58
72
}
59
- }
73
+ }
0 commit comments