2023-03-24 12:41:27 -03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'conexion/conexion.php';
|
|
|
|
require_once 'respuestas.class.php';
|
|
|
|
|
2023-03-24 14:15:02 -03:00
|
|
|
// auth hereda de conexion (solo metodos public y protected)
|
2023-03-24 12:41:27 -03:00
|
|
|
class auth extends conexion{
|
|
|
|
|
|
|
|
public function login($json){
|
|
|
|
$_respuestas = new respuestas;
|
|
|
|
$datos = json_decode($json,true);
|
|
|
|
if (!isset($datos['usuario']) || !isset($datos['password'])){
|
|
|
|
// echo "Error parametros auth";
|
|
|
|
return $_respuestas->error_400();
|
|
|
|
} else {
|
|
|
|
$usuario = $datos['usuario'];
|
|
|
|
$password = $datos['password'];
|
2023-03-24 14:15:02 -03:00
|
|
|
$password = parent::encriptar($password);
|
2023-03-24 12:41:27 -03:00
|
|
|
$datos = $this->obtenerDatosUsuario($usuario);
|
|
|
|
if ($datos){
|
2023-03-24 14:15:02 -03:00
|
|
|
// Verificar contraseña
|
|
|
|
if ($password == $datos[0]['Password']){
|
|
|
|
if ($datos[0]['Estado'] == 'Activo'){
|
|
|
|
// Crear token
|
|
|
|
$verificar = $this->insertarToken($datos[0]['UsuarioId']);
|
|
|
|
if (!$verificar == 0){
|
|
|
|
// Guardado exitoso
|
|
|
|
$result = $_respuestas->response;
|
|
|
|
$result["result"] = array("token" => $verificar);
|
|
|
|
return $result;
|
|
|
|
} else {
|
|
|
|
return $_respuestas->error_500("Error al guardar");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $_respuestas->error_200("Usuario inactivo");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $_respuestas->error_200("Contraseña incorrecta");
|
|
|
|
}
|
2023-03-24 12:41:27 -03:00
|
|
|
} else {
|
|
|
|
return $_respuestas->error_200("No existe el usuario $usuario");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function obtenerDatosUsuario($correo){
|
2023-03-24 14:15:02 -03:00
|
|
|
$query = "SELECT UsuarioId,Password,Estado FROM usuarios WHERE Usuario = '$correo'";
|
2023-03-24 12:41:27 -03:00
|
|
|
# Llamado a método de clase padre
|
|
|
|
$datos = parent::obtenerDatos($query);
|
2023-03-24 14:15:02 -03:00
|
|
|
if(isset($datos[0]["UsuarioId"])){
|
2023-03-24 12:41:27 -03:00
|
|
|
return $datos;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 14:15:02 -03:00
|
|
|
private function insertarToken($usuarioid){
|
|
|
|
$val = true;
|
|
|
|
$token = bin2hex(openssl_random_pseudo_bytes(16, $val));
|
|
|
|
$date = date("Y-m-d H:i");
|
|
|
|
$estado = "Activo";
|
|
|
|
$query = "INSERT INTO usuarios_token (UsuarioId, Token, Estado, Fecha)
|
|
|
|
VALUES('$usuarioid', '$token', '$estado', '$date')";
|
|
|
|
$verificar = parent::nonQuery($query);
|
|
|
|
if ($verificar){
|
|
|
|
return $token;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 12:41:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|