-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_token.php
More file actions
63 lines (49 loc) · 1.68 KB
/
validate_token.php
File metadata and controls
63 lines (49 loc) · 1.68 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
<?php
header("Access-Control-Allow-Origin: http://localhost/api/");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
//Archivos para decodificar el JWT
include_once 'config/core.php';
include_once 'libs/php-jwt-master/src/BeforeValidException.php';
include_once 'libs/php-jwt-master/src/ExpiredException.php';
include_once 'libs/php-jwt-master/src/SignatureInvalidException.php';
include_once 'libs/php-jwt-master/src/JWT.php';
use \Firebase\JWT\JWT;
//Obtenemos POST DATA
$data = json_decode(file_get_contents("php://input"));
//Obtenemos JWT
$jwt=isset($data->jwt) ? $data->jwt : "";
//Si JWT no es NULL
if($jwt){
try {
//Decodificamos JWT
$decoded = JWT::decode($jwt, $key, array('HS256'));
//Seteamos estado
http_response_code(200);
//Mostramos datos de usuario
echo json_encode(array(
// "message" => "Acceso garantizado.",
// "data" => $decoded->data,
"email" => $decoded->data->email,
"nombre" => $decoded->data->nombre,
"token" => $decoded->data->password,
"idCliente" => (INT)$decoded->data->idCliente,
));
}
//Significa que el JWT es invalido
catch (Exception $e){
http_response_code(401);
echo json_encode(array(
"message" => "Acceso denegado.",
"error" => $e->getMessage()
));
}
}
//El JWT esta vacio
else{
http_response_code(401);
echo json_encode(array("message" => "Token vacio."));
}
?>